Search code examples
wpfmvvmtextblock

How can i bind a IsSelected Property to a textblock


my treeview is populated with textblock items. If a user clicks on a textblock, i want to set a property in my model called "isSelected". But : the textblock have no Property IsSelected. How can i Implement this ? Derive from textblock and add a Property ?


Solution

  • You have to use the TreeViewItem.IsSelected property. You will have to specify the custom style for all items of the tree view.

    <TreeView>
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
            </Style>
        </TreeView.Resources>
    </TreeView>