Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

c# - WPF Style DataTrigger with binding to DataContext not working

I have a TextBox with a style that has a DataTrigger which changes the text, like this:

<Grid>
    <TextBlock Text="Foo">
        <TextBlock.Style>
            <Style BasedOn="{StaticResource TextStyle}" TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding MyBool}" Value="True">
                        <Setter Property="Text" Value="Bar"/>
                    </DataTrigger>
                 </Style.Triggers>
             </Style>
         </TextBlock.Style>
     </TextBlock>
</Grid>

But it's not working, the text never changes to "Bar". I have tested using another TextBlock with Text="{Binding MyBool}" and this text changes from "False" to "True". Snoop reveals no errors that I can see and there is nothing in the output.

This question may seem like a duplicate of WPF Trigger binding to MVVM property, but my code does not seem different from the accepted answer there (http://www.thejoyofcode.com/Help_Why_cant_I_use_DataTriggers_with_controls_in_WPF.aspx, section "Using a style") in any relevant way. And using a DataTemplate as suggested in the actual answer seems wrong since I only want this to apply to a single TextBlock, but if it is correct, I'm not sure how to write a DataTemplate for this...

EDIT:

This is what the property I'm binding to looks like:

public bool MyBool
{
    get { return _myBool; }
    set
    {
        if (_myBool== value)
            return;

        _myBool= value;
        NotifyPropertyChanged();
    }
}
private bool _myBool;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Dependency Properties can be set from many different places; inline, animations, coercion, triggers, etc. As such a Dependency Property Value Precedence list was created and this dictates which changes override which other changes. Because of this order of precedence, we can't use a Trigger to update a property that is explicitly set inline in your XAML. Try this instead:

<Grid>
    <TextBlock>
        <TextBlock.Style>
            <Style BasedOn="{StaticResource TextStyle}" TargetType="TextBlock">
                <!-- define your default value here -->
                <Setter Property="Text" Value="Foo" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding MyBool}" Value="True">
                        <!-- define your triggered value here -->
                        <Setter Property="Text" Value="Bar" />
                    </DataTrigger>
                 </Style.Triggers>
             </Style>
         </TextBlock.Style>
     </TextBlock>
</Grid>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...