C# 绑定到 DataContext 的 WPF 样式 DataTrigger 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19319215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Style DataTrigger with binding to DataContext not working
提问by moggizx
I have a TextBox with a style that has a DataTrigger which changes the text, like this:
我有一个带有 DataTrigger 样式的 TextBox,它可以更改文本,如下所示:
<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.
但它不起作用,文本永远不会更改为“Bar”。我已经使用另一个带有 Text="{Binding MyBool}" 的 TextBlock 进行了测试,并且此文本从“False”变为“True”。Snoop 没有显示我可以看到的任何错误,并且输出中没有任何内容。
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...
这个问题似乎是WPF Trigger binding to MVVM property的副本,但我的代码似乎与那里接受的答案没有什么不同(http://www.thejoyofcode.com/Help_Why_cant_I_use_DataTriggers_with_controls_in_WPF.aspx,“使用样式”部分)任何相关的方式。并且按照实际答案中的建议使用 DataTemplate 似乎是错误的,因为我只想将其应用于单个 TextBlock,但如果它是正确的,我不确定如何为此编写 DataTemplate...
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;
采纳答案by Sheridan
Dependency Properties can be set from many different places; inline, animations, coercion, triggers, etc. As such a Dependency Property Value Precedencelist 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:
可以从许多不同的地方设置依赖属性;内联、动画、强制、触发器等。因此,创建了一个依赖属性值优先级列表,这决定了哪些更改会覆盖哪些其他更改。由于这种优先顺序,我们不能使用 aTrigger
来更新在 XAML 中显式设置的内联属性。试试这个:
<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>