我正在实现一个功能,用于按列值对ListView
中的项目进行排序。
这是我的XAML代码的一部分:
<ListView x:Name="ListView4" SelectedItem="{Binding SelectedRepere}" ItemsSource="{Binding ListeDesReperes}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" MouseDoubleClick="ListView_MouseDoubleClick" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" ContextMenuOpening="ListView4_ContextMenuOpening" SelectionChanged="ListView4_SelectionChanged" Visibility="{Binding Grid4Visible, Converter={StaticResource BoolToVisConverter}}" >
<ListView.ContextMenu>
<ContextMenu x:Name="Context4">
<MenuItem x:Name="Context4MakeLonger" Header="{x:Static p:Resources.MakeLonger}" Click="Make_Longer"/>
<MenuItem x:Name="Context4Search" Header="{x:Static p:Resources.Search}" Click="Search_Detail"/>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView AllowsColumnReorder="true" x:Name="GridView4">
<GridViewColumn DisplayMemberBinding="{Binding Path=ID}" Header="ID" Width="50"/>
<GridViewColumn DisplayMemberBinding= "{Binding Path=Name}" Header="{x:Static p:Resources.Nom}" Width="200">
</GridViewColumn>
<GridViewColumn Header="{x:Static p:Resources.QuantiteUnit}" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Quantite}" TextAlignment="Right"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{x:Static p:Resources.QuantiteTot}" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=TotalQuantity}" TextAlignment="Right"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=Profil}" Header="{x:Static p:Resources.Profil}" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Priorite}" Header="{x:Static p:Resources.Priorite}" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Matiere}" Header="{x:Static p:Resources.Matiere}" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=GroupeProfil}" Header="{x:Static p:Resources.GroupeProfil}" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Longueur}" Header="{x:Static p:Resources.Longueur}" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Longueur, StringFormat={}{0:#000}}" TextAlignment="Right"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{x:Static p:Resources.Weight}" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Poids, StringFormat=N2}" TextAlignment="Right"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="140" Header="{x:Static p:Resources.Avancement}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Rectangle Name="PART_Track" Height="20" Stroke="black" StrokeThickness="1" Fill="{Binding RectangleProgression}" Tag="{Binding Name}" MouseMove="mouseOverProgression">
<Rectangle.ToolTip>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="10"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=ToolTipProgressionTb1}" TextAlignment="Left" HorizontalAlignment="Left"/>
<TextBlock Grid.Column="2" Text="{Binding Path=ToolTipProgressionTb2}" TextAlignment="Right" HorizontalAlignment="Right"/>
</Grid>
</StackPanel>
</Rectangle.ToolTip>
</Rectangle>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
当我想采用绑定列的值时,我使用以下公式:
string header = ((System.Windows.Data.Binding)headerClicked.Column.DisplayMemberBinding).Path.Path;
此代码与我在GridViewColumn
中定义绑定权限(即:Profil,Priorite ...)
问题是,当我在列中有一些CellTemplate
和一个TextBox
时,我想拿DisplayMemberBinding
中的TextBox
。
对于“ Longueur”列,我试图在GridViewColumn
和TextBox
中都设置绑定,它解决了问题,但我认为这不是一个很好的解决方案。
我该怎么做:
header = ((System.Windows.Data.Binding)headerClicked.Column.CellTemplate.TextBox.DisplayMemberBinding).Path.Path;
答案 0 :(得分:1)
这是从TextBlock
中检索绑定路径的方法:
DataTemplate cellTemplate = headerClicked.Column.CellTemplate;
Grid grid = cellTemplate.LoadContent() as Grid;
TextBlock textBlock = grid.Children.OfType<TextBlock>().FirstOrDefault();
string bindingPath = BindingOperations.GetBinding(textBlock, TextBlock.TextProperty).Path.Path;