[英]apply a template to a column of datagrid
I'm using MVVM
and I want to apply a template to a specific column of my datagrid. 我正在使用
MVVM
,我想将模板应用于datagrid的特定列。
I create Machine class which have Name, costs, properties. 我创建具有名称,成本,属性的Machine类。
I create also Operation Class which have Name porperty, ExecutiveMachine property (Type : Machine) When I use itemsSource to display my list of opertations, I see the path of my machines whereas I want to display their name. 我还创建了具有名称属性,ExecutiveMachine属性(类型:计算机)的操作类,当我使用itemsSource显示操作列表时,我看到了机器的路径,而我想显示它们的名称。 I added
DataGridTemplateColumn
but now, I have machine column twice (one correct, and the other is incorrect) 我添加了
DataGridTemplateColumn
但是现在,我有两次机器列(一个正确,另一个不正确)
Actually my code is : 实际上我的代码是:
<DataGrid ItemsSource="{Binding Path=Operations}" Name="datagridOperation" Width="Auto" Height="Auto" Margin="10" HorizontalContentAlignment="Center" SelectionMode="Single" MinColumnWidth="80" CanUserAddRows="False" CanUserDeleteRows="False" RowBackground="DimGray" AlternatingRowBackground="#FF3E3E3E" AlternationCount="1" IsReadOnly="True" SelectionChanged="UpdateOperationComboBox">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Machine" CanUserSort="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=ExecutiveMachine.Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
My first idea was to add manually DataGridColumns
. 我的第一个想法是手动添加
DataGridColumns
。 I tried it but when a do it i have twice all my columns and if I delete ItemsSource
property of my DataGrid
I have only once but the three columns become empty. 我尝试了一下,但是当我这样做时,我的所有列都增加了两倍,如果删除
DataGrid
ItemsSource
属性,我只有一次,但是三列变为空。 If my idea is good, how can I bind this ItemsSource
? 如果我的想法很好,如何绑定此
ItemsSource
?
If you want to add you columns manually then you need to switch off AutoGenerateColumns
against your DataGrid
如果要手动添加列,则需要针对
DataGrid
关闭AutoGenerateColumns
<DataGrid
ItemsSource="{Binding Path=Operations}"
Name="datagridOperation"
...
AutoGenerateColumns="False">
EDIT 编辑
To manually define sorting path for any DataGridColumn
you can specify DataGridColumn.SortMemberPath
要为任何
DataGridColumn
手动定义排序路径,可以指定DataGridColumn.SortMemberPath
<DataGridTemplateColumn Header="Machine" CanUserSort="True" SortMemberPath="SortPropertyName">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.