Wpf 绑定按钮已启用到TextBox.CanUndo

Wpf 绑定按钮已启用到TextBox.CanUndo,wpf,data-binding,Wpf,Data Binding,我正在尝试将一个按钮绑定到textbox的CanUndo属性,但无法使其工作。我试过像这样直接装订 IsEnabled="{Binding CanUndo, ElementName=txtDocument}" 但那没用。即使在文本框中键入将CanUndo属性更改为true的文本框后,按钮仍保持禁用状态 我也试过了 <Button IsEnabled="False" > <Button.Resources> <Style TargetTyp

我正在尝试将一个按钮绑定到textbox的CanUndo属性,但无法使其工作。我试过像这样直接装订

IsEnabled="{Binding CanUndo, ElementName=txtDocument}"
但那没用。即使在文本框中键入将CanUndo属性更改为true的文本框后,按钮仍保持禁用状态

我也试过了

<Button IsEnabled="False" >
    <Button.Resources>
         <Style TargetType="Button">
               <Style.Triggers>
                      <DataTrigger Binding="{Binding CanUndo, ElementName=txtDocument}" Value="True">
                          <Setter Property="IsEnabled" Value="True" />
                      </DataTrigger>

               </Style.Triggers>
        </Style>
    </Button.Resources>
    <Image Source="/Images/32/undo.png" />
</Button>

我还尝试使用两个单独的DataTrigger,一个用于启用的true,一个用于启用的false,但仍然不起作用。我错过了一些基本的东西吗?比如,这个属性可能不会引发绑定更改所需的事件


谢谢

将按钮的command属性设置为command,并将CommandTarget属性指定为文本框

该按钮将基于ApplicationCommand.Undo命令CanExecute状态设置IsEnabled属性。CanExecute为false时,该按钮将被禁用,CanExecute为true时,该按钮将被启用

<StackPanel>
  <TextBox x:Name="txtDocument">Test
  </TextBox>
  <Button Command="ApplicationCommands.Undo" CommandTarget="{Binding ElementName=txtDocument}" Content="Undo"/>
</StackPanel>

试验

哇,太棒了!工作非常完美,我不必添加自己的事件或命令。干杯