我想对我的树视图的第二级进行排序。排序取决于两个不同的条件,具体取决于字段类型和顺序属性。就我而言,订单可能会因用户而异。用户使用向上和向下箭头按钮。如果添加或删除了项目并且用户更改了 Order 属性,如何保留列表排序?
我当前的树视图:
<TreeView Grid.Row="0" x:Name="tvImages" ItemsSource="{Binding SelectedTemplate.Images}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type entities:TemplateImageDTO}" ItemsSource="{Binding FieldSections}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Order, StringFormat=Image {0}}" Margin="0,3,0,0"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type entities:FieldSectionDTO}">
<DockPanel LastChildFill="False" HorizontalAlignment="Stretch" Width="Auto">
<StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
<TextBlock Text="{Binding FieldType, Converter={StaticResource enumConverter}}" Margin="0,3,0,0" />
<TextBlock Text=" - Field " Margin="0,3,0,0" />
<TextBlock Text="{Binding Order, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,3,0,0" />
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Right" Margin="0,0,10,0">
<Button Width="24" Height="24" Style="{DynamicResource MahApps.Metro.Styles.MetroCircleButtonStyle}">
<iconPacks:PackIconFontAwesome Kind="AngleUpSolid" Foreground="{DynamicResource AccentColorBrush}" />
</Button>
<Button Width="24" Height="24" Margin="10,0,0,0" Style="{DynamicResource MahApps.Metro.Styles.MetroCircleButtonStyle}">
<iconPacks:PackIconFontAwesome Kind="AngleDownSolid" Foreground="{DynamicResource AccentColorBrush}" />
</Button>
</StackPanel>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
我在这里找到了我的解决方案:https://stackoverflow.com/a/19363888/6751312
用户单击向上和向下箭头按钮后,我增加和减少订单属性并重新调整列表。