Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 822
Nescio Avatar asked Jun 02 '11 06:06

Nescio


1 Answers

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>

like image 63
blindmeis Avatar answered Nov 12 '22 20:11

blindmeis