开发者

Why can't I use a DataTrigger to set TextBox.IsEnabled = True?

开发者 https://www.devze.com 2023-03-09 18:43 出处:网络
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 \"

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>
                &开发者_开发百科lt;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>

0

精彩评论

暂无评论...
验证码 换一张
取 消