故障设置在WPF一个DataTrigger [英] Trouble setting a DataTrigger in WPF

查看:388
本文介绍了故障设置在WPF一个DataTrigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框按钮在我的主要看法,我想一个样式应用到按钮,这样当组合框索引设置为1,该按钮变为可见(最初它是隐藏的)。这是我的XAML代码:

I have a ComboBox and a Button on my main view, and I want to apply a style to the button such that when the combobox index is set to 1, the button becomes visible (initially it's hidden). This is my XAML code:

<Grid>
    <StackPanel Orientation="Vertical" Margin="10">
        <ComboBox Name="comboBox"/>

        <Button Name="myBtn" Content="Hello" Visibility="Hidden">
             <Button.Style>
                 <Style TargetType="{x:Type Button}">
                     <Style.Triggers>
                         <DataTrigger Binding="{Binding ElementName=comboBox, Path=SelectedIndex}" Value="1">
                             <Setter Property="Visibility" Value="Visible"/>
                          </DataTrigger>
                      </Style.Triggers>
                  </Style>
              </Button.Style>
         </Button>
     </StackPanel>
</Grid>



有人已经问了一个关于这个问题的此处,和我做几乎同样的事情,但它不工作时,按钮保持隐藏即使索引更改为1。comobox是最初在代码中填入背后有2项。任何帮助表示赞赏。

Someone already asked a question about this here, and I'm doing pretty much the same thing, but it doesn't work, the button remains hidden even when the index is changed to 1. The comobox is initially being populated in the code behind with 2 items. Any help is appreciated.

推荐答案

问题是本地设置的依赖属性值(就像你能见度完成)有更高的优先级则这些从样式触发器设置。因此,即使当触发被击中,也不会覆盖您已设置的值。

The problem is that dependency property values set locally (like you've done with visibility) have a higher precedence then those set from a style trigger. As such, even when the trigger is hit, it won't override the value you've already set.

最简单的解决方案是一个,而不是设置的默认值风格二传手

The simple solution is to instead set the default value in a style Setter:

    <Button Name="myBtn" Content="Hello">
         <Button.Style>
             <Style TargetType="{x:Type Button}">
                 <Setter Property="Visibility" Value="Hidden"/>
                 <Style.Triggers>
                     <DataTrigger Binding="{Binding ElementName=comboBox, Path=SelectedIndex}" Value="1">
                         <Setter Property="Visibility" Value="Visible"/>
                      </DataTrigger>
                  </Style.Triggers>
              </Style>
          </Button.Style>
     </Button>



现在你的触发器将覆盖在被击中的属性值。

And now your trigger will override the property value when it is hit.

当你在这,你应该看看此链接,其中列出了设置DP值的优先顺序。

While you're at it, you should have a look at this link that lists the precedence order for setting DP values.

这篇关于故障设置在WPF一个DataTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆