Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Subclass WPF's TreeViewItem and use it in a treevview
    primarykey
    data
    text
    <p>I have created a simple dependency property that I want to attach to a treeViewitem, I have done similar things for other controls such as buttons but cannot figure out how to use the TreeViewItem in a treeview without loosing my defined style. With the code below I get "A style intended for type 'ErrorTreeViewItem' cannot be applied to type 'TreeViewItem'."</p> <pre><code>public class ErrorTreeViewItem : TreeViewItem { static ErrorTreeViewItem() { } public bool ErrorState { get { return (bool)GetValue(ErrorStateProperty); } set { base.SetValue(ErrorStateProperty, value); } } public static readonly DependencyProperty ErrorStateProperty = DependencyProperty.Register("ErrorState", typeof(bool), typeof(ErrorTreeViewItem), new UIPropertyMetadata(false)); } </code></pre> <p>The style of my tree view looks like:</p> <pre><code> &lt;Style TargetType="me:ErrorTreeViewItem"&gt; &lt;Style.Resources&gt; ... &lt;/Style.Resources&gt; &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="TreeViewItem"&gt; ... &lt;/Setter.Value&gt; &lt;/Setter&gt; </code></pre> <p>I am using it like:</p> <pre><code> &lt;TreeView Name="ApplicationTree" ItemsSource="{Binding Applications}" HorizontalContentAlignment="Stretch" Background="#E8E8E8" &gt; &lt;TreeView.ItemContainerStyle&gt; &lt;Style TargetType="me:ErrorTreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}"&gt; &lt;Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /&gt; &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /&gt; &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt; &lt;/Style&gt; &lt;/TreeView.ItemContainerStyle&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload