0

私は次のモデルを持っています:

public class Car : BindableBase
{
    private string _model;
    private string _wheels;

    public string Model
    {
        get { return _model; }
        set { SetProperty(ref _model, value); }
    }

    public string Wheels
    {
        get { return _wheels; }
        set { SetProperty(ref _wheels, value); }
    }
}

public class Customer : BindableBase
{
    private Car _car;

    public Car Car
    {
        get { return _car; }
        set { SetProperty(ref _car, value); }
    }
}

バインディングは次のようになります。

<Page.Resources>
    <viewModels:CustomerViewModelLocator x:Key="PageViewModel" />
</Page.Resources>

<StackPanel>
    <TextBox Background="AliceBlue" Text="{Binding ViewModel.Car.Model, Mode=TwoWay, Source={StaticResource PageViewModel}}"></TextBox>
    <TextBox Background="AliceBlue" Text="{Binding ViewModel.Car.Wheels, Mode=TwoWay, Source={StaticResource PageViewModel}}"></TextBox>
</StackPanel>

デザイン時のビューに ViewModelLocator パターンを使用していますが、見栄えがします。しかし、実行時にモデルのセッターをヒットしません。

私は何を間違っていますか?

Customer モデル内の自明なタイプが設定されます...

4

1 に答える 1

2

これは、Carがnullの場合に発生します。Customerで_car=new Car()を設定してみてください。

于 2013-01-22T09:48:10.027 に答える