【问题标题】:Replace NULL with a default Value in an ItemsSource IEnumerable将 NULL 替换为 ItemsSource IEnumerable 中的默认值
【发布时间】:2017-01-23 03:02:36
【问题描述】:

我有一个从 ItemsControl 派生的 Custom Control。

我的想法来自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 代码是

<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&lt;string&gt;();,它就会变成

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 中

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) &amp;&amp; ((tb.ItemsSource as IList) != null))。如果 ItemsSource 是 NOT NULL,那么只有这样项目才会被推入集合 (tb.ItemsSource as IList).Add(item);

请帮助我,如何在 Null-able IEnumerable 中分配值?

【问题讨论】:

  • IF the ItemsSource is NOT NULL, then it pushes the data 有点模糊......
  • @JeroenvanLangen 请看上面的方法,它会检查NULL ~ if ((e.NewValue != null) &amp;&amp; ((tb.ItemsSource as IList) != null))。如果 ItemsSource 不为 NULL,那么只有项目才会被推入集合 (tb.ItemsSource as IList).Add(item);。
  • 但是,您将 Collection 绑定到 Xaml 中的 ItemsSource,但您还将这些项目添加到 AutoItemsSource?
  • 当我在 xaml 中绑定一个项目时,我只创建集合.._(在构造函数中)_
  • 为什么不使用这个:tb.ItemsSource = e.NewValue;

标签: c# wpf list custom-controls ienumerable


【解决方案1】:

我们可以添加Item,在添加Item之前我们需要创建一个Instance

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

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

现在,它不会抛出任何 NULL 引用异常。

【讨论】:

    【解决方案2】:

    每当您向 ObservableCollection 添加数据时,只需在此之前添加一个构造函数。这是我一贯的做法。

    例如,

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

    视情况而定。

    编辑

    如果您想要自定义控件级别的解决方案,不妨尝试以下方法:

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

    【讨论】:

    • 这里我需要的不是 ViewModel 级别的通用解决方案,而是自定义控制级别的解决方案。
    • 看看这个答案:User Control with custom ItemsSource dependency property。有趣的是他们如何使用 e.oldValue 和 e.newValue 来检查和处理 null 情况。
    猜你喜欢
    • 2017-01-02
    • 2012-09-04
    • 2014-12-26
    • 1970-01-01
    • 2017-11-01
    • 2016-09-29
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多