【问题标题】:WPF Data binding Label contentWPF 数据绑定标签内容
【发布时间】:2013-06-18 11:47:08
【问题描述】:

我正在尝试使用数据绑定创建一个简单的 WPF 应用程序。 代码看起来不错,但是当我更新我的属性时,我的视图没有更新。 这是我的 XAML:

<Window x:Class="Calculator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:calculator="clr-namespace:Calculator"
        Title="MainWindow" Height="350" Width="525"
        Name="MainWindowName">
    <Grid>
        <Label Name="MyLabel" Background="LightGray" FontSize="17pt" HorizontalContentAlignment="Right" Margin="10,10,10,0" VerticalAlignment="Top" Height="40" 
               Content="{Binding Path=CalculatorOutput, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

这是我的代码隐藏:

namespace Calculator
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            DataContext = new CalculatorViewModel();
            InitializeComponent();
        }
    }
}

这是我的视图模型

namespace Calculator
{
    public class CalculatorViewModel : INotifyPropertyChanged
    {
        private String _calculatorOutput;
        private String CalculatorOutput
        {
            set
            {
                _calculatorOutput = value;
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            var handler = PropertyChanged;
            if (handler != null)
               handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我看不到我在这里缺少什么? o.O

【问题讨论】:

    标签: c# wpf data-binding inotifypropertychanged


    【解决方案1】:

    CalculatorOutput 没有吸气剂。 View 应该如何获取价值?该属性也必须是公开的。

    public String CalculatorOutput
    {
        get { return _calculatorOutput; }
        set
        {
            _calculatorOutput = value;
            NotifyPropertyChanged();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多