Replace NULL with a default Value in an ItemsSource IEnumerable

最后都变了- 提交于 2020-01-07 12:02:06

问题


I'm having a Custom Control derived from an ItemsControl.

I got the idea from Two-Way Binding Issue of Unknown Object in WPF Custom Control Dependency Property

In the above Question, they use the Collection in the View Model

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 Code is

<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>

If I removed the new ObservableCollection<string>(); then it will became

private ObservableCollection<string> _collection;

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

Now the Property Collection hold the value NULL. This Property is bind in the ItemsSource. So, how could I push the data into the ItemsSource

The CustomControl Method is

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);
        }
    }
}

In this method, it checks for NULL, IF the ItemsSource is NOT NULL, then it pushes the data.

if ((e.NewValue != null) && ((tb.ItemsSource as IList) != null)). If the ItemsSource is NOT NULL, then only then item gets pushed into the collection (tb.ItemsSource as IList).Add(item);

Kindly assist me, how to assign the Value in a Null-able IEnumerable ?


回答1:


We Can add the Item, before adding the Item we need to create an Instance

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

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

Now, it won't throw any NULL Reference exception.




回答2:


Whenever you add data to the ObservableCollection, just add a constructor before that. It is my usual practice.

For example,

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

Depending on your situation.

Edit

If you want a solution on the Custom Control level, maybe try something like this:

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
...


来源:https://stackoverflow.com/questions/39505127/replace-null-with-a-default-value-in-an-itemssource-ienumerable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!