Hi, I have a ListBox with a GroupStyle that contains another ListBox. Now I want to filter the Items of this nested ListBox depending on the group name of the parent ListBox.
In the code below I tried to chain the "GroupItem.Name.Name" property to the "GroupName" property of the ViewModel of the nested ListBox, but this didn't work out so well.
Essentially the "GroupNameIn" Property is filled by the GroupItems' name(the TextBlock Text) and then sets the "GroupNameOut" Property to the same value in the "PropertyChangedCallback".
But when the setter of the "GroupName" Property of the "NestedItemsViewModel" gets called, the value is always null. (The NestedItemsDataContext is a DP of the parent ListBox).
Are there any mistakes in my approach or is there even a simpler/better way to achieve this behavior?
I would be very grateful if someone could point me in the right direction.
In the code below I tried to chain the "GroupItem.Name.Name" property to the "GroupName" property of the ViewModel of the nested ListBox, but this didn't work out so well.
Essentially the "GroupNameIn" Property is filled by the GroupItems' name(the TextBlock Text) and then sets the "GroupNameOut" Property to the same value in the "PropertyChangedCallback".
But when the setter of the "GroupName" Property of the "NestedItemsViewModel" gets called, the value is always null. (The NestedItemsDataContext is a DP of the parent ListBox).
Are there any mistakes in my approach or is there even a simpler/better way to achieve this behavior?
I would be very grateful if someone could point me in the right direction.
GroupStyle of the parent 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=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"</StackPanel></ControlTemplate></Setter.Value></Setter></Style>
The nested ListBox:
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) => { MyNestedListBox lb = obj as MyNestedListBox; lb.SetValue(GroupNameOutProperty, target.NewValue); // lb.GetBindingExpression(MyNestedListBox.GroupNameOutProperty).UpdateSource(); // doesn't seem to have an effect } }); public static readonly DependencyProperty GroupNameOutProperty = DependencyProperty.Register("GroupNameOut", typeof(string), typeof(MyNestedListBox), new UIPropertyMetadata(null)); }
ViewModel bound to the nested ListBox:
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>(); } }