如何在WPF中正确绑定我的模型?

时间:2015-06-27 18:01:16

标签: c# wpf xaml mvvm

我有一个名为ViewModel的类,它实现了INotifyPropertyChanged接口。在我的XAML中,我有3个文本框,它们具有与我的ViewModel属性匹配的绑定属性:

TestsOrdered TestsCompleted TestsRemaining

我的代码隐藏中有一个PropertyChanged事件处理程序。我有一个附加到XAML窗口的上下文“MyContext”。我不确定为什么在更改TestsOrdered和TestsCompleted值时UI不会更新。我希望在我从文本框中跳出时更新UI,但直到我选中TestsRemaining文本框才会更新。我设计中的缺陷在哪里?

public partial class MainWindow : Window
{
    public ViewModel vm;

    public MainWindow()
    {
        InitializeComponent();
        vm = new ViewModel();
        //ct = new CompletionTime();
        //ct.TestsOrdered = 8000;

        vm.Name = "John Doe";
        vm.TestsCompleted = 0;
        vm.TestsOrdered = 0;
        vm.TestsRemaining = 0;

        this.MyContext.DataContext = vm;
    }

    private void btnName_Click(object sender, RoutedEventArgs e)
    {
        vm.Name = "Joe";
    }
}

public class ViewModel : INotifyPropertyChanged
{
    private string name;
    private int mTestsOrdered;
    private int mTestsRemaining;
    private int mTestsCompleted;

    public string Name
    {
        get { name; }
        set
        {
            if (name != value)
            {
                name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }
    }


    public int TestsOrdered
    {
        get { return mTestsOrdered; }
        set
        {
            if (mTestsOrdered != value)
            {
                mTestsOrdered = value;
                PropertyChanged(this, new PropertyChangedEventArgs("TestsOrdered"));
            }
        }
    }


    public int TestsCompleted
    {
        get { return mTestsCompleted; }
        set
        {
            if (mTestsCompleted != value)
            {
                mTestsCompleted = value;
                PropertyChanged(this, new PropertyChangedEventArgs("TestsCompleted"));
            }
        }
    }


    public int TestsRemaining
    {
        get { return TestsOrdered - TestsCompleted; }
        set
        {
            if (mTestsRemaining != value)
            {
                mTestsRemaining = value;
                PropertyChanged(this, new PropertyChangedEventArgs("TestsRemaining"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

MyContext是包含ViewModel对象的DataContext。

XAML:

 <Grid Margin="-1,-9,1,9">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="244*"/>
            <ColumnDefinition Width="251*"/>
        </Grid.ColumnDefinitions>
        <TextBox Width="100"  HorizontalAlignment="Center" Margin="96.791,40,54,0" TextWrapping="Wrap" Text="{Binding Path=Name}" VerticalAlignment="Top" Grid.Column="1"/>

        <Button x:Name="btnName" Content="Button" HorizontalAlignment="Left" Margin="171,210,0,0" VerticalAlignment="Top" Width="148" Height="47" Click="btnName_Click" Grid.ColumnSpan="2"/>
        <TextBox Grid.Column="0" HorizontalAlignment="Left" Height="23" Margin="97,84,0,0" TextWrapping="Wrap" Text="{Binding Path=TestsOrdered}" VerticalAlignment="Top" Width="120"/>
        <TextBox Grid.Column="0" HorizontalAlignment="Left" Height="23" Margin="97,112,0,0" TextWrapping="Wrap" Text="{Binding Path=TestsCompleted}" VerticalAlignment="Top" Width="120"/>
        <TextBox Grid.Column="0" HorizontalAlignment="Left" Height="23" Margin="97,140,0,0" TextWrapping="Wrap" Text="{Binding Path=TestsRemaining}" VerticalAlignment="Top" Width="120"/>

    </Grid>

1 个答案:

答案 0 :(得分:1)

您的绑定和属性已正确写入。但问题在于您将viewmodel实例分配给Window。您需要在属性DataContext上设置viewmodel的实例才能使绑定生效。像这样:

 public MainWindow()
{
    InitializeComponent();
    vm = new ViewModel();
    //ct = new CompletionTime();
    //ct.TestsOrdered = 8000;

    vm.Name = "John Doe";
    vm.TestsCompleted = 0;
    vm.TestsOrdered = 0;
    vm.TestsRemaining = 0;

    //this.MyContext.DataContext = vm;
    this.DataContext = vm; // <-- Like so
}

原因是绑定的工作方式。如果未提供其他来源(SourceRelativeSourceElementName属性Binding),则绑定将使用DataContext的{​​{1}}属性他们被分配到。在您的情况下FrameworkElement

当您设置Window的TextBox属性时,它的所有子对象都会继承该对象实例及其子对象,依此类推。

尝试更改ViewModel分配,您将看到绑定有效。

//干杯