Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c# - Replace NULL with a default Value in an ItemsSource IEnumerable

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...