简体   繁体   English

根据扩展属性值设置扩展TreeViewItem的样式

[英]Set style of extended TreeViewItem based on an extended property value

I have extended the TreeViewItem class to allow me to store extra data within a tree view item. 我扩展了TreeViewItem类,允许我在树视图项中存储额外的数据。 I would like to be able to set the style of the treeview item based on the value of one of the extended properties I have added. 我希望能够根据我添加的扩展属性之一的值设置treeview项的样式。

So far I have: 到目前为止,我有:

namespace GX3GUIControls
{
    public class GX3TreeViewItem : TreeViewItem
    {

        public bool Archived { get; set; }
        public object Value { get; set; }
    }
}

<src:GX3ClientPlugin.Resources>
        <Style TargetType="{x:Type Controls:GX3TreeViewItem}">
            <Style.Triggers>
                <DataTrigger Archived="True">
                    <Setter Property="Background" Value="Gray" />
                    <Setter Property="FontStyle" Value="Italic" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </src:GX3ClientPlugin.Resources>

But I get the error - Error 1 The property 'Archived' was not found in type 'DataTrigger 但我收到错误 - 错误1在'DataTrigger类型中找不到属性'已存档'

DataTrigger has no Archived property, but you can bind your Achived-property to it via the Binding property like so <DataTrigger Binding="{Binding Path=Archived}" Value="True"> DataTrigger没有Archived属性,但您可以通过Binding属性将<DataTrigger Binding="{Binding Path=Archived}" Value="True">属性绑定到它,如此<DataTrigger Binding="{Binding Path=Archived}" Value="True">

To notify your view if the Achived property changes, you could either: 要通知您的观点,Achived属性是否发生变化,您可以:

1.Implement the INotifyPropertyChanged Interface in your GX3TreeViewItem -class: public class GX3TreeViewItem : TreeViewItem, INotifyPropertyChanged , create a method which raises the PropertyChanged Event: 1.在GX3TreeViewItem -class中实现INotifyPropertyChanged接口: public class GX3TreeViewItem : TreeViewItem, INotifyPropertyChanged ,创建一个引发PropertyChanged事件的方法:

private void PropertyChanged(string prop)
{
   if( PropertyChanged != null )
   {
      PropertyChanged(this, new PropertyChangedEventArgs(prop);
   }
}

and place this method in the setter of your property: 并将此方法放在属性的setter中:

private bool _achived;
public bool Achived
{
   get
   {
      return _achived;
   }
   set
   {
      _achived = value;
      PropertyChanged("Achived");
   }
}

2.Or make your property a DependencyProperty . 2.或者让你的属性成为DependencyProperty

Honestly it seems like you're doing it wrong. 老实说,你似乎做错了。 Those properties should be on your data. 这些属性应该在您的数据上。

You can do something like this, 你可以这样做,

Style="{Binding Path=Archived, Converter={StaticResource GetStyle}}"

GetStyle is an IValueConverter, no need to extend TreeView imo. GetStyle是一个IValueConverter,无需扩展TreeView imo。

This is not the correct way to implement this. 这不是实现此目的的正确方法。 you should take a look at the MVVM Pattern . 你应该看看MVVM模式

Your UI is not the proper place to "store extra data". 您的UI不是“存储额外数据”的适当位置。 UI is UI and data is data. UI是UI,数据是数据。 This is the worst mistake done by people coming from a winforms or otherwise non-WPF background, using a wrong approach and a wrong mindset in WPF. 这是来自winforms或其他非WPF背景的人所犯的最大错误,在WPF中使用错误的方法和错误的心态。

This will either not work (because the ItemContainerGenerator of the TreeView knows nothing about your class, or require extra work in overriding the default behavior of such class. 这将无效(因为TreeViewItemContainerGenerator对您的类一无所知,或者需要额外的工作来覆盖此类的默认行为。

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

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