绑定前检查父属性是否为null

时间:2023-01-07 11:46:37

I am currently trying to find a way to check if a parent property is null before binding to a sub property, like this:

我目前正在尝试找到一种方法来检查父属性在绑定到子属性之前是否为null,如下所示:

...{Binding Item.Text}

Is there a way to check if Item is null before accessing the Text property? As it stands right now I get a NullReferenceException in PresentationFramework.dll which crashes the app.

有没有办法在访问Text属性之前检查Item是否为null?现在,我在PresentationFramework.dll中得到一个NullReferenceException,它崩溃了应用程序。

This is particularly strange as I set the Item in the ViewModel constructor and have verified that it exists before the rendering step begins:

当我在ViewModel构造函数中设置Item并在渲染步骤开始之前验证它存在时,这一点特别奇怪:

public MyViewModel()
{
    Item = new Foo();
    Item.Text = "Bar";
}

1 个解决方案

#1


0  

I would suggest creating a viewmodel property for the model property you want to expose.

我建议为要公开的模型属性创建一个viewmodel属性。

Since the example you've shown works for me..

既然你展示的例子对我有用..

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new MyViewModel();
    }
}

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Foo item;

    public MyViewModel()
    {
        Item = new Foo();
        Item.Item = "Bar";
    }

    public Foo Item
    {
        get
        {
            return this.item;
        }
        set
        {
            this.item = value;
            RaisePropertyChanged("Item");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class Foo
{
    public string Item { get; set; }
}

<TextBox Width="200" Height="30" Text="{Binding Item.Item, Mode=TwoWay}" TextAlignment="Center"/>

You can expose the subproperty and perform the checking like this..

您可以公开子属性并执行这样的检查。

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Foo item;

    public MyViewModel()
    {
        //Item = new Foo();
        //Item.Item = "Bar";
    }

    public Foo Item
    {
        get
        {
            return this.item;
        }
        set
        {
            this.item = value;
            RaisePropertyChanged("Item");
        }
    }

    public string Bar
    {
        get
        {
            if(Item == null)
            {
                return "Item is null. OMG!";
            }
            else
            {
                return Item.Item;
            }
        }
        set
        {
            Item.Item = value;
            RaisePropertyChanged("Bar");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

<TextBox Width="200" Height="30" Text="{Binding Bar}" TextAlignment="Center"/>

With this, you might want to change your view model design.

有了这个,您可能想要更改视图模型设计。

#1


0  

I would suggest creating a viewmodel property for the model property you want to expose.

我建议为要公开的模型属性创建一个viewmodel属性。

Since the example you've shown works for me..

既然你展示的例子对我有用..

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new MyViewModel();
    }
}

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Foo item;

    public MyViewModel()
    {
        Item = new Foo();
        Item.Item = "Bar";
    }

    public Foo Item
    {
        get
        {
            return this.item;
        }
        set
        {
            this.item = value;
            RaisePropertyChanged("Item");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class Foo
{
    public string Item { get; set; }
}

<TextBox Width="200" Height="30" Text="{Binding Item.Item, Mode=TwoWay}" TextAlignment="Center"/>

You can expose the subproperty and perform the checking like this..

您可以公开子属性并执行这样的检查。

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Foo item;

    public MyViewModel()
    {
        //Item = new Foo();
        //Item.Item = "Bar";
    }

    public Foo Item
    {
        get
        {
            return this.item;
        }
        set
        {
            this.item = value;
            RaisePropertyChanged("Item");
        }
    }

    public string Bar
    {
        get
        {
            if(Item == null)
            {
                return "Item is null. OMG!";
            }
            else
            {
                return Item.Item;
            }
        }
        set
        {
            Item.Item = value;
            RaisePropertyChanged("Bar");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

<TextBox Width="200" Height="30" Text="{Binding Bar}" TextAlignment="Center"/>

With this, you might want to change your view model design.

有了这个,您可能想要更改视图模型设计。