2017-04-05 36 views
1

我有依賴屬性選擇的項目的用戶控件(這是枚舉值的集合)轉換器沒有得到在運行時調用

public IEnumerable SelectedItems 
{ 
    get { return (IEnumerable)GetValue(SelectedItemsProperty); } 
    set { SetValue(SelectedItemsProperty, value); } 
} 

public static readonly DependencyProperty SelectedItemsProperty = 
    DependencyProperty.Register("SelectedItems", typeof(IEnumerable), 
     typeof(UserControl1), new FrameworkPropertyMetadata(OnChangeSelectedItems) 
     { 
      BindsTwoWayByDefault = true 
     }); 


private static void OnChangeSelectedItems(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    var uc = d as UserControl1; 

    if (uc != null) 
    { 
     var oldObservable = e.OldValue as INotifyCollectionChanged; 
     var newObservable = e.NewValue as INotifyCollectionChanged; 
     if (oldObservable != null) 
     { 
      oldObservable.CollectionChanged -= uc.SelectedItemsContentChanged; 
     } 
     if (newObservable != null) 
     { 
      newObservable.CollectionChanged += uc.SelectedItemsContentChanged; 
      uc.UpdateMultiSelectControl(); 

     } 
    } 

} 

private void SelectedItemsContentChanged(object d, NotifyCollectionChangedEventArgs e) 
{ 
    UpdateMultiSelectControl(); 
} 

我一直在使用一個轉換器在我的用戶控件綁定selectedItems依賴屬性有一個複選框

<ItemsControl ItemsSource="{Binding Items}" > 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Vertical"> 
       <CheckBox x:Name="ChkBox" Margin="13 0 0 10" Content="{Binding}" > 
        <CheckBox.IsChecked> 
         <MultiBinding Converter="{StaticResource CheckBoxConverter}" Mode="TwoWay" > 
          <Binding ElementName="ChkBox" Path="Content" /> 
          <Binding RelativeSource="{RelativeSource FindAncestor,AncestorType={x:Type UserControl}}" Path="DataContext.SelectedItems" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay"></Binding> 
         </MultiBinding> 
        </CheckBox.IsChecked> 

       </CheckBox> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

和轉換器

public class CheckBoxConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, 
      object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (values != null && values.Length > 1) 
     { 
      var content = values[0]; 
      var selected = values[1] as IList; 

      if (selected != null && selected.Contains(content)) 
      { 
       return true; 
      } 
     } 

     return false; 

    } 
    public object[] ConvertBack(object value, Type[] targetTypes, 
      object parameter, System.Globalization.CultureInfo culture) 
    { 
     return new[] { Binding.DoNothing, Binding.DoNothing }; 
    } 
} 

我的問題如果我在構建時添加了選定項目中的值,則轉換器被調用,但如果我在按鈕上單擊添加值,則不會調用它。可以讓任何人告訴我爲什麼會發生這種情況,以及我如何糾正這一情況。

回答

2

轉換器的Convert方法只有在綁定到的任何屬性發生更改時纔會被調用。您與UserControlCheckBoxSelectedItems屬性的Content屬性綁定。將新項目添加到集合時,這些更改都不會改變。

嘗試也結合收集的Count屬性:

<CheckBox x:Name="ChkBox" Margin="13 0 0 10" Content="{Binding}" > 
    <CheckBox.IsChecked> 
     <MultiBinding Converter="{StaticResource CheckBoxConverter}" Mode="TwoWay" > 
      <Binding ElementName="ChkBox" Path="Content" /> 
      <Binding RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}" Path="SelectedItems"/> 
      <Binding RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}" Path="SelectedItems.Count"/> 
     </MultiBinding> 
    </CheckBox.IsChecked> 
</CheckBox> 
相關問題