c - celltemp结合数据

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

各位,我有代码,我想绑定(bind)textBlock的Text,以便在其他文件中再次使用数据,那我该怎么办?

最佳答案

您可以创建此 View 的 ViewModel。并将 viewModel 属性绑定(bind)到该 View 。例如:

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

然后你可以在 View 的构造函数中绑定(bind)这个viewModel。例如:

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

关于c - celltemp结合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53679487/

相关文章:

c# - 在 WPF 中绑定(bind)源时图像未显示

c - 只打印字符串的最右边部分

c - 清理代码的最佳命令行工具是什么?

c - 桌面环境之外的基于 GTK 的应用程序

c# - 从另一个项目的可执行文件启动一个项目的可执行文件

c# - 处理 "The operation completed successfully"错误

c - 二维数组的无限 for 循环

c - lldb 的行号是如何确定的?

c# - 将 DataGrid 列标题拖放到标签上?

.net - 提供两个接口(interface)的 Web 服务在 WPF 应用程序中导致错误