2013-06-21 47 views
10

Tôi đang cố gắng tạo một ứng dụng WPF đơn giản bằng cách sử dụng ràng buộc dữ liệu. Mã có vẻ tốt, nhưng chế độ xem của tôi không cập nhật khi tôi cập nhật thuộc tính của mình. Dưới đây là XAML của tôi:Kết nối dữ liệu WPF Nội dung nhãn

<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> 

Dưới đây là code-behind của tôi:

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

Dưới đây là xem mô hình của tôi

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)); 
     } 
    } 
} 

Tôi không thể nhìn thấy những gì tôi đang thiếu ở đây? ? o.O

Trả lời

14

CalculatorOutput không có getter. Chế độ xem nên nhận giá trị như thế nào? Tài sản cũng phải được công khai.

public String CalculatorOutput 
{ 
    get { return _calculatorOutput; } 
    set 
    { 
     _calculatorOutput = value; 
     NotifyPropertyChanged(); 
    } 
} 
Các vấn đề liên quan