2009-06-04 29 views
0

私はCheckedComboBox WPFコントロールの作成に取り組んでいます。 SelectedValuesPropertyを追加して、XAML経由でバインドできるようにします。私はいくつかのことを試してきましたが、それをまだ働かせることができませんでした。誰もこれにアプローチする方法についての提案はありますか?私のコントロールはMultiSelectorを継承しています。前もって感謝します!WPFコントロールでSelectedValuesプロパティを実装する

public static readonly DependencyProperty SelectedValuesProperty = DependencyProperty.Register( 
    "SelectedValues", typeof(IEnumerable), typeof(CheckedComboBox), 
     new FrameworkPropertyMetadata((IEnumerable) null, 
     new PropertyChangedCallback(OnSelectedValuesChanged))); 

private static void OnSelectedValuesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    CheckedComboBox combo = (CheckedComboBox) d; 
    IEnumerable oldValue = (IEnumerable) e.OldValue; 
    IEnumerable newValue = (IEnumerable) e.NewValue; 

    // unselect all the old vlaues 
    if (oldValue != null) 
    { 
    foreach (object obj in oldValue) 
    { 
     CheckedComboBoxItem item = obj as CheckedComboBoxItem; 
     if (item == null) 
     item = combo.ItemContainerGenerator.ContainerFromItem(obj) as CheckedComboBoxItem; 
     if (item != null && item.IsEnabled && item.IsSelected) 
     item.IsSelected = false; 
    } 
    } 

    // select all the new values 
    if (e.NewValue != null) 
    { 
    foreach (object obj in newValue) 
    { 
     CheckedComboBoxItem item = obj as CheckedComboBoxItem; 
     if (item == null) 
     item = combo.ItemContainerGenerator.ContainerFromItem(obj) as CheckedComboBoxItem; 
     if (item != null && item.IsEnabled && !item.IsSelected) 
     item.IsSelected = true; 
    } 
    } 
} 

答えて

0

私はこの同じ問題を把握しようとしている:

これは私がこれまでのところ、問題は、私は、オブジェクトからitemcontainerを得ることができないで持っているものです。 SelectedValuePathと組み合わせて使用​​するSelectedValues(SelectedItemsではない)が必要なので、オブジェクトのCollectionを渡すと、返された値を使用するためにこれらのオブジェクトのプロパティを指定できます。 SelectedValuesは値のコレクションを返します。

関連する問題