locked
reference grouping controls of ItemsControl RRS feed

  • Question

  • I have an ItemControl with a ListCollectionView as a data source. The ItemControl uses Expander controls to group items. When I do ListCollectionView.Refresh(), then expanded Expander controls get collapsed. How to leave the expanded controls expanded?

    <Window x:Class="GroupingWithExpander.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:GroupingWithExpander="clr-namespace:GroupingWithExpander" Title="MainWindow" Height="350" Width="525">
        <StackPanel Orientation="Vertical">
            <ItemsControl HorizontalAlignment="Stretch" ItemsSource="{Binding}" Name="ItemsControl1">
                <ItemsControl.Resources>
                    <DataTemplate DataType="{x:Type GroupingWithExpander:DataItem}">
                        <TextBlock Text="{Binding Text}" />
                    </DataTemplate>
                </ItemsControl.Resources>
                <ItemsControl.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="GroupItem">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="GroupItem">
                                            <Expander BorderThickness="1" BorderBrush="Gray" >
                                                <Expander.Resources>
                                                    <GroupingWithExpander:DataGroupToNameConverter x:Key="DataGroupToNameConverter" />
                                                </Expander.Resources>
                                                <Expander.Header>
                                                    <TextBlock Text="{Binding Name, Converter={StaticResource DataGroupToNameConverter}}" />
                                                </Expander.Header>
                                                <StackPanel Orientation="Vertical">
                                                    <ItemsPresenter Margin="5 0" />
                                                </StackPanel>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ItemsControl.GroupStyle>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Vertical" IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
            <TextBox Name="TextBox1" />
            <Button Click="Button_Click">
                <Button.Content>
                    <TextBlock Text="do something" />
                </Button.Content>
            </Button>
        </StackPanel>
    </Window>

     public partial class MainWindow : Window{
    public MainWindow()
                {
                    InitializeComponent();
                    var group1 = new DataGroup {IsExpanded = false, Name = "group #1"};
                    var group2 = new DataGroup {IsExpanded = true, Name = "group #2"};
                    const int itemsCount = 6;
                    DataItem[] dataItems = new DataItem[itemsCount];
                    for (int i = 0; i < itemsCount; i++)
                    {
                        DataItem item = new DataItem
                                            {
                                                Group = (i%2 == 0 ? group1 : group2),
                                                Text = System.IO.Path.GetRandomFileName()
                                            };
                        dataItems[i] = item;
                    }
                    ListCollectionView v = new ListCollectionView(dataItems);            
                    v.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
                    v.Filter = FilterDataItem;
                    this.DataContext = v;            
                }
    
                private bool FilterDataItem(object o)
                {
                    DataItem item = o as DataItem;
                    return item.Contains(this.TextBox1.Text);            
                }
    
                private void Button_Click(object sender, RoutedEventArgs e)
                {
                    ListCollectionView v = (ListCollectionView) this.DataContext;
                    v.Refresh();
                }
        }
    
        class DataGroup : IEquatable<datagroup>, INotifyPropertyChanged
    {
        public string Name { get; set; }
    
        private bool _isExpanded;
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set 
            { 
                _isExpanded = value; 
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("IsExpanded"));
            }
        }
    
        public bool Equals(DataGroup other)
        {
            return other != null && this.Name == other.Name;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    
        class DataItem
    {
        public string Text { get; set; }
        public DataGroup Group { get; set; }
    
        public virtual bool Contains(string filterString)
        {
            return Text != null && (string.IsNullOrEmpty(filterString) || Text.Contains(filterString));
        }
    }
    
    
    
    
        class DataGroupToNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is DataGroup)) throw new ArgumentException("type DataGroup is expected", "value");
            DataGroup g = (DataGroup) value;
            return g.Name;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    </datagroup>

    Thursday, December 6, 2012 2:38 PM

Answers

  • Hello,

    Change your code of your property "IsExpanded"

    Try this :

    public bool IsExpanded
            {
                get { return _isExpanded; }
                set { _isExpanded = value; RaisePropertyChanged("IsExpanded"); }
            }

    let me know if it works.


    « To create a market we have to invent a problem and then find its solution »


    • Edited by Nestor Aimé Thursday, December 6, 2012 6:19 PM
    • Marked as answer by Sheldon _Xiao Friday, December 21, 2012 5:23 AM
    Thursday, December 6, 2012 6:15 PM

All replies

  • Hello,

    Change your code of your property "IsExpanded"

    Try this :

    public bool IsExpanded
            {
                get { return _isExpanded; }
                set { _isExpanded = value; RaisePropertyChanged("IsExpanded"); }
            }

    let me know if it works.


    « To create a market we have to invent a problem and then find its solution »


    • Edited by Nestor Aimé Thursday, December 6, 2012 6:19 PM
    • Marked as answer by Sheldon _Xiao Friday, December 21, 2012 5:23 AM
    Thursday, December 6, 2012 6:15 PM
  • Hi Bogdan Verbenets,
     
    I am marking your issue as "Answered", if you have new findings about your issue, please let me know.


    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 21, 2012 5:23 AM