Wpf ToggleButton 命令绑定数据触发器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22258619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:09:22  来源:igfitidea点击:

Wpf ToggleButton Command Binding Data Trigger

wpfdata-bindingdatatrigger

提问by jfin3204

I have a toggle button that has two data triggers that are bound to the buttons IsChecked property. Within the Data triggers I am setting the buttons content and also its command property. The problem that I am having is that when you click the button the data trigger is changing the command value and then firing the command. Is it possible to have the command fire first then the data trigger? Here is the code.

我有一个切换按钮,它有两个绑定到按钮 IsChecked 属性的数据触发器。在数据触发器中,我正在设置按钮内容及其命令属性。我遇到的问题是,当您单击按钮时,数据触发器正在更改命令值,然后触发命令。是否可以先触发命令然后触发数据?这是代码。

 <ToggleButton x:Name="commandToggleButton" Grid.Row="0"  Height="30" HorizontalAlignment="Left" Margin="26,24,0,0"  VerticalAlignment="Top" Width="75">
                <ToggleButton.Style>
                    <Style TargetType="ToggleButton">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=commandToggleButton, Path=IsChecked}" Value="False">
                                <Setter Property="Content" Value="Build" />
                                <Setter Property="FontWeight" Value="Bold" />
                                <Setter Property="Command" Value="{Binding Build}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=commandToggleButton, Path=IsChecked}" Value="True">
                                <Setter Property="Content" Value="Cancel" />
                                <Setter Property="FontWeight" Value="Bold" />
                                <Setter Property="Command" Value="{Binding CancelBuild}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ToggleButton.Style>
            </ToggleButton>

What I would like to happen is that when the button is clicked the first time for the Build command to be fired, then if it is clicked the second time for the CancelBuild command to be fired.

我想要发生的是,当第一次单击按钮以触发 Build 命令时,如果第二次单击它以触发 CancelBuild 命令。

回答by Federico Berasategui

Keep it simple:

把事情简单化:

<ToggleButton IsChecked="{Binding EnableBuild}" FontWeight="Bold">
   <ToggleButton.Style>
        <Style TargetType="ToggleButton">
            <Style.Triggers>
               <Trigger Property="IsChecked" Value="False">
                   <Setter Property="Content" Value="Build" />
               </Trigger>

               <Trigger Property="IsChecked" Value="True">
                  <Setter Property="Content" Value="Cancel" />
               </Trigger>
            </Style.Triggers>
         </Style>
   </ToggleButton.Style>
</ToggleButton>

ViewModel:

视图模型:

public Command Build {get;set;}
public Command Cancel {get;set;}

//...

private bool _enableBuild;
public bool EnableBuild
{
    get { return _enableBuild; }
    set
    {
       _enableBuild = value;
       NotifyPropertyChange(() => EnableBuild);

       if (value)
          Build.Execute();
       else
          Cancel.Execute();
    }
}