title: Multibinding Multiselection ListView
body: <p>OK I have a weird one here. What I am trying to figure out is how to have one listview, populated by an ObservableCollection, update another ListView, populated by another ObservableCollection, based on the first listview's selection and then use a valueconverter to check or uncheck a checkbox based on the selection combined with the current item in the second listview. This part I somewhat have working by using multibinding, but the part that has me stumped is when I check or uncheck an item in the second listview I need to be able to grab that event and all the currently checked items in that listview an update a database field based on that.</p>
  
  <p>I know this may not make a lot of sense and I'm struggling on how to make it clearer, but below is the xaml for the two listviews and the code for the converter. I can see that when I check or uncheck a box in the secodn listview that the converter tries to do the ConvertBack method which blows up, but if I set it to just return null then the code no longer blows up, but the checkbox is highlighted in red like a validation error has occured.</p>
  
  <p>I am not even sure that multibinding is the way to go here and I have looked at Josh Smith's multiselection listview stuff, but with the conversion needed I can not see how to implement that successfully either.   </p>
  
  <p>If anyone has any ideas I would greatly appreciate it. Also my apologies if I didn't explain my need very clearly, but I am hoping with the chaotic description and the code you can kind of see where I am going with it.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>First ListView that feeds the second one</strong></p>
  
  <pre><code>&lt;Grid&gt;
                  &lt;ListView x:Name="listRule" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="3,3,3,3" ItemsSource="{Binding RuleListing}" exts:Selected.Command="{Binding RuleSelectedCommand}" SelectedIndex="0"&gt;
                      &lt;ListView.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;TextBlock Text="{Binding Path=DisplayName}" ToolTip="{Binding Path=Expression}" FontWeight="Bold"/&gt;
                                  &lt;TextBlock Text=" ( "/&gt;
                                  &lt;TextBlock Text="{Binding Description}" FontStyle="Italic" /&gt;
                                  &lt;TextBlock Text=" )"/&gt;
                              &lt;/StackPanel&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ListView.ItemTemplate&gt;
                  &lt;/ListView&gt;
              &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>Second ListView with converter and multibinding</strong></p>
  
  <pre><code>&lt;Grid HorizontalAlignment="Stretch"&gt;
                  &lt;Grid.Resources&gt;
                      &lt;converters:RuleToRoleBooleanConverter x:Key='RuleRoleConverter' /&gt;                        
                      &lt;DataTemplate x:Key="RoleTemplate"&gt;
                          &lt;Grid HorizontalAlignment="Stretch"&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="1*" MinWidth="200"/&gt;
                                  &lt;ColumnDefinition Width="20"/&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
                              &lt;TextBlock Text="{Binding RoleName}" HorizontalAlignment="Left" Margin="3,0,0,0" Grid.Column="0" /&gt;
                              &lt;CheckBox HorizontalAlignment="Right" Margin="0,0,3,0" Grid.Column="1"&gt;
                                  &lt;CheckBox.IsChecked&gt;
                                      &lt;MultiBinding Converter="{StaticResource RuleRoleConverter}"&gt;
                                          &lt;Binding ElementName="listRule" Path="SelectedItem" /&gt;
                                          &lt;Binding Path="RoleName"/&gt;
                                      &lt;/MultiBinding&gt;
                                  &lt;/CheckBox.IsChecked&gt;
                              &lt;/CheckBox&gt;
                          &lt;/Grid&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/Grid.Resources&gt;
                  &lt;ListView Name="listRoles" ItemsSource="{Binding RoleListing}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                            SelectionMode="Multiple" ItemTemplate="{StaticResource ResourceKey=RoleTemplate}"&gt;
                      &lt;ListView.ItemContainerStyle&gt;
                          &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
                              &lt;Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsRoleSelected}"/&gt;
                          &lt;/Style&gt;
                      &lt;/ListView.ItemContainerStyle&gt;
                  &lt;/ListView&gt;
              &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>Value Converter</strong></p>
  
  <pre><code>public class RuleToRoleBooleanConverter : IMultiValueConverter
  {
      public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          if (values[0] != null &amp;&amp; values[1] != null)
          {
              string expression = ((EliteExtenderRule)values[0]).Expression;
              string role = values[1].ToString();
  
              if (expression.Contains("R:*") || expression.Contains("R:" + role))
              {
                  return true;
              }
          }
          return false;
      }
  
      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
      {
          return null;// new object[] { (bool)value, null };
      }}
  </code></pre>