ViewModel 属性更改上的 WPF DataTrigger

发布于 2024-11-14 15:14:54 字数 1093 浏览 2 评论 0原文

当我的 ViewModel 中的属性发生更改时,我试图触发触发器。无论我如何尝试,我似乎都无法触发扳机。我的 XAML 看起来像这样:

<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    <Setter Property="Padding" Value="0 1" />
    <Setter Property="FontWeight" Value="Normal" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsExpanding}" Value="True">
            <Setter Property="Control.Template" Value="{StaticResource ResourceKey=loadingTreeViewItem}" />
        </DataTrigger >
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="FontWeight" Value="Bold" />                            
        </Trigger>
    </Style.Triggers>
</Style>

IsExpanded 和 IsSelected 绑定工作正常,但是当 IsExpanding 为 true 时,我无法触发 DataTrigger。这些属性位于同一个 ViewModel 中。我尝试为relativesource 添加不同的变体,但没有任何运气。如有任何反馈,我们将不胜感激。

I'm trying to fire a trigger when a property in my ViewModel changes. I can't seem to get the trigger to fire no matter what I try. My XAML looks like this:

<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    <Setter Property="Padding" Value="0 1" />
    <Setter Property="FontWeight" Value="Normal" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsExpanding}" Value="True">
            <Setter Property="Control.Template" Value="{StaticResource ResourceKey=loadingTreeViewItem}" />
        </DataTrigger >
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="FontWeight" Value="Bold" />                            
        </Trigger>
    </Style.Triggers>
</Style>

The IsExpanded and IsSelected bindings are working fine, however I can't get the DataTrigger to fire when IsExpanding is true. These properties are in the same ViewModel. I have tried adding different variations for RelativeSource but am not having any luck. Any feedback is appreciated.

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

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

发布评论

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

评论(1

与酒说心事 2024-11-21 15:14:54

事实证明触发器起作用了。问题在于 IsExpanding 属性和获取 TreeView 数据的调用都发生在 UI 线程上。我通过线程调用来获取数据,一切都按预期工作

Turns out the trigger was working. The problem was that the IsExpanding property and the call to get the data for the TreeView were both happening on the UI thread. I threaded the call to get the data and everything is working as expected

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