【发布时间】:2018-10-24 04:01:57
【问题描述】:
默认情况下,加载我的 DataGrid 时,我的数据将按 ProductName asc 排序。但是,gridview 的 ProductName 标题不会显示向上箭头图标。无论如何,我是否可以通过编程方式触发图标?
XAML:
<DataGrid x:Name="GridProduct"
ItemsSource="{Binding Path=ProductResult}"
Style="{StaticResource defaultDataGridStyle}"
CellStyle="{StaticResource defaultCellStyle}"
ColumnHeaderStyle="{StaticResource defaultCellHeaderStyle}">
<DataGrid.Columns>
<DataGridTextColumn Header="Product Name" Binding="{Binding ProductName}" />
<DataGridTextColumn Header="Product Price" Binding="{Binding ProducPrice}"/>
</DataGrid.Columns>
</DataGrid>
风格:
<Style x:Key="defaultCellHeaderStyle" TargetType="DataGridColumnHeader" BasedOn="{StaticResource MetroDataGridColumnHeader}">
<Setter Property="FontSize" Value="16"></Setter>
<Setter Property="Command" Value="{Binding Path=DataContext.SortCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
<Setter Property="CommandParameter" Value="{Binding Path=Content, RelativeSource={RelativeSource Self}}"></Setter>
</Style>
<Style x:Key="defaultCellStyle" TargetType="DataGridCell" BasedOn="{StaticResource MetroDataGridCell}">
<Setter Property="FontSize" Value="16"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
</Style>
MVVM:
public List<Product> ProductResult
{
get
{
_productResult = _productResult.OrderBy(x => x.Name).ToList();
return _productResult;
}
}
【问题讨论】:
-
MVVM 中的排序、选择跟踪和过滤(如果您在 GUI 端执行任何操作)通常通过 CollectionView 完成。如果您不将控件交给一个,它们将自动创建一个。您可能需要控制它。 docs.microsoft.com/en-us/dotnet/framework/wpf/controls/…