如何动态更改标题的 DataGrid TextColumn?
我正在尝试构建一个 DataGrid 布局,其中第一列的名称将被动态更改。 我该如何在 DataGridTextColumn 的 Header 属性中更改它?我见过一些将 Header 属性连接到 StaticResource 的示例,但 StaticResource 是固定值,这对我来说不起作用, 一旦我需要的是几个值。 例子:
记住,这是我需要更改的几个示例之一。 谢谢。
I'm trying to build a DataGrid layout where the first column's name will be dinamically changed.
How can I do, into DataGridTextColumn's Header property, to change that? I've saw some examples than the Header property is connected into a StaticResource, but a StaticResource is a fixed value, and that's doesn't work for me,
once what I need is several values.
Example:
...
Remembering, this is one of several examples than i would need to change.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可以通过数据绑定轻松完成。
CodeBehind 方式
在窗口的代码隐藏中创建一个属性来保存字符串值;我将调用我的 TextProp。在本例中,我假设您的窗口的元素名称是“Window”。在 DataGridTextColumn 标记中,将 Header 属性数据绑定到该属性。
MVVM 方式
执行与上面相同的操作,除了将属性放在数据网格绑定到的视图模型上。将 XAML 更改为:
然后您所要做的就是以您选择的任何方式更改该属性值。要使其在属性更改时更新值,您需要实现 INotifyPropertyChanged(查看该帖子的底部)。
This can be done easily with Databinding.
The CodeBehind Way
Create a property in the codebehind of your window to hold the string value; I will call mine TextProp. I will assume the elementname of your window is "Window" for this example. In the DataGridTextColumn tag, databind the Header attribute to that property.
The MVVM Way
Do the same as above, except put the property on your viewmodel to which the datagrid is bound. Change the XAML to:
Then all you have to do is change that Property value in whatever way you choose. To get this to update the value when the property changes, you will need to implement INotifyPropertyChanged (Check at the bottom of that post).