无法以编程方式找到 DataGridColumn 的名称
我在数据网格中找到了 Columns 集合,并希望迭代它以找到某个列名称。但是,我不知道如何解决该列的 x:Name 属性。此 xaml 说明了我的 DataGridTextColumn 和 DataGridTemplateColumn 问题:
<t:DataGrid x:Name="dgEmployees" ItemsSource="{Binding Employees}"
AutoGenerateColumns="false" Height="300" >
<t:DataGrid.Columns>
<t:DataGridTextColumn x:Name="FirstName" Header="FirstName"
Binding="{Binding FirstName}" />
<t:DataGridTemplateColumn x:Name="LastName" Header="LastName" >
<t:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastName}" />
</DataTemplate>
</t:DataGridTemplateColumn.CellTemplate>
</t:DataGridTemplateColumn>
</t:DataGrid.Columns>
</t:DataGrid>
这是我的代码:
DataGrid dg = this.dgEmployees;
foreach (var column in dg.Columns)
{
System.Console.WriteLine("name: " + (string)column.GetValue(NameProperty));
}
在运行时,不存在任何值; column.GetValue 不返回任何内容。使用 Snoop,我确认 DataGridTextColumn 或 DataGridTemplateColumn 上没有 Name 属性。
我缺少什么?
I found the Columns collection in my datagrid, and was hoping to iterate through it to find a certain column Name. However, I can't figure out how to address the x:Name attribute of the column. This xaml illustrates my problem with a DataGridTextColumn and a DataGridTemplateColumn:
<t:DataGrid x:Name="dgEmployees" ItemsSource="{Binding Employees}"
AutoGenerateColumns="false" Height="300" >
<t:DataGrid.Columns>
<t:DataGridTextColumn x:Name="FirstName" Header="FirstName"
Binding="{Binding FirstName}" />
<t:DataGridTemplateColumn x:Name="LastName" Header="LastName" >
<t:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastName}" />
</DataTemplate>
</t:DataGridTemplateColumn.CellTemplate>
</t:DataGridTemplateColumn>
</t:DataGrid.Columns>
</t:DataGrid>
And here is my code:
DataGrid dg = this.dgEmployees;
foreach (var column in dg.Columns)
{
System.Console.WriteLine("name: " + (string)column.GetValue(NameProperty));
}
At runtime, no value is present; column.GetValue doesn't return anything. Using Snoop, I confirmed that there is no Name property on either DataGridTextColumn or DataGridTemplateColumn.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WPF 有两个不同但相似的概念,x:Name,用于创建引用 XAML 中定义的元素的字段,即将代码隐藏到 XAML,以及 FrameworkElement.Name,它唯一地命名名称范围。
如果元素具有 FrameworkElement.Name 属性,则 x:Name 会将此属性设置为 XAML 中给定的值。但是,在某些情况下,将非 FrameworkElement 元素链接到代码隐藏中的字段很有用,例如在您的示例中。
请参阅此相关问题:
在 WPF 中, x:Name 和 Name 属性之间有什么区别?
作为替代方案,您可以定义自己的附加属性,该属性可用于命名列。附加属性定义如下:
然后您可以为每列分配一个名称...
然后在代码中访问该名称,如下所示:
希望有帮助
WPF has two different, yet similar concepts, x:Name, which is used to create a field which references an element defined in XAML, i.e. connecting your code-behind to your XAML, and FrameworkElement.Name, which uniquely names an element within a namescope.
If an element has a FrameworkElement.Name property, x:Name will set this property to the value given in XAML. However, there are instances where it is useful to link non FrameworkElement elements to fields in code-behind, such as in your example.
See this related question:
In WPF, what are the differences between the x:Name and Name attributes?
As an alternative, you could define your own attached property which can be used to name the columns. The attached property is defined as follows:
You can then assign a name to each column ...
Then access this name in code as follows:
Hope that helps
您可以使用 linq 查询查找数据网格列标题的名称
,其中 dgvReports 是数据网格的名称。
You can use linq query to find name of the datagrid column Headers
where dgvReports is name of the datagrid.