我有一个listview,其中包含gridcolumns的gridview,我想重新排序除第一列之外的所有列。我怎样才能做到这一点?另外我想保存新列位置。
我的listview代码是:
<Style x:Key="myListViewStyle" TargetType="{x:Type ListView}" x:Shared="False">
<Setter Property="BorderBrush" Value="RoyalBlue" />
<Setter Property="Foreground" Value="Teal" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Margin" Value="20, 60, 20, 50" />
<Setter Property="BorderThickness" Value="4, 4, 4, 4" />
<Setter Property="ListView.View">
<Setter.Value>
<GridView AllowsColumnReorder="True" >
<GridView.Columns>
<GridViewColumn Header="A/A" Width="30" DisplayMemberBinding="{Binding AA}"></GridViewColumn>
<GridViewColumn Header="Price" Width="350" DisplayMemberBinding="{Binding Price}" >
</GridViewColumn>
<GridViewColumn Header="Shop" Width="200" DisplayMemberBinding="{Binding Shop}" >
</GridViewColumn>
</GridView.Columns>
</GridView>
</Setter.Value>
</Setter>
</Style>
我也用这样的代码创建listview:
ListView l1 = new ListView();
l1.Style = Resources["myListViewStyle"] as Style;
答案 0 :(得分:1)
如果它只是您要保留的第一列,请尝试以下操作:
<强> XAML:强>
<GridViewColumnHeader x:Name="FirstColumn" MouseMove="PreventDrag">
代码背后:
public MyWindow()
{
InitializeComponent();
(MyListview.View as GridView).Columns.CollectionChanged += ColumnsReordered;
}
private void PreventDrag(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
e.Handled = true;
}
private void ColumnsReordered(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Move)
{
var columns = sender as GridViewColumnCollection;
if (columns != null)
Dispatcher.BeginInvoke((Action)(() => columns.Move(columns.IndexOf(FirstColumn.Column), 0)));
}
}