基于视图模型属性更新的 Silverlight 行为

发布于 2024-08-13 01:39:00 字数 985 浏览 2 评论 0原文

我希望有一个 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(或更复杂的内容)。

如何指定这个触发器?这有可能吗?

亲切的问候,

罗纳德

I'd like to have a Silverlight behavior that is triggered by a change to a property in the view model for my page. I can't figure out how to do this, however.

So, I have a very simple view model:

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.......
}

This view model is the data context for a user control that has a text block bound to SomeProperty:

<TextBlock x:Key="myTextBlock" Text="{Binding SomeProperty}" />

This all works fine. Now I'd like to attach a behavior to myTextBlock that is triggered by changes to ChangingProperty in my view model. The behavior should highlight the TextBlock, for example (or something more sophisticated).

How do I specify this trigger? Is this possible at all?

Kind regards,

Ronald

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2024-08-20 01:39:00

我对类似问题的回答此处可能会有所帮助。

以下是如何将该技术应用于您的需求的示例。

<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>

Something along the lines of my answer to a similar issue here might help.

Here is an example of how you might apply that technique to your requirement.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文