基于视图模型属性更新的Silverlight行为
-
05-07-2019 - |
题
我希望通过更改页面视图模型中的属性来触发Silverlight行为。但是,我无法弄清楚如何做到这一点。
所以,我有一个非常简单的视图模型:
public class MyViewModel : INotifyPropertyChanged
{
private bool changingProperty;
public bool ChangingProperty
{
get { return changingProperty; }
set
{
if (changingProperty != value)
{
changingProperty = value;
NotifyPropertyChanged("ChangingProperty");
}
}
}
public string SomeProperty { get { return "SomePropertyValue"; } }
// INotifyPropertyChanged implementation here.......
}
此视图模型是用户控件的数据上下文,该控件具有绑定到 SomeProperty
的文本块:
<TextBlock x:Key="myTextBlock" Text="{Binding SomeProperty}" />
一切正常。现在,我想将一个行为附加到 myTextBlock
,该行为是由我视图模型中对 ChangingProperty
的更改触发的。例如,行为应该突出显示 TextBlock
(或更复杂的东西)。
如何指定此触发器?这有可能吗?
亲切的问候,
罗纳德
解决方案
我对类似问题的答案有点像这里帮助
以下是如何将该技术应用于您的要求的示例。
<Grid.Resources>
<local:BoolToBrushConverter x:Key="Highlighter"
FalseBrush="Transparent" TrueBrush="Yellow" />
</Grid.Resources>
<Border Background="{Binding ChangingProperty, Converter={StaticResource Highlighter}}">
<TextBlock x:Name="txtTarget" Text="{Binding SomeProperty}" />
</Border>
不隶属于 StackOverflow