C# 将(依赖项)属性链接到嵌套控件

C# 将(依赖项)属性链接到嵌套控件,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我有一个ListBox,其中包含另一个ListBox的GroupStyle。 现在,我想根据父列表框的组名筛选嵌套列表框的项 在下面的代码中,我尝试将GroupItem.Name.Name属性链接到嵌套列表框的ViewModel的GroupName属性,但效果不是很好 本质上,GroupNameIn属性由GroupItems的名称(文本块文本)填充,然后将GroupNameOut属性设置为PropertyChangedCallback中的相同值。但问题是GroupNameOut绑定到的neste

我有一个
ListBox
,其中包含另一个ListBox的
GroupStyle
。 现在,我想根据父列表框的组名筛选嵌套列表框的项

在下面的代码中,我尝试将
GroupItem.Name.Name
属性链接到嵌套列表框的ViewModel的
GroupName
属性,但效果不是很好

本质上,
GroupNameIn
属性由GroupItems的名称(文本块文本)填充,然后将
GroupNameOut
属性设置为
PropertyChangedCallback
中的相同值。但问题是
GroupNameOut
绑定到的
nestedemsviewmodel
GroupName
属性没有更新

我的方法中是否存在一些错误,或者是否有更简单/更好的方法来实现这种行为

如果有人能给我指出正确的方向,我将非常感激

父列表框的GroupStyle:

绑定到嵌套列表框的ViewModel:

公共类nestedemsviewmodel:ViewModelBase
{
私有字符串_groupName;
公共可观测集合NestedItems{get;set;}
公共字符串组名
{
得到
{
返回_groupName;
}
设置
{
_组名=值;
OnPropertyChanged(()=>GroupName);
}
}
公共nestedemsviewmodel()
{
NestedItems=新的ObservableCollection();
}
}

您正在通过RelativeSource将内部ListBox的DataContext绑定到父ListBox的属性“NestedItemsDataContext”。但是ListBox不包含具有该名称的属性,声明的“NestedItemsDataContext”在哪里?它被声明为父ListBox的依赖属性。
<Style x:Key="MyListBoxGroupStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <StackPanel Name="container" Width="Auto" Orientation="Vertical">
                    <TextBlock Name="groupNameTextBlock" Text="{Binding Path=Name.Name}"/>

                    <ItemsPresenter/>

                    <MyNestedListBox  
                          DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Path=NestedItemsDataContext}"
                          ItemsSource="{Binding NestedItems}"
                          GroupNameIn="{Binding ElementName=groupNameTextBlock, Path=Text}"
                          GroupNameOut="{Binding Path=GroupName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
public class MyNestedListBox : ListBox
{
    static MyNestedListBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyNestedListBox), new FrameworkPropertyMetadata(typeof(MyNestedListBox)));
    }

    public string GroupNameIn
    {
        get { return (string)GetValue(GroupNameInProperty); }
        set { SetValue(GroupNameInProperty, value); }
    }
    public string GroupNameOut
    {
        get { return (string)GetValue(GroupNameOutProperty); }
        set { SetValue(GroupNameOutProperty, value); }
    }

    // DepenencyProperties
    public static readonly DependencyProperty GroupNameInProperty =
        DependencyProperty.Register("GroupNameIn", typeof(string), typeof(MyNestedListBox), new UIPropertyMetadata(null) { PropertyChangedCallback = (obj, target) => 
            {
                obj.SetValue(GroupNameOutProperty, target.NewValue);
            } 
        });

    public static readonly DependencyProperty GroupNameOutProperty =
        DependencyProperty.Register("GroupNameOut", typeof(string), typeof(MyNestedListBox), new UIPropertyMetadata(null));
}
public class NestedItemsViewModel : ViewModelBase
{
    private string _groupName;

    public ObservableCollection<NestedItem> NestedItems { get; set; }
    public string GroupName 
    {
        get 
        {
            return _groupName;
        }
        set
        {
            _groupName = value;
            OnPropertyChanged(() => GroupName);
        }
    }

    public NestedItemsViewModel()
    {
        NestedItems = new ObservableCollection<NestedItem>();
    }
}