简体   繁体   English

如果datacontext是T类型的对象,如何将样式应用于treeviewitem

[英]How to apply style to treeviewitem if the datacontext is an object of Type T

Is there a way to do this, or do I have to create an IsSelectedProperty on the ViewModel and Bind to that instead? 有没有办法做到这一点,还是我必须在ViewModel上创建一个IsSelectedProperty并绑定到该对象?

I would like to be able to do something like Source={Binding RelativeAncestor ListViewItem} 我希望能够做类似Source = {Binding RelativeAncestor ListViewItem}的操作

but instead there is only this property sourcename which I can use to set triggers based off of the items in the datatemplate if I name them using x:Name 但是相反,只有此属性源名称,如果我使用x:Name命名触发器,则可以根据数据模板中的项目设置触发器

                <HierarchicalDataTemplate.Triggers>
                  <Trigger SourceName="" Property="IsSelected" Value="True">
                    <Setter TargetName="bdr" Property="Foreground" Value="White"/>
                    <Setter TargetName="bdr" Property="Foreground" Value="Red"/>
                  </Trigger>
                </HierarchicalDataTemplate.Triggers>

Looking back, I realized that yesterday I made a very confusing post. 回顾过去,我意识到昨天我发了一个非常混乱的帖子。 Also, considering how hard I find it to even interpret people's comments to my questions, I should probably give more detail. 此外,考虑到我什至难以理解人们对我的问题的评论,我可能应该提供更多细节。

I know that in wpf you can set triggers based on the control by setting a style for the target control type. 我知道在wpf中,您可以通过为目标控件类型设置样式来基于控件设置触发器。

In addition you also have data triggers that can trigger off of properties in the datacontext. 此外,您还有数据触发器,可以触发数据上下文中的属性。 What I was hoping was that there was a way to use triggers, or datatriggers to set a property when the datacontext is an object of a specific type. 我希望的是,当数据上下文是特定类型的对象时,可以使用触发器或数据触发器来设置属性。

is this possible? 这可能吗? If not, I will just accept the provided answer as applicable in my situation, but since this will require me to add a property to my viewmodel, it seemed reasonable to check if there was a way to just check item type rather than having to check the actual property. 如果不是,我将只接受所提供的答案(适用于我的情况),但是由于这将需要我在视图模型中添加一个属性,因此检查是否有一种仅检查项目类型而不是必须检查的方法似乎是合理的实际属性。

I would suggest that you bind the IsSelected property in your view model, but that's just me. 我建议您在视图模型中绑定IsSelected属性,但这就是我。

I'm not sure how complex your HierarchicalDataTemplate is, or if some items need to have their Foreground changed, and some don't; 我不确定您的HierarchicalDataTemplate有多复杂,或者某些项目是否需要更改其前景,而有些则不需要? but I'm going to assume you want to update the item that is selected throughout the entire TreeView (if that's what this is for). 但是我假设您要更新整个TreeView中选择的项目(如果这是为此目的)。

If that is the case, just add DataTrigger s to the Style of the TreeViewItem : 如果是这种情况,只需将DataTrigger添加到TreeViewItem的样式中:

<Style TargetType="TreeViewItem">
    <Setter Property="IsSelected"
            Value="{Binding IsSelected, Mode=TwoWay}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsSelected}" Value="True">
            <Setter Property="Foreground"
                    Value="Red" />
        </DataTrigger>
        <DataTrigger Binding="{Binding IsSelected}"
                     Value="False">
            <Setter Property="Foreground"
                    Value="Black" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Note that you don't need to bind the IsSelected parameter if you don't want to, its just there because it's in my code. 请注意,如果您不想绑定IsSelected参数,它就在那里,因为它在我的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM