Ich bin ein mit Custom Control
abgeleitet von einem ItemsControl
.Ersetzen NULL mit einem Standardwert in einem Itemssource IEnumerable
hatte ich die Idee von Two-Way Binding Issue of Unknown Object in WPF Custom Control Dependency Property
In der obigen Frage, sie nutzen die Sammlung in der Ansicht Modell
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"));
}
}
Der XAML-Code ist
<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>
Wenn ich die new ObservableCollection<string>();
entfernt dann wird es
private ObservableCollection<string> _collection;
public ObservableCollection<string> Collection
{
get { return _collection; }
set
{
_collection = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Collection"));
}
}
werden
Nun ist die Immobilie Collection
halten den Wert NULL
. Diese Eigenschaft ist bind in ItemsSource
. Also, wie kann ich die Daten in die Itemssource
Die Custom Methode Push ist
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);
}
}
}
Bei diesem Verfahren überprüft es für NULL
, WENN die ItemsSource
ist NOT NULL
, dann drückt er den Daten .
if ((e.NewValue != null) && ((tb.ItemsSource as IList) != null))
. Wenn die ItemsSource
NOT NULL
ist, dann wird nur dann bekommt Element in die Sammlung geschoben (tb.ItemsSource as IList).Add(item);
bitte mir helfen, wie sich der Wert in einem Null-able IEnumerable
zuweisen?
'Ist der Itemssource NOT NULL ist, dann drückt er die data' irgendwie vage ... –
@JeroenvanLangen Bitte sehen Sie die obige Methode, es überprüft 'NULL' ~' if ((e.NewValue! = Null) && ((tb.ItemsSource als IList)! = Null)) '. Wenn die ItemsSource nicht NULL ist, dann wird das Item nur in die Collection '(tb.ItemsSource as IList) .Add (item); '. –
Aber Sie binden die 'Collection' an die ItemsSource in Xaml, aber Sie fügen diese Elemente auch zur' AutoItemsSource' hinzu? –