1

現在のタブが変更されたときのTabItem内のボタンと同じように、TabControlの外のボタンを無効および有効にするつもりでした。ただし、TabItemのCommandBindingsは、ビジュアルツリーの「上」に影響を与えないようです。これを行う正しい方法何ですか?

このXAMLの場合:

<Window x:Class="WpfApplication10.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication10"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Button Content="MyCommand1" Command="local:MainWindow.MyCommand1" />
    <Button Content="MyCommand2" Command="local:MainWindow.MyCommand2" />
    <TabControl>
        <TabItem Header="tabItem1" Name="tabItem1">
            <TabItem.CommandBindings>
                <CommandBinding Command="local:MainWindow.MyCommand1" 
                                Executed="ExecuteMyCommand" />
            </TabItem.CommandBindings>
            <StackPanel>
                <Button Content="MyCommand1" Command="local:MainWindow.MyCommand1" />
                <Button Content="MyCommand2" Command="local:MainWindow.MyCommand2" />
            </StackPanel>
        </TabItem>
        <TabItem Header="tabItem2" Name="tabItem2">
            <TabItem.CommandBindings>
                <CommandBinding Command="local:MainWindow.MyCommand2" 
                                Executed="ExecuteMyCommand"/>
            </TabItem.CommandBindings>
            <StackPanel>
                <Button Content="MyCommand1" Command="local:MainWindow.MyCommand1" />
                <Button Content="MyCommand2" Command="local:MainWindow.MyCommand2" />
            </StackPanel>
        </TabItem>
    </TabControl>
</StackPanel>
</Window>

このコードの背後にあるもの:

    public static readonly RoutedUICommand MyCommand1 = new RoutedUICommand();
    public static readonly RoutedUICommand MyCommand2 = new RoutedUICommand();
    public MainWindow()
    {
        InitializeComponent();
    }
    private void ExecuteMyCommand(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("Hello");
    }
4

3 に答える 3

1

MSFT は、こちらの WPF フォーラムで正しい答えをくれました ( http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bb3d1eb1-96fa-4fbc-beda-799613acb9f7 ) 。

<StackPanel>
     <StackPanel FocusManager.IsFocusScope="True">
         <Button Content="MyCommand1" Command="local:Window8.MyCommand1" />
         <Button Content="MyCommand2" Command="local:Window8.MyCommand2" />
     </StackPanel>
     <TabControl>
         <TabItem Header="tabItem1" Name="tabItem1">
             <TabItem.CommandBindings>
                 <CommandBinding Command="local:Window8.MyCommand1" Executed="ExecuteMyCommand" />
             </TabItem.CommandBindings>
             <StackPanel>
                 <Button Content="MyCommand1" Command="local:Window8.MyCommand1" />
                 <Button Content="MyCommand2" Command="local:Window8.MyCommand2" />
             </StackPanel>
         </TabItem>
         <TabItem Header="tabItem2" Name="tabItem2">
             <TabItem.CommandBindings>
                 <CommandBinding Command="local:Window8.MyCommand2" Executed="ExecuteMyCommand"/>
             </TabItem.CommandBindings>
             <StackPanel>
                 <Button Content="MyCommand1" Command="local:Window8.MyCommand1" />
                 <Button Content="MyCommand2" Command="local:Window8.MyCommand2" />
             </StackPanel>
         </TabItem>
     </TabControl>
</StackPanel>
于 2010-08-05T08:56:14.797 に答える
0

これがすべてのコードですか?

MyCommandsXを無効にする特別なCanExecuteが定義されていますか?
または、バインドされたボタンのEnabledプロパティにバインドがあり、INotifyPropertyChangedなどを実装していますか?

または、コードサンプルで有効/無効にする必要があるのはなぜですか?
私はあなたが私に尋ねます、私はコードがボタンを無効にすることを期待していません。

アップデート1:

たとえば、周囲のスタックパネルにコマンドバインディングを追加することで、ボタンを有効にすることができます。

    <StackPanel>
    <StackPanel.CommandBindings>
            <CommandBinding Command="local:MainWindow.MyCommand1"  
                            Executed="ExecuteMyCommand" />
    </StackPanel.CommandBindings>
    <Button Content="MyCommand1" Command="local:MainWindow.MyCommand1" />
    <Button Content="MyCommand2" Command="local:MainWindow.MyCommand2" />
    <TabControl>

コマンドバインディングのCanExecute部分を使用して、バインドされたボタンが有効になる条件を確認できます。代わりに、コマンド自体を処理する必要があると思います。

于 2010-07-26T22:01:33.027 に答える
0

ボタンを無効にするコードはありません。いくつかの方法でそれを行うことができます:

CanExecute1.イベント ハンドラを定義します。

<CommandBinding Command="local:MainWindow.MyCommand1" 
        Executed="ExecuteMyCommand" 
        CanExecute="MyCommand_CanExecute"/>

コードビハインド:

private void MyCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = tabItem1.IsSelected;
}

2. ボタンIsEnabledのプロパティをタブ項目IsSelectedのプロパティにバインドする

<Button IsEnabled="{Binding ElementName=tabItem1, Path=IsSelected}" 
        Content="MyCommand1" Command="local:MainWindow.MyCommand1" />
于 2010-08-01T17:34:29.587 に答える