为什么我不能使用DataTrigger来设置TextBox.IsEnabled = True?

时间:2020-12-14 22:49:45

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?

在我的应用程序中,我有一个TextBox,我想根据我的datacontext中的枚举启用/禁用。枚举有三个值(任何人,我,某人),我想在设置值“Someone”时启用文本框。我可以通过反向设置值来破解解决方案(见下文)。但是,有人可以解释为什么第一个解决方案不起作用?

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>

1 个解决方案

#1


14  

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

你必须在你的风格中设置初始的isEnabled。否则你的“本地”IsEnabled = false将永远胜利!

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>

#1


14  

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

你必须在你的风格中设置初始的isEnabled。否则你的“本地”IsEnabled = false将永远胜利!

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>