![]() |
风度翩翩的丝瓜 · 香港、澳门特别行政区律师事务所驻内地代表机构 ...· 1 年前 · |
![]() |
痴情的油条 · Mybatis拦截器动态修改sql语句 - ...· 1 年前 · |
![]() |
腼腆的木耳 · python加载properties配置-腾 ...· 1 年前 · |
![]() |
谦虚好学的紫菜 · [C#]MemoryStream.Dispo ...· 1 年前 · |
![]() |
微笑的松球 · JDK从1.8升级到9.0.1后sun.mi ...· 1 年前 · |
默认情况下,当加载DataGrid时,ProductName将对数据进行排序。但是,网格视图的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
_productResult = _productResult.OrderBy(x => x.Name).ToList();
return _productResult;
}
发布于 2018-05-14 13:34:31
将以下内容添加到
DataGridTextColumn
SortDirection="Ascending"
发布于 2019-02-01 10:49:27
如果要将有效排序与列上的视觉样式同步,这将有助于:
( (INotifyCollectionChanged)Items.SortDescriptions ).CollectionChanged += new NotifyCollectionChangedEventHandler( OnItemsSortDescriptionsChanged );
private void OnItemsSortDescriptionsChanged( object sender, NotifyCollectionChangedEventArgs e )
//Synchronize effective sorting in the grid and Visual style on columns
if ( Items != null )
foreach ( DataGridColumn column in Columns )
column.SortDirection = null;
foreach ( SortDescription sd in Items.SortDescriptions )
if ( column.SortMemberPath == sd.PropertyName )
column.SortDirection = sd.Direction;