首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定到ItemContainerStyle中的ItemContainerStyle属性以便在触发器中使用

绑定到ItemContainerStyle中的ItemContainerStyle属性以便在触发器中使用
EN

Stack Overflow用户
提问于 2016-10-10 10:52:10
回答 1查看 828关注 0票数 0

我有一个TreeView,它的DataContext是我的ViewModel。ViewModel有一个属性MyProp。在TreeView中,我用一个DataTrigger定义了一个ItemContainerStyle:

代码语言:javascript
复制
<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Style.Triggers>
            <DataTrigger Binding={MyProp} Value="...">
            <!-- does not work because DataContext is the Item, not the ViewModel -->
                <Setter Property="Focusable" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TreeView.ItemContainerStyle>

但是,由于ItemContainerStyle有一个项作为它的DataContext而不是ViewModel,所以DataTrigger中的绑定不起作用。那么,我如何告诉绑定,我想绑定到ViewModel.MyProp?似乎我不能在样式和触发器中设置DataContext,那么我还能怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-10 11:01:46

你有没有想过你的亲戚:

代码语言:javascript
复制
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MyProp}" Value="  ">
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39956616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档