Tuesday, August 28, 2012

Updating ListBox from a property of selected item in another ListBox on property change

Updating ListBox from a property of selected item in another ListBox on property change

Hello and thanks in advance for the assistance. I apologize if this is an easy question and there is something I am missing, but I have looked and been unable to find a solution.

I have two listboxes, one which binds to a List of objects(which I have called RegisterServerObject), each of which has a list of children(called AssociatedModules of type RegisterModuleObject), and then a second that binds to the list of children of the selected object in the first listbox.

their xaml looks like this:

 <StackPanel Orientation="Horizontal">                     <StackPanel Orientation="Vertical" Width="235">                         <sdk:Label Content="Servers" HorizontalAlignment="Center"></sdk:Label>                         <!--new group servers list box-->                         <ListBox x:Name="NewGroupServersLB" Height="150" Width="200" ItemsSource="{Binding}" SelectionChanged="NewGroupServersLB_SelectionChanged" >                             <ListBox.ItemTemplate>                                 <DataTemplate>                                     <TextBlock Text="{Binding Path=ServerName}"></TextBlock>                                 </DataTemplate>                             </ListBox.ItemTemplate>                         </ListBox>                     </StackPanel>                     <StackPanel Orientation="Vertical" Width="235">                         <sdk:Label Content="Modules" HorizontalAlignment="Center"/>                         <!--new group modules list box-->                         <ListBox x:Name="NewGroupModulesLB" Height="150" Width="200"                                   ItemsSource="{Binding Path=AssociatedModules, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DataContextChanged="NewGroupModulesLB_DataContextChanged_1">                             <ListBox.ItemTemplate>                                 <DataTemplate>                                     <StackPanel Orientation="Horizontal">                                         <TextBlock Text="{Binding Path=ModName,Mode=TwoWay}"></TextBlock>                                         <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"></CheckBox>                                     </StackPanel>                                 </DataTemplate>                             </ListBox.ItemTemplate>                         </ListBox>                     </StackPanel> 

I have a textbox associated with a button, and on the click event of that button, I instantiate a new RegisterModuleObject with its ModName property set to the text of the textbox, and then I add it to the children list of the selected RegisterServerObject in the first listbox. That event looks like this:

private void AddCustomModuleButton_Click_2(object sender, RoutedEventArgs e)         {               //if the custom module text box is not empty             if(CustomModuleTextBox.Text != String.Empty)             {                 //create a new module object with the text as mod name                 RegisterModuleObject tempMod = new RegisterModuleObject();                 RegisterServerObject tempServer = NewGroupServersLB.SelectedItem as RegisterServerObject;                  /*after the values are set this could just as easily be serialized to xml                  for insertion to the master xml doc like jae was talking about*/                 tempMod.ModName = CustomModuleTextBox.Text;                 tempMod.ServerId = tempServer.ServerID;                  //here we would write the module to the xml documennt as opposed to adding it to the                  //server's associated modules list                 tempServer.AssociatedModules.Add(tempMod);                   //refresh the modules list?              }                         } 

I need the listbox showing the modules(the second listbox) to update on the screen as soon as this event finishes. I thought having the UpdateSourceTrigger=PropertyChanged would do the trick, but it does not seem to be working. Currently, I know the list of modules for the selected server is being updated, because if the user clicks another server in the list changing the selection, and then clicks back to the original server, the added custom module then appears in the listbox along with the others. However, I need it to update immediately on screen. Is there anyway to achieve this without drastically refactoring? I have looked for a way to manually fire the DataContextChanged event for the second listbox, because I thought I could just reset its DataContext in that event, refreshing the display, but I have been unsuccessful in that foray. Any help is greatly appreciated, thanks again!

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog