根据其他控件中的属性更改属性 - WPF

发布于 2024-08-24 20:35:52 字数 187 浏览 3 评论 0原文

当我的文本框的文本框属性 Validation.HasErrorsTrue 时,我想使用某种触发器将窗口中的按钮更改为 IsEnabled = False代码>.

我可以使用按钮上的某种触发器来执行此操作吗?

如果是的话,一些例子会很好!

I want to use some kind of trigger to change a button in my window to IsEnabled = False when the textbox property Validation.HasErrors of my textbox is True.

Can I do this with Triggers of some kind on the Button?

If yes, some example would be nice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2024-08-31 20:35:57

是的,您可以使用触发器来执行此操作,但因为您引用的是另一个元素,所以您必须使用 DataTrigger 而不是普通触发器:

<Button>
  <Button.Style>
    <Style TargetType="Button">
      <Style.Triggers>
        <DataTrigger Binding="{Binding (Validation.HasError), ElementName=tb}" Value="True">
          <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

请注意,您必须使用 Style 而不是 Button.Triggers 集合,因为 Button.Triggers 只能包含事件触发器。

Yes, you can do this with triggers, but because you are referring to another element, you must use a DataTrigger rather than a normal Trigger:

<Button>
  <Button.Style>
    <Style TargetType="Button">
      <Style.Triggers>
        <DataTrigger Binding="{Binding (Validation.HasError), ElementName=tb}" Value="True">
          <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

Note you must use a Style rather than the Button.Triggers collection, because Button.Triggers can contain only EventTriggers.

┾廆蒐ゝ 2024-08-31 20:35:56

您可以使用样式来做到这一点。如果您想将其直接绑定到文本框,您可以执行以下操作:

    <Style x:Key="DisabledOnErrors" TargetType="{x:Type Button}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding (Validation.HasErrors)}" Value="True">
                <Setter Property="IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <TextBox x:Name="myTextBox" />
    <Button Style="{StaticResource DisabledOnErrors}" 
            DataContext="{Binding ElementName=myTextBox}" />

如果按钮尚未绑定到其他属性的 DataContext,则此方法有效。在这种情况下,样式可重复用于其他按钮和文本框配对。

You can do this by using a Style. If you want to tie it directly to the text box, you could do something like this:

    <Style x:Key="DisabledOnErrors" TargetType="{x:Type Button}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding (Validation.HasErrors)}" Value="True">
                <Setter Property="IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <TextBox x:Name="myTextBox" />
    <Button Style="{StaticResource DisabledOnErrors}" 
            DataContext="{Binding ElementName=myTextBox}" />

This works if the button is not already binding to the DataContext for other properties. In this case, the Style is reusable for other button and text box pairings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文