1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| //Code xaml winform
<Window x:Class="WpfDataGridSortingHeaderCharp.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:WpfDataGridSortingHeaderCharp"
Title="Sort Header Column with ListView" Height="300" Width="457">
<Window.Resources>
<DataTemplate x:Key="HeaderTemplateArrowUp">
<DockPanel>
<TextBlock HorizontalAlignment="Center" Text="{Binding}"/>
<Path x:Name="arrow"
StrokeThickness = "1"
Fill = "gray"
Data = "M 5,10 L 15,10 L 10,5 L 5,10"/>
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="HeaderTemplateArrowDown">
<DockPanel>
<TextBlock HorizontalAlignment="Center" Text="{Binding }"/>
<Path x:Name="arrow"
StrokeThickness = "1"
Fill = "gray"
Data = "M 5,5 L 10,10 L 15,5 L 5,5"/>
</DockPanel>
</DataTemplate>
<ObjectDataProvider
x:Key="myDataSource"
MethodName="GetMyClients"
ObjectType="{x:Type local:MyClientFactory}">
</ObjectDataProvider>
</Window.Resources>
<Grid
DataContext="{Binding Source={StaticResource myDataSource}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="100"></RowDefinition>
<RowDefinition Height="*" ></RowDefinition>
</Grid.RowDefinitions>
<my:DataGrid
Grid.Row="0"
AutoGenerateColumns="False"
Name="DataGrid1"
Height="150"
VerticalAlignment="Center"
AlternationCount="0"
CanUserSortColumns="True"
ItemsSource="{Binding}">
<my:DataGrid.Columns>
<my:DataGridTextColumn
x:Name="Type"
Header="Type"
Binding="{Binding Path=Type}"/>
<my:DataGridTextColumn
x:Name="Name"
Header="Name"
Binding="{Binding Path=Name}"/>
<my:DataGridTextColumn
x:Name="Date"
Header="Date"
Binding="{Binding Path=RaisingDate}"/>
<my:DataGridTextColumn
x:Name="Level"
Header="Level"
Binding="{Binding Path=Level}"/>
</my:DataGrid.Columns>
</my:DataGrid>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="32" ></RowDefinition>
<RowDefinition Height="32*" ></RowDefinition>
<RowDefinition Height="32" ></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="20*"></ColumnDefinition>
<ColumnDefinition Width="60"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.Column="0"
Name="lblRisingDate"
Background="LightYellow"
BorderBrush="Red"
FontSize="12"
FontWeight ="Bold"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Content="Textbox Binde à Col3 (RisingDate) " >
</Label>
<!--TextBox binde à DisplayIndex et permettant de modifier le DisplayIndex
d'un DataGridColum-->
<TextBox
Grid.Row="0"
Grid.Column="2"
Name="txtRisingDate"
Background="LavenderBlush"
BorderBrush="Red"
FontSize="12"
FontWeight ="Bold"
Width="60"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Text="{Binding ElementName=Date,Path=DisplayIndex,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
</TextBox>
<!--ListView binde à la collection Columns en lecture seule
permettant de trier les Headers du DataGrid-->
<ListView
Grid.Row="1"
Grid.ColumnSpan="3"
Name="myListView"
Background="LightSalmon"
Foreground="White"
FontSize="12"
FontWeight ="Bold"
ToolTip="myHeaders"
GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler"
ItemsSource="{Binding ElementName=DataGrid1,Path=Columns}">
<ListView.View>
<GridView>
<GridViewColumn
Header="Header"
Width="100"
DisplayMemberBinding="{Binding Path=Header,Mode=TwoWay}" />
<GridViewColumn
Header="DisplayIndex"
Width="100"
DisplayMemberBinding="{Binding Path=DisplayIndex,Mode=TwoWay}" />
</GridView>
</ListView.View>
</ListView>
<!--Button permettant d'appliquer l'ordre de tri des Headers
effectue par le ListView en ordre de tri des DisplayIndex
des colonnes du DataGrid-->
<Button
Grid.Row="2"
Grid.ColumnSpan="3"
Name="btnTri"
Width="100"
Content="TriHeader"
Click="btnTri_Click">
</Button>
</Grid>
</Grid>
</Window> |
Partager