单元格的绑定数据

时间:2018-12-08 04:27:17

标签: c wpf

 <GridView AllowsColumnReorder="False">
      <GridViewColumn Header="Tên Mặt Hàng" CellTemplate="{StaticResource Ten}">
          <GridViewColumn.CellTemple>
                <DataTemplate x:Key="Ten">
                     <TextBlock Text="{Binding Ten}"></TextBlock>
                </DataTemplate>
          </GridViewColumn.CellTemple>
      </GridViewColumn> </GridView>

每个人都有代码,我想绑定textBlock的Text以在其他文件中再次使用数据,那该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以创建此视图的ViewModel。并将viewModel属性绑定到该视图。例如:

public class NamViewModel: INotifyPropertyChanged {
    private string _ten;
    public string Ten {
        get
        {
           return _ten;
        }
        set 
        {
           _ten = value;
           this.RaisePropertyChanged("Ten");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

然后,您可以将此viewModel绑定到视图的构造函数中。例如:

public partial class NamView : Window {
    public NamView() {
        InitializeComponent();
        this.DataContext = new NamViewModel();
    }
}