WPF显示/隐藏带有触发器的控件 [英] WPF Showing / Hiding a control with triggers

查看:278
本文介绍了WPF显示/隐藏带有触发器的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手,我试图创建xaml逻辑来基于ViewModel上的AllowMiscTitle的值来显示/隐藏控件。 xaml由两个字段组成,当选择其他时,我希望文本框显示标准瓷砖的组合框(先生,夫人,...,其他)。

I'm new to WPF and I trying to create xaml logic to show / hide a control based on the value of the AllowMiscTitle on the ViewModel. The xaml consist of two fields a combobox of the standard tiles ("Mr", "Mrs", ..., "Other") when "Other" is selected I want the textbox to display.

我创建了以下xaml:

I've created the follow xaml:

                <DockPanel Validation.Error="Validation_Error" HorizontalAlignment="Stretch">
                <ComboBox ItemsSource="{Binding Path=Titles, Mode=OneTime}" 
                      Text="{Binding Path=Title}"/>
                <TextBox x:Name="TxtBxTitle" Margin="5,5" Visibility="Visible">
                    <TextBox.Style>
                        <Style>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=AllowMiscTitle}" Value="false">
                                    <Setter Property="TextBox.Visibility" Value="Hidden"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>

            </DockPanel>


推荐答案

触发不起作用,因为您已在 TextBox

That Trigger won't work because you have set Visibility property explicitly in TextBox

这样做:

<TextBox x:Name="TxtBxTitle" Margin="5,5">
    <TextBox.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=AllowMiscTitle}" Value="false">
                      <Setter Property="TextBox.Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

原因是相关性属性值优先级

这篇关于WPF显示/隐藏带有触发器的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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