Я с Custom Control
, полученный из ItemsControl
.Заменить NULL со значением по умолчанию в 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"));
}
}
The XAML кодекса
<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>
Если я удалил new ObservableCollection<string>();
то это будет
private ObservableCollection<string> _collection;
public ObservableCollection<string> Collection
{
get { return _collection; }
set
{
_collection = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Collection"));
}
}
Теперь недвижимость Collection
содержать значение NULL
. Это свойство связывается в ItemsSource
. Итак, как я мог бы передать данные в ItemsSource
The 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);
}
}
}
В этом методе, он проверяет NULL
, ЕСЛИ ItemsSource
является NOT NULL
, то толкает данные ,
if ((e.NewValue != null) && ((tb.ItemsSource as IList) != null))
. Если ItemsSource
является NOT NULL
, то только затем пункт получает толкнул в коллекцию (tb.ItemsSource as IList).Add(item);
Просьба помочь мне, как присвоить значение в Null-able IEnumerable
?
'IF, что ItemsSource не является NULL, то он толкает данные' является своего рода неопределенным ... –
@JeroenvanLangen Просьба видеть вышеописанный метод, он проверяет 'NULL' ~' if ((e.NewValue! = Null) && ((tb.ItemsSource как IList)! = Null)) '. Если ItemSource не является NULL, то только тогда элемент попадает в коллекцию '(tb.ItemsSource как IList) .Add (item);'. –
Но вы привязываете 'Collection' к ItemsSource в Xaml, но вы добавляете эти элементы также в' AutoItemsSource'? –