0

我有一个 TreeView,我绑定到一个为每个项目ItemsSource创建一个的 TreeView。CheckBox这是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>

然后在我的应用程序中创建这些的多个实例。根据 的实例TreeView,某些CheckBoxes需要被禁用,以便用户无法选择它们,但是我不确定如何访问HierarchicalDataTemplate代码中的各个项目。

环顾了一会后,我唯一能想到的就是TreeView在代码后面而不是 xaml 中构建整体,但我宁愿不必诉诸于此。还有什么我可以做的吗?

为了帮助澄清我的观点并出于说明目的,这本质上是我想要做的(在伪代码中):ReasonTreeView.ItemsSource[5].IsEnabled = false;

这将禁用's 的索引 5 处的CheckBox(以及其中的任何其他控件)HierarchicalDataTemplateItemTreeViewItemsSource

让我知道是否需要更多信息

4

2 回答 2

1

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

于 2013-03-29T16:56:38.060 回答
0

而不是通过 Control.ItemsSource 属性访问 CheckBox,您应该在基础集合(即控件的 itemssource)中进行更改。进行更改后,通知视图(您的控件)数据已更改,因此更新控件。

在您的基础类中实现INotifyPropertyChanged,并在更改属性(负责启用/禁用)值后通知视图。

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

于 2013-03-29T16:50:48.273 回答