2016-09-15 6 views
-1

のデフォルト値とNULLを交換しCustom ControlItemsControl由来。私がいるのItemsSource IEnumerableを

私は上記の質問でTwo-Way Binding Issue of Unknown Object in WPF Custom Control Dependency Property

からアイデアを得た、彼らはビューモデルでコレクションを使用

private ObservableCollection<string> _collection = new ObservableCollection<string>(); 

public ObservableCollection<string> Collection 
{ 
    get { return _collection; } 
    set 
    { 
     _collection = value; 
     if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Collection")); 
    } 
} 

XAMLコード私はnew ObservableCollection<string>();を取り外した場合は

<Window x:Class="SampleControl.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:SampleControl" 
     Title="MainWindow" Height="400" Width="525"> 
    <Grid> 
     <local:BTextBox 
      ItemsSource="{Binding Collection}" 
      ProviderCommand="{Binding AutoBTextCommand}" 
      AutoItemsSource="{Binding SuggCollection}" /> 
    </Grid> 
</Window> 

ですその後になります

​​

今プロパティCollectionは値NULLを開催。このプロパティはItemsSourceにバインドされています。だから、どのように私はこの方法でのItemsSource

CustomControlメソッドにデータを

private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { 
    var tb = d as BTextBox; 
    if ((e.NewValue != null) && ((tb.ItemsSource as IList) != null)) { 
     foreach (var item in e.NewValue as IEnumerable) { 
      (tb.ItemsSource as IList).Add(item); 
     } 
    } 
} 

されるプッシュすることができItemsSourceNOT NULLされている場合は、それがデータをプッシュし、NULLをチェック。

if ((e.NewValue != null) && ((tb.ItemsSource as IList) != null))ItemsSourceNOT NULLある場合のみ、その項目がコレクション(tb.ItemsSource as IList).Add(item);

に押し込まれます、そして、親切にNull-able IEnumerableに値を割り当てる方法を、私を助けますか?

+0

'ItemsSourceがNULLでない場合は、データをプッシュします。ちょっと曖昧です... –

+0

@ JeroenvanLangen上記のメソッドを見れば、 'NULL'〜' if((e.NewValue!= null)&&((tb.ItemsSource as IList)!= null)) 'がチェックされます。 ItemsSourceがNULLでない場合、アイテムだけが '(tb.ItemsSource as IList).Add(item);'コレクションにプッシュされます。 –

+0

しかし、XamlのItemsSourceに 'Collection'をバインドしていますが、それらのアイテムを' AutoItemsSource'にも追加していますか? –

答えて

0

を例えば今

if(this.ItemsSource == null) 
{ 
    this.ItemsSource = (new List<object>()).AsEnumerable(); 
} 

(tb.ItemsSource as IList).Add(item); 

、それはどんなNULLリファレンス例外をスローしないであろう

0

ObservableCollectionにデータを追加するときは、その前にコンストラクタを追加するだけです。それは私の通常の練習です。例えば

、あなたの状況に応じて

Collection = new ObservableCollection<string>(); 
...add data to the collection here 

編集

あなたはカスタムコントロールのレベルで解決をしたい場合は、おそらくこのような何かしてみてください:項目を追加する前に、私たちが作成する必要があり、我々は項目を追加することができます

var tb = d as BTextBox; 
if (((tb.ItemsSource as IList) == null)) 
    tb.ItemsSource = new IList<string>(); // or new ObservableCollection, List, or any suitable datatype, just to give default values 
if ((e.NewValue != null) && ((tb.ItemsSource as IList) != null)) { // probably remove the checking for ((tb.ItemsSource as IList) != null) since we initialized it 
... 
+0

ここでは、ViewModelカスタムコントロールレベルで解決策が必要です。 –

+0

'IList 'は一般的ではありません。私が 'Int' Collectionをバインドすれば、??? –

+0

この回答を見て:[カスタムItemsSource依存プロパティを持つユーザーコントロール](http://stackoverflow.com/a/4539697/6741868)。 e.oldValueとe.newValueを使ってnullの場合をチェックして処理する方法は面白いです。 –

関連する問題