为什么我不能用一个DataTrigger设置TextBox.IsEnabled =真实的吗? [英] Why can't I use a DataTrigger to set TextBox.IsEnabled = True?

查看:571
本文介绍了为什么我不能用一个DataTrigger设置TextBox.IsEnabled =真实的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的申请,我有,我想根据我的datacontext枚举启用/禁用一个TextBox。枚举有三个值(任何人,我,有人),我想,当值有人设置为启用文本框。我能够通过设置在反(见下文)的值来破解的溶液。然而,有人可以请解释为什么第一个解决方案没有奏效?

这不工作...

 <文本框的文本={结合ModifiedUser,UpdateSourceTrigger =的PropertyChanged}
         IsEnabled =FALSE>
    < TextBox.Style>
        <风格的TargetType ={X:TextBox类型}>
            < Style.Triggers>
                < D​​ataTrigger绑定={结合ModifiedBy}
                             值=有人>
                    < setter属性=IsEnabled
                            值=真/>
                < / DataTrigger>
            < /Style.Triggers>
        < /样式和GT;
    < /TextBox.Style>
< /文本框>

奇怪的是,这个code 确实的工作。

 <文本框的文本={结合ModifiedUser,UpdateSourceTrigger =的PropertyChanged}>
    < TextBox.Style>
        <风格的TargetType ={X:TextBox类型}>
            < Style.Triggers>
                < D​​ataTrigger绑定={结合ModifiedBy}
                             值=任何人>
                    < setter属性=IsEnabled
                            值=FALSE/>
                < / DataTrigger>
                < D​​ataTrigger绑定={结合ModifiedBy}
                             VALUE =我>
                    < setter属性=IsEnabled
                            值=FALSE/>
                < / DataTrigger>
            < /Style.Triggers>
        < /样式和GT;
    < /TextBox.Style>
< /文本框>


解决方案

您必须设置初始isEnabled在你的风格了。否则你的本地IsEnabled = FALSE将永远是赢家!

改变你的风格,它会正常工作。

 <文本框的文本={结合ModifiedUser,UpdateSourceTrigger =的PropertyChanged}>
< TextBox.Style>
    <风格的TargetType ={X:TextBox类型}>
        < setter属性=IsEnabledVALUE =FALSE/>
        < Style.Triggers>
            < D​​ataTrigger绑定={结合ModifiedBy}
                         值=有人>
                < setter属性=IsEnabled
                        值=真/>
            < / DataTrigger>
        < /Style.Triggers>
    < /样式和GT;
< /TextBox.Style>

In my application, I have a TextBox that I want to enable/disable based on an enum in my datacontext. The enum has three values (Anyone, Me, Someone) and I want to enable the Textbox when the value "Someone" is set. I am able to hack a solution by setting the value in reverse (see below). However, can someone please explain why the first solution didn't work?

This does not work...

<TextBox Text="{Binding ModifiedUser, UpdateSourceTrigger=PropertyChanged}"
         IsEnabled="False">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ModifiedBy}"
                             Value="Someone">
                    <Setter Property="IsEnabled"
                            Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Strangely, this code does work.

<TextBox Text="{Binding ModifiedUser, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ModifiedBy}"
                             Value="Anyone">
                    <Setter Property="IsEnabled"
                            Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ModifiedBy}"
                             Value="Me">
                    <Setter Property="IsEnabled"
                            Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

解决方案

you have to set the initial isEnabled in your style too. otherwise your "local" IsEnabled=false will always win!

change your style and it will work.

<TextBox Text="{Binding ModifiedUser, UpdateSourceTrigger=PropertyChanged}">
<TextBox.Style>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="IsEnabled" Value="False" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding ModifiedBy}"
                         Value="Someone">
                <Setter Property="IsEnabled"
                        Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBox.Style>

这篇关于为什么我不能用一个DataTrigger设置TextBox.IsEnabled =真实的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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