のデフォルト値とNULLを交換しCustom Control
はItemsControl
由来。私がいるの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);
}
}
}
されるプッシュすることができItemsSource
がNOT NULL
されている場合は、それがデータをプッシュし、NULL
をチェック。
if ((e.NewValue != null) && ((tb.ItemsSource as IList) != null))
。 ItemsSource
がNOT NULL
ある場合のみ、その項目がコレクション(tb.ItemsSource as IList).Add(item);
に押し込まれます、そして、親切にNull-able IEnumerable
に値を割り当てる方法を、私を助けますか?
'ItemsSourceがNULLでない場合は、データをプッシュします。ちょっと曖昧です... –
@ JeroenvanLangen上記のメソッドを見れば、 'NULL'〜' if((e.NewValue!= null)&&((tb.ItemsSource as IList)!= null)) 'がチェックされます。 ItemsSourceがNULLでない場合、アイテムだけが '(tb.ItemsSource as IList).Add(item);'コレクションにプッシュされます。 –
しかし、XamlのItemsSourceに 'Collection'をバインドしていますが、それらのアイテムを' AutoItemsSource'にも追加していますか? –