1

我有一个绑定到我的视图模型中的选项卡集合的选项卡控件。现在有两种类型的选项卡,其中一种有一个树视图。首次创建选项卡时,树视图选择起作用。当我切换选项卡并返回到其中包含树视图的选项卡时,树视图项目似乎变得未绑定。当我尝试选择一个时,我收到以下错误:

System.Windows.Data Error: 8 : Cannot save value from target back to source. 
BindingExpression:Path=IsSelected; DataItem='NavigationItem' (HashCode=50956576);
target element is 'TreeViewItem' (Name=''); target property is 'IsSelected' (type
'Boolean') NullReferenceException:'System.NullReferenceException: 
Object reference not set to an instance of an object.
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(
Object component, Object  value)
at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value)
at MS.Internal.Data.ClrBindingWorker.UpdateValue(Object value)
at System.Windows.Data.BindingExpression.UpdateSource(Object value)'

我不确定为什么会这样。通过应用程序进行调试时,我的模型仍然具有所有正确的数据,并且树视图甚至显示来自模型的信息,这意味着在最初更改选项卡时它必须已正确绑定。只是当我在离开选项卡并返回后尝试选择一个项目时。这是我的选项卡控件和有问题的选项卡的源代码,其中包含树视图:

<TabControl ItemsSource="{Binding Tabs}">
        <TabControl.ItemTemplate>
            ...
        </TabControl.ItemTemplate>
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
            </Style>
        </TabControl.ItemContainerStyle>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ContentControl Content="{Binding}">
                    <ContentControl.Resources>
                        <DataTemplate DataType="{x:Type tabs:ArchitectureTabViewModel}">
                            <views:ArchitectureTab/>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type tabs:TestOrderTabViewModel}">
                            <views:TestOrderTab/>
                        </DataTemplate>
                    </ContentControl.Resources>
                </ContentControl>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

和有问题的标签:

<Grid DataContext="{Binding GraphNavigationModel}">
    ...
        <TreeView ItemsSource="{Binding NavigationTree}" Background="#00ffffff" Margin="5" BorderThickness="0" MinWidth="200">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding Text}">
                    </TextBlock>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=OneWayToSource}"/>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
    </Grid>
</Grid>
4

0 回答 0