This is a migrated thread and some comments may be shown as answers.

Selected Item on SubListBox no update

1 Answer 129 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
figueiredorj
Top achievements
Rank 1
figueiredorj asked on 21 May 2012, 03:08 PM
Hi.

I am trying to have a listbox with many listboxes.
Detailing:

I have a "Parent" class with visibility property that performs on a sublist of "childs".
This means that I have a header item for each listbox with a checkbox binded to visiblity property.

If checked then sublist (listbox) is visible else not.

Also when I select an item on parents, his first child is selected.
When iterating with cursor I want to go between childs and collapsed parents.

I am doing a lab with it but binding with child is not being triggered....

XAML:
<UserControl x:Class="CollecttionBinding.CollectionListView"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:local="clr-namespace:CollecttionBinding" mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <UserControl.Resources>
        <DataTemplate x:Key="ChildTemplate" >
            <StackPanel >
                <TextBlock Text="{Binding Path=Designation}"></TextBlock>
            </StackPanel>
        </DataTemplate>
        <local:BoolToVisibility x:Key="boolToVisibility" />
      <DataTemplate x:Key="parentTemplate" >
            <StackPanel>
                <StackPanel Orientation="Horizontal" >
                    <CheckBox IsTabStop="False" IsChecked="{Binding Path=IsVisible, Mode=TwoWay, Converter={StaticResource Emty}}" ></CheckBox>
                    <TextBlock Text="{Binding Path=Designation}"></TextBlock>
                </StackPanel>
                <telerik:RadListBox IsTabStop="True" ItemsSource="{Binding Path=Childs}"
                                    SelectedItem="{Binding Path=SelectedC, Mode=TwoWay, Converter={StaticResource Emty}}"
                                    ItemTemplate="{StaticResource ChildTemplate}" Visibility="{Binding Path=IsVisible, Mode=TwoWay, Converter={StaticResource boolToVisibility}}">                   
                </telerik:RadListBox>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
 
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
 
        <telerik:RadListBox Grid.Column="1" ItemsSource="{Binding Path=Parents}" SelectedItem="{Binding Path=SelectedP, Mode=TwoWay}" ItemTemplate="{StaticResource parentTemplate}"></telerik:RadListBox>
    </Grid>
</UserControl>

codeBehind:
public partial class CollectionListView : UserControl, INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 
    #endregion
 
    private ObservableCollection<Parent> _parents;
    public ObservableCollection<Parent> Parents
    {
        get { return _parents; }
        set
        {
            if(_parents != value)
            {
                _parents = value;
                OnPropertyChanged("Parents");
            }
        }
    }
 
    private Parent _selectedP;
    public Parent SelectedP
    {
        get { return _selectedP; }
        set
        {
            if(_selectedP != value)
            {
                _selectedP = value;
                OnPropertyChanged("SelectedP");
                SelectedC = _selectedP.Childs.FirstOrDefault();
            }
        }
    }
 
    private Child _selectedC;
    public Child SelectedC
    {
        get { return _selectedC; }
        set
        {
            if (_selectedC != value)
            {
                _selectedC = value;
                OnPropertyChanged("SelectedC");
            }
        }
    }
 
    public CollectionListView()
    {
        InitializeComponent();
        this.DataContext = this;
        Parents = new Data().GetData();
    }
}

Datasource:
public class Data
  {
      public ObservableCollection<Parent> GetData()
      {
          return new ObservableCollection<Parent>()
                     {
                         new Parent()
                             {
                                 Code = "A",
                                 Designation = "AA",
                                 Childs = new ObservableCollection<Child>()
                                              {
                                                  new Child()
                                                      {
                                                          Code = "1",
                                                          Designation = "A.1"
                                                      },
                                                  new Child()
                                                      {
                                                          Code = "2",
                                                          Designation = "A.2"
                                                      }
                                              }
                             },
                          new Parent()
                             {
                                 Code = "B",
                                 Designation = "BB",
                                 Childs = new ObservableCollection<Child>()
                                              {
                                                  new Child()
                                                      {
                                                          Code = "1",
                                                          Designation = "B.1"
                                                      },
                                                  new Child()
                                                      {
                                                          Code = "2",
                                                          Designation = "B.2"
                                                      },
                                                  new Child()
                                                      {
                                                          Code = "3",
                                                          Designation = "B.3"
                                                      }
                                              }
                             },
                          new Parent()
                             {
                                 Code = "C",
                                 Designation = "CC",
                                 Childs = new ObservableCollection<Child>()
                                              {
                                                  new Child()
                                                      {
                                                          Code = "1",
                                                          Designation = "C.1"
                                                      },
                                                  new Child()
                                                      {
                                                          Code = "2",
                                                          Designation = "C.2"
                                                      }
                                              }
                             },
                          new Parent()
                             {
                                 Code = "D",
                                 Designation = "DD",
                                 Childs = new ObservableCollection<Child>()
                                              {
                                                  new Child()
                                                      {
                                                          Code = "1",
                                                          Designation = "D.1"
                                                      },
                                                  new Child()
                                                      {
                                                          Code = "2",
                                                          Designation = "D.2"
                                                      },
                                                  new Child()
                                                      {
                                                          Code = "3",
                                                          Designation = "D.3"
                                                      }
                                              }
                             }
 
                     };
      }
  }
 
 
  public class Child : INotifyPropertyChanged
  {
      #region INotifyPropertyChanged Members
      public event PropertyChangedEventHandler PropertyChanged;
      protected void OnPropertyChanged(string propertyName)
      {
          if (this.PropertyChanged != null)
          {
              this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
 
      #endregion
 
      private string _code;
 
      public string Code
      {
          get { return _code; }
          set
          {
              if (_code != value)
              {
                  _code = value;
                  OnPropertyChanged("Code");
              }
          }
      }
 
      private string _designation;
      public string Designation
      {
          get { return _designation; }
          set
          {
              if (_designation != value)
              {
                  _designation = value;
                  OnPropertyChanged("Value");
              }
          }
      }
  }
  public class Parent : INotifyPropertyChanged
  {
      #region INotifyPropertyChanged Members
      public event PropertyChangedEventHandler PropertyChanged;
      protected void OnPropertyChanged(string propertyName)
      {
          if (this.PropertyChanged != null)
          {
              this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
 
      #endregion
 
 
      public Parent()
      {
          IsVisible = true;
      }
 
      private string _code;
      public string Code
      {
          get { return _code; }
          set
          {
              if (_code != value)
              {
                  _code = value;
                  OnPropertyChanged("Code");
              }
          }
      }
 
      private string _designation;
      public string Designation
      {
          get { return _designation; }
          set
          {
              if (_designation != value)
              {
                  _designation = value;
                  OnPropertyChanged("Value");
              }
          }
      }
 
      private ObservableCollection<Child> _childs;
      public ObservableCollection<Child> Childs
      {
          get { return _childs; }
          set
          {
              if (_childs != value)
              {
                  _childs = value;
                  OnPropertyChanged("Childs");
              }
          }
      }
 
      private bool _isVisible;
      public bool IsVisible
      {
          get { return _isVisible; }
          set
          {
              if(_isVisible != value)
              {
                  _isVisible = value;
                  OnPropertyChanged("IsVisible");
              }
          }
      }
  }

Am I doing something wrong or can't I achieve binding to subitem this way?

Thanks in advance

1 Answer, 1 is accepted

Sort by
0
figueiredorj
Top achievements
Rank 1
answered on 22 May 2012, 07:42 PM
I have already understood what I was doing wrong... I wasn't binding children to right context. 

<telerik:RadListBox IsTabStop="True" ItemsSource="{Binding Path=Childs}"
                    SelectedItem="{Binding Element=LayoutRoot, Path=DataContext.SelectedC, Mode=TwoWay, Converter={StaticResource Emty}}"
                    ItemTemplate="{StaticResource ChildTemplate}" Visibility="{Binding Path=IsVisible, Mode=TwoWay, Converter={StaticResource boolToVisibility}}">                  
</telerik:RadListBox>
Binding it to datacontext of element does the trick.

However I was forced to abandon this initial strategy because I was unable to control scrolling with children listbox...
Tags
ListBox
Asked by
figueiredorj
Top achievements
Rank 1
Answers by
figueiredorj
Top achievements
Rank 1
Share this question
or