tags:

views:

26

answers:

1

I have a treeviewitem where the header property contains other elements such as TextBlock. I want if the TextBlock Text = "Empty", the TreeViewItem not to be focasable. Here I set the TextBox Focasable property but the containing TreeViewItem is focasable.

I want the TreeViewItem containing the TextBlock with the Text="Empty" not to be focasable. Thanks

Here is my attempt.

    <Grid>
    <TreeView>
        <TreeViewItem>
            <TreeViewItem.Header>
                <TextBlock Text="John">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                                    <Trigger Property="Text" Value="Empty">
                                    <Setter Property="Background" Value="Red" />
                                        <Setter Property="Focusable" Value="False"></Setter>
                                    </Trigger>
                        </Style.Triggers>
                    </Style>
                    </TextBlock.Style>
                </TextBlock>
            </TreeViewItem.Header>
        </TreeViewItem>
        <TreeViewItem>
            <TreeViewItem.Header>
                <TextBlock Text="Empty">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                           <Trigger Property="Text" Value="Empty">
                                    <Setter Property="Background" Value="Red" />
                                        <Setter Property="Focusable" Value="False"></Setter>
                                    </Trigger>
                        </Style.Triggers>
                    </Style>
                    </TextBlock.Style>
                </TextBlock>
            </TreeViewItem.Header>
        </TreeViewItem>
    </TreeView>
</Grid>
A: 

Your need is setting the Focusable property for TreeViewItem, not for TextBlock. This is why your item is always focusable. I suggest you to bind the property Text (of the TextBlock) and the property Focusable (of the TreeViewItem) to another property in the ViewModel class, and use a converter to convert the string into a bool for your Focusable property.

Maurizio Reginelli