Question

I have a TreeView that I am binding to an ItemsSource that creates a CheckBox for each item. Here is the xaml:

<TreeView x:Name="ReasonTreeView" Height="Auto" Background="Transparent"
        BorderThickness="0" IsTabStop="False"
        ItemsSource="{Binding Path=AnswerOptions}">
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="{x:Type QSB:Answer}" ItemsSource="{Binding Path=AnswerOptions}">
        <StackPanel Orientation="Horizontal">
            <CheckBox Margin="0,5"
                        IsChecked="{Binding Path=IsSelected}"
                        IsEnabled="{Binding Path=Value,
                                            Converter={StaticResource ReasonValueToEnabledConverter}}"
                        Visibility="{Binding Path=AnswerOptions,
                                            Converter={StaticResource ParentNodeVisConverter}}" />
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

In my application I then create multiple instances of these. Depending on the instance of the TreeView, certain CheckBoxes need to be disabled so the user can not select them, however I'm uncertain of how I can access the individual items in the HierarchicalDataTemplate in the code.

After looking around for a while the only thing I can think of is to build the whole TreeView in the code behind instead of the xaml, but I would rather not have to resort to that. Is there anything else that I can do?

To help clarify my point and for illustrative purposes, this is essentially what I want to be able to do (in pseudocode): ReasonTreeView.ItemsSource[5].IsEnabled = false;

Which would disable the CheckBox (and any other controls in that HierarchicalDataTemplateItem) at index 5 of the TreeView's ItemsSource

Let me know if more information is needed

Was it helpful?

Solution

I meant that binding on the checkbox's isenabled property Path=Value. That Value member has to be bool and implement INotifyPropertyChanged then you can control IsEnabled from your model. Dont forget to add Mode=Twoway to your binding

OTHER TIPS

Instead of accessing the CheckBox through Control.ItemsSource property you should make the change in your underlying collection (that is itemssource of your control). After making the change notify the View (your Control) that data has been changed so update the control.

Implement INotifyPropertyChanged in your underlying class and after changing the Property (which is responsible for Enabled/Disabled) value Notify the View.

If you are not familiar with concepts of Data Binding and INotifyPropertyChanged, I would suggest you to read some basic tutorials about it. It is one of the major feature of WPF which makes life very easy for doing things like yours

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top