【问题标题】:ListBox ItemSource binding asks for Dependency property from unknown reasonListBox ItemSsource 绑定从未知原因请求依赖属性
【发布时间】:2014-06-15 14:05:59
【问题描述】:

您好,我有以下用户控件。 可以注意到在第二个 HierarchicalDataTemplate 中还包含一个 ListBox。

我希望 ListBox ItemsSource 绑定到 viewModel 中的字段,即 CurrentPropertyValues

当我尝试在主窗口或树外绑定它时,它可以工作,(只是 ItemsSource="{Binding CurrentPropertyValues}")

我尝试了各种方法使其在 HierarchicalDataTemplate 中工作(它具有不同的数据上下文),但我一直失败。

感谢您的帮助!

<UserControl x:Class="RidaDiff2.Views.LeftTreeControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:converters="clr-namespace:RidaDiff2.Converters"
         xmlns:model="clr-namespace:RidaDiff2.Model"
         xmlns:viewModel="clr-namespace:RidaDiff2.ViewModels"
         xmlns:views="clr-namespace:RidaDiff2.Views"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <converters:EnumToPicConverter x:Key="Converter" />
    <HierarchicalDataTemplate DataType="{x:Type model:TreeNode}" ItemsSource="{Binding ChildListNodes}" x:Key="NotSelected">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding Path=EntityType,Converter={StaticResource Converter}}" />
            <TextBlock Margin="5,0" Text="{Binding Name1}" />
            <!--"{Binding ItemName, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" -->
        </StackPanel>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type model:TreeNode}" ItemsSource="{Binding ChildListNodes}" x:Key="Selected">
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Path=EntityType,Converter={StaticResource Converter}}" />
                <TextBlock Margin="5,0" Text="{Binding Name1}" />
            </StackPanel>
            <ListBox DataContext="viewModel:TreeViewModel" Height="300" Width="100"  ItemsSource="{Binding Path=(viewModel:TreeViewModel.CurrentPropertyValues), Mode=TwoWay}" ></ListBox>
        </StackPanel>
    </HierarchicalDataTemplate>
</UserControl.Resources>
<views:ExtendedTreeView x:Name="Tree"  ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding RootNode}" SelectedItem_="{Binding CurrentCouple,Mode=TwoWay}">
    <views:ExtendedTreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding Path=(model:TreeNode.IsExpanded), Mode=TwoWay}" />
            <Setter Property="Background" Value="{Binding Path=(model:TreeNode.BackgroundColor), Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding Path=(model:TreeNode.IsSelected), Mode=TwoWay}"></Setter>
            <Setter Property="HeaderTemplate" Value="{StaticResource NotSelected}"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="HeaderTemplate" Value="{StaticResource Selected}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

    </views:ExtendedTreeView.ItemContainerStyle>
</views:ExtendedTreeView>

【问题讨论】:

  • CurrentPropertyValues 是哪个视图模型的一部分?附在UserControl的那个?
  • DataContext="viewModel:TreeViewModel" 只是将一个字符串分配给 ListBox 的 DataContext。与DataContext="Hello World" 相同。我猜viewmodel是TreeView(或UserControl)的DataContext;在这种情况下,您需要从 itemtemplate 进行绑定,该模板会寻找适当的祖先容器(关于视觉/逻辑树)并通过其 DataContext 属性访问视图模型。绑定应该像"{Binding DataContext.MyProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type views:ExtendedTreeView}}}}"
  • @dkozl - CurrentPropertyValuesTreeViewModel 的一部分,是的,这是UserControl 的一部分。 @elgonzo - 它不起作用......

标签: c# wpf xaml mvvm listbox


【解决方案1】:

如果CurrentPropertyValuesTreeViewModel 的属性,那么您可以使用RelativeSource 绑定并在可视化树上找到TreeView 并将ItemsSource 绑定到其DataContext 的属性:

<ListBox 
    Height="300" 
    Width="100"  
    ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}, Path=DataContext.CurrentPropertyValues}" />

【讨论】:

  • 谢谢,它有效,感谢您的回答!我会 +1,但我没有足够的声誉..
  • 没问题@matansab。很高兴它有帮助
猜你喜欢
  • 2019-12-08
  • 1970-01-01
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 2016-01-22
  • 2012-11-08
相关资源
最近更新 更多