mog*_*izx 17 c# wpf binding datatrigger
我有一个TextBox,其样式有一个DataTrigger,用于更改文本,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
但它不起作用,文本永远不会变为"Bar".我已经使用Text ="{Binding MyBool}"测试了另一个TextBlock,此文本从"False"变为"True".Snoop没有发现我能看到的错误,输出中没有任何内容.
这个问题可能看起来像WPF触发器绑定到MVVM属性的重复,但我的代码似乎与接受的答案不同(http://www.thejoyofcode.com/Help_Why_cant_I_use_DataTriggers_with_controls_in_WPF.aspx,"使用样式"一节)任何相关的方式.并且在实际答案中建议使用DataTemplate似乎是错误的,因为我只希望将其应用于单个TextBlock,但如果它是正确的,我不知道如何为此编写DataTemplate ...
编辑:
这就是我绑定的属性看起来像:
public bool MyBool
{
get { return _myBool; }
set
{
if (_myBool== value)
return;
_myBool= value;
NotifyPropertyChanged();
}
}
private bool _myBool;
Run Code Online (Sandbox Code Playgroud)
She*_*dan 57
您不能使用a Trigger
来更新在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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24952 次 |
最近记录: |