gpt4 book ai didi

c# - WPF MVVM-以编程方式触发datagrid排序方向的箭头

转载 作者:行者123 更新时间:2023-12-03 11:02:34 25 4
gpt4 key购买 nike

默认情况下,在加载DataGrid时,我的数据将按ProductName asc排序。但是,gridview的ProductName header 不会显示向上箭头图标。反正我是通过编程方式触发图标吗?

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;
}
}

最佳答案

将以下内容添加到DataGridTextColumn中:

SortDirection="Ascending" 

关于c# - WPF MVVM-以编程方式触发datagrid排序方向的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50331237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com