在代码隐藏中编辑 DataGridTemplateColumn?
我认为这非常简单,但我到处搜索,似乎找不到答案。我有一个 DataGridTemplateColumn
,我想用它来显示 DataGrid
的 DataContext
中不存在的值。即我有一个实体,根据文化有不同的名称。当网格加载时,我想根据当前的文化获取适当的名称。每次我看到有关 DataGridTemplateColumn
的任何内容时,它们总是使用 Binding 语法。我在这里不能这样做。我需要什么 C# 代码来访问以下 XAML 中的“nameValue”TextBlock,以及我应该在什么事件处理程序中访问它:
<Datagrid:DataGridTemplateColumn Header="Name" x:Name="nameField">
<Datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="nameValue" />
</StackPanel>
</DataTemplate>
</Datagrid:DataGridTemplateColumn.CellTemplate>
</Datagrid:DataGridTemplateColumn>
提前感谢所有人,对于超级 n00b 问题我深表歉意。
I would think that this would be incredibly simple, but I've searched all over and can't seem to find an answer. I have a DataGridTemplateColumn
that I want to use to display a value that isn't in the DataContext
of the DataGrid
. I.e. I have an entity that has different names based on the culture. When the grid loads, I want to go get the appropriate name based on the current culture. Every time I see anything about DataGridTemplateColumn
s, they're always using the Binding syntax. I can't do that here. What C# code do I need to access the "nameValue" TextBlock in the following XAML, and in what event handler should I access it:
<Datagrid:DataGridTemplateColumn Header="Name" x:Name="nameField">
<Datagrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="nameValue" />
</StackPanel>
</DataTemplate>
</Datagrid:DataGridTemplateColumn.CellTemplate>
</Datagrid:DataGridTemplateColumn>
Thanks to all in advance and I'm sorry for the super n00b question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仍然可以使用绑定语法,听起来您只需要绑定到静态方法而不是网格的数据上下文。这里有一个很好的参考http:// /blog.mrlacey.co.uk/2011/03/binding-to-static-classes-in-windows.html 以此为例并根据您的情况进行修改。
第一:像平常一样设置网格、项目源和列、标准数据绑定。这将处理您需要从数据库或其他来源获取的任何列。
第二:在您的项目中添加静态类
第三:将新资源添加到应用资源中
最后:在
TextBlock
中设置绑定,如果您已经构建了项目,您应该能够在绑定编辑器窗口中看到该属性。You can still use the binding syntax, it sounds like you just need to bind to a static method instead of the data context of the grid. There is a good reference here http://blog.mrlacey.co.uk/2011/03/binding-to-static-classes-in-windows.html Taking that as an example and modifying for your case.
First: Setup your grid as you would normally, item source and columns, standard data binding. This will take care of any columns you need from the database or other source.
Second: In your project add your static class
Third: Add your new resource to app resources
Finally: Set the binding in your
TextBlock
, if you've built your project, you should be able to see the property in the binding editor window.