I have a problem with a wpf toolkit DataGrid
.
我对wpf工具箱DataGrid有一个问题。
I have an ItemsSource
with three columns:
我有一个项目资源,有三个栏目:
FirstName
FirstName
LastName
姓
Address
地址
In the C# codebehind I set the sort direction and which column to sort on like this:
在c# codebehind中,我设置了排序方向和要对哪个列进行排序:
ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
view.SortDescriptions.Clear();
view.SortDescriptions.Add(new SortDescription("LastName", ListSortDirection.Ascending));
view.Refresh();
There is no problem in the actual sorting but there is in the headers visual style. If the user sorts a column by clicking on the header, the visual style changes, but the visual style does not indicate that the column sort description is set programmatically.
在实际的排序中没有问题,但是在header的视觉样式中有问题。如果用户通过单击标题对列进行排序,则视觉样式会发生变化,但视觉样式不会指示以编程方式设置列排序描述。
Why is this, and how can I toggle the header so it will show up as sorted?
为什么会这样,我怎么才能切换页眉让它显示为排序?
2 个解决方案
#1
13
I haven't tried it before, but I would think you could set the SortDirection property of the column.
我以前没有尝试过,但是我认为你可以设置列的SortDirection属性。
int columnIndex = 0;
this.dataGrid1.ColumnFromDisplayIndex(columnIndex).SortDirection =
ListSortDirection.Descending;
#2
1
Below sample will let you sort the data grid using the comboboxes as well as clicking directly on the datagrid.
下面的示例将允许您使用组合框对数据网格进行排序,并直接单击datagrid。
XAML:
XAML:
<Window x:Class="DataGridDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=System"
Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type System:Enum}"
x:Key="SortDirections">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ComponentModel:ListSortDirection" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<StackPanel>
<ComboBox
Name="_columnsComboBox"
ItemsSource="{Binding Path=Columns, ElementName=_dataGrid}"
DisplayMemberPath="Header"
SelectionChanged="OnSort" />
<ComboBox
Name="_sortDirectionsComboBox"
ItemsSource="{Binding Source={StaticResource SortDirections}}"
SelectionChanged="OnSort" />
<Controls:DataGrid
Name="_dataGrid"
ItemsSource="{Binding Path=PeopleData}" />
</StackPanel>
</Window>
Code behind:
背后的代码:
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Windows.Controls;
namespace DataGridDemo
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
PeopleData = new DataTable();
PeopleData.Columns.Add(new DataColumn("Name", typeof(string)));
PeopleData.Columns.Add(new DataColumn("Age", typeof(int)));
var row = PeopleData.NewRow();
row["Name"] = "Sara";
row["Age"] = 25;
PeopleData.Rows.Add(row);
row = PeopleData.NewRow();
row["Name"] = "Bob";
row["Age"] = 37;
PeopleData.Rows.Add(row);
row = PeopleData.NewRow();
row["Name"] = "Joe";
row["Age"] = 10;
PeopleData.Rows.Add(row);
DataContext = this;
}
public DataTable PeopleData { get; private set;}
private void OnSort(object sender, SelectionChangedEventArgs e)
{
if (_sortDirectionsComboBox.SelectedIndex == -1 || _columnsComboBox.SelectedIndex == -1)
{
return;
}
foreach (DataGridColumn dataColumn in _dataGrid.Columns)
{
dataColumn.SortDirection = null;
}
ListSortDirection sortDescription = (ListSortDirection)(_sortDirectionsComboBox.SelectedItem);
DataGridColumn selectedDataColumn = _columnsComboBox.SelectedItem as DataGridColumn;
selectedDataColumn.SortDirection = sortDescription;
ICollectionView view = CollectionViewSource.GetDefaultView(_dataGrid.ItemsSource);
view.SortDescriptions.Clear();
view.SortDescriptions.Add(new SortDescription(selectedDataColumn.Header as string, sortDescription));
view.Refresh();
}
}
}
#1
13
I haven't tried it before, but I would think you could set the SortDirection property of the column.
我以前没有尝试过,但是我认为你可以设置列的SortDirection属性。
int columnIndex = 0;
this.dataGrid1.ColumnFromDisplayIndex(columnIndex).SortDirection =
ListSortDirection.Descending;
#2
1
Below sample will let you sort the data grid using the comboboxes as well as clicking directly on the datagrid.
下面的示例将允许您使用组合框对数据网格进行排序,并直接单击datagrid。
XAML:
XAML:
<Window x:Class="DataGridDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=System"
Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type System:Enum}"
x:Key="SortDirections">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ComponentModel:ListSortDirection" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<StackPanel>
<ComboBox
Name="_columnsComboBox"
ItemsSource="{Binding Path=Columns, ElementName=_dataGrid}"
DisplayMemberPath="Header"
SelectionChanged="OnSort" />
<ComboBox
Name="_sortDirectionsComboBox"
ItemsSource="{Binding Source={StaticResource SortDirections}}"
SelectionChanged="OnSort" />
<Controls:DataGrid
Name="_dataGrid"
ItemsSource="{Binding Path=PeopleData}" />
</StackPanel>
</Window>
Code behind:
背后的代码:
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Windows.Controls;
namespace DataGridDemo
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
PeopleData = new DataTable();
PeopleData.Columns.Add(new DataColumn("Name", typeof(string)));
PeopleData.Columns.Add(new DataColumn("Age", typeof(int)));
var row = PeopleData.NewRow();
row["Name"] = "Sara";
row["Age"] = 25;
PeopleData.Rows.Add(row);
row = PeopleData.NewRow();
row["Name"] = "Bob";
row["Age"] = 37;
PeopleData.Rows.Add(row);
row = PeopleData.NewRow();
row["Name"] = "Joe";
row["Age"] = 10;
PeopleData.Rows.Add(row);
DataContext = this;
}
public DataTable PeopleData { get; private set;}
private void OnSort(object sender, SelectionChangedEventArgs e)
{
if (_sortDirectionsComboBox.SelectedIndex == -1 || _columnsComboBox.SelectedIndex == -1)
{
return;
}
foreach (DataGridColumn dataColumn in _dataGrid.Columns)
{
dataColumn.SortDirection = null;
}
ListSortDirection sortDescription = (ListSortDirection)(_sortDirectionsComboBox.SelectedItem);
DataGridColumn selectedDataColumn = _columnsComboBox.SelectedItem as DataGridColumn;
selectedDataColumn.SortDirection = sortDescription;
ICollectionView view = CollectionViewSource.GetDefaultView(_dataGrid.ItemsSource);
view.SortDescriptions.Clear();
view.SortDescriptions.Add(new SortDescription(selectedDataColumn.Header as string, sortDescription));
view.Refresh();
}
}
}