使用している WPF エディターの ControlTemplate 内のボタンを有効または無効にしたいだけです。
4 に答える
ボタンを簡単に有効または無効にするなど、RoutedUICommandsを使用してボタンを簡単に制御することをお勧めします。コマンドはボタンへの参照を必要としないので、これも問題を解決します。
ここを参照してください:
<Window.CommandBindings>
<CommandBinding
Command="{x:Static local:MyCommands.MyCommand}"
Executed="CommandBinding_Executed"
CanExecute="CommandBinding_CanExecute" />
</Window.CommandBindings>
<Window.Resources>
<ControlTemplate x:Key="MyTemplate" TargetType="Label">
<Button Command="{x:Static local:MyCommands.MyCommand}">
<TextBlock>
Click Me
<TextBlock Text="{TemplateBinding Content}" />
</TextBlock>
</Button>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<Label Template="{StaticResource MyTemplate}">1</Label>
<Label Template="{StaticResource MyTemplate}">2</Label>
<Label Template="{StaticResource MyTemplate}">3</Label>
<Label Template="{StaticResource MyTemplate}">4</Label>
<Label Template="{StaticResource MyTemplate}">5</Label>
</StackPanel>
これを使って:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CommandBinding_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
{
// your logic here
int _Integer = -1;
int.TryParse(e.Parameter.ToString(), out _Integer);
e.CanExecute = _Integer % 2 == 0;
}
private void CommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
{
// do something when clicked
}
}
public static class MyCommands
{
public static RoutedUICommand MyCommand = new RoutedUICommand();
}
次のようになります:
それを明示的に行うべきではありませんが、代わりに TriggersControlTemplate
自体で設定する必要があります。これらのトリガーは、他の変更に反応し、 のEnabled
プロパティを設定しますButton
。
推奨される方法は、xaml マークアップを使用してボタンの Enabled プロパティの設定をトリガーすることであることに私は Joel に同意します。バリューコンバータ。
ただし、C# で明示的に行う必要がある場合は、ボタンのテンプレート プロパティで FindName() メソッドを使用できます。ここにリンクされている MSDN の「ハウツー」があります。
controlTemplates と dataTemplates のボタンに問題がありました。クライアントが望むものを正確に提供するには、トリガーが煩雑すぎることがよくあります。私が最初に試した方法は、これらのより難しいテンプレートをインラインで定義して、画面の ViewModel でコマンド (およびコード ビハインド イベント) にバインドできるようにすることでした。
独自のコントロールをテンプレート化する場合、最も簡単な方法は、単にボタンに名前を付けてから、次のようなものを使用することです。
hourHand = this.Template.FindName( "PART_HourHand", this ) as Rectangle;
...TemplateCommandStore
しかし最近、私はタクトを変更しました(個々のファイルに戻って..再利用!)が、少し複雑になるすべてのテンプレートに接尾辞を付けるViewModel(ある種の)を追加し始めました。カスタムコントロールをテンプレート化するときにもこれを使用します(ほとんどの場合、一貫性のため)
public class BookingDetailsTemplateCommandStore
{
public OpenWindowCommand_Command OpenWindowCommand_Command { get; set; }
public BookingDetailsTemplateCommandStore()
{
OpenWindowCommand_Command = new OpenWindowCommand_Command( this );
}
}
その後、このコマンドをテンプレートで問題なく使用できます。また、(依存関係?) プロパティをコマンド ストアにバインドして、コントロール参照をコマンド ストアに渡して、イベントをキャプチャし、テンプレートをより正確に制御することもできます。
<DataTemplate DataType="{x:Type LeisureServices:BookingSummary}">
<Border Height="50" otherStyling="xyz">
<Border.Resources>
<local:BookingDetailsTemplateCommandStore x:Key="CommandStore" />
</Border.Resources>
<DockPanel>
<Button otherStyling="xyz"
Command="{Binding Source={StaticResource CommandStore},
Path=OpenWindowCommand_Command}" />
MVVM:グラフィカル ロジックがまさにそれであり、ビジネス ロジックとは完全に区別されている限り、この方法論は MVVM に干渉しません。本質的に、私はテンプレートに一貫した C# コード機能を追加します。