为什么内容绑定没有更新

时间:2020-01-02 12:41:55

标签: c# wpf events mvvm binding

我尝试在xaml中的labal内容中使用数据绑定 为了从视图模型显示为整数或字符串, 但是绑定不起作用,无法更新。

谢谢

这是我的代码:

在xmal中:

<Window x:Class="Calculator.View.Calculator"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Calculator.View"
        xmlns:vm="clr-namespace:Calculator.ViewModel"
        mc:Ignorable="d"
        Title="Calculator" Height="500" Width="350">
    <Window.Resources>
        <vm:Calculations x:Key="vm"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource vm}">   
       <Label Content="{Binding Calc.Result, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
        HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="60"/>
    </Grid>

在xaml.cs中,我从视图模型添加了孔隙率计算:

public partial class Calculator : Window
    {
        private readonly Calculations Calculations = new Calculations();

        public Calculator()
        {
            InitializeComponent();
            DataContext = Calculations;
        }
    }

在视图模型中:

  {
        private CalculatorModel calc;

        public CalculatorModel Calc
        {
            get { return calc; }
            set { calc = value; }
        }
  }
public class CalculatorModel : INotifyPropertyChanged
    {
        private string result;

        public string Result
        {
            get { return result; }
            set
            {
                result = value;
                OnPropertyChanged("Result");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

2 个答案:

答案 0 :(得分:1)

删除XAML资源,并让根Grid从父窗口继承DataContext,即您的XAML标记应如下所示:

<Window x:Class="Calculator.View.Calculator"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Calculator.View"
        xmlns:vm="clr-namespace:Calculator.ViewModel"
        mc:Ignorable="d"
        Title="Calculator" Height="500" Width="350">
    <Grid>
       <Label Content="{Binding Calc.Result, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
        HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="60"/>
    </Grid>
</Window>

然后,如果您在Calculations中设置Calculator.xaml.cs字段的相应属性,它将起作用。

取决于设置属性的方式,您可能还需要实现INotifyPropertyChanged并在视图模型类中引发PropertyChanged事件。

答案 1 :(得分:0)

您有Calculations类的两个不同的实例。一个在代码隐藏(Calculations Calculations = new Calculations())中创建,另一个在标记(<vm:Calculations x:Key="vm"/>)中创建。

Window使用calculations实例运行,但显示vm实例。仅保留其中之一,例如:

public partial class Calculator : Window
{
    private readonly Calculations calculations = new Calculations();

    public Calculator()
    {
        InitializeComponent();
        calculations = (Calculations)this.Resources["vm"];
    }
}

替换DataContext(DataContext = Calculations;可能可行,但是为Window设置了新的DataContext,而不是由Grid拾取,因为Grid明确将其DataContxt设置为StaticResource