WPF 列表视图加载时排序

Posted

技术标签:

【中文标题】WPF 列表视图加载时排序【英文标题】:WPF List View Sort on Load 【发布时间】:2009-02-26 18:38:49 【问题描述】:

问题来了:

我想在 ListView 首次加载时对其进行排序。 我已经实现了可以在列表视图中排序的功能,如果标题列 在 ListView 中被点击。

我找不到合适的事件来调用我的排序函数。 我尝试使用 UserControl 和 Loaded 事件的 OnInitialized 但似乎列表视图是 当我调用这些函数时没有填充。

我尝试了 ListView 的 GotFocus。它可以工作,但我必须点击窗口才能完成排序。

我希望在加载 ListView 后立即进行排序。

我在列表视图中使用 XML 数据绑定。 ListView 是用户控件的一部分。用户控件托管在 MMC 应用程序中。

如果您需要任何其他信息,请告诉我。

公共类 SortableGridViewColumn : GridViewColumn 公共字符串 SortPropertyName 获取返回(字符串)GetValue(SortPropertyNameProperty); 设置 SetValue(SortPropertyNameProperty, 值);

    // Using a DependencyProperty as the backing store for SortPropertyName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SortPropertyNameProperty =
        DependencyProperty.Register("SortPropertyName", typeof(string), 
        typeof(SortableGridViewColumn), new UIPropertyMetadata(""));

    public bool IsDefaultSortColumn
    
        get  return (bool)GetValue(IsDefaultSortColumnProperty); 
        set  SetValue(IsDefaultSortColumnProperty, value); 
    
    public static readonly DependencyProperty IsDefaultSortColumnProperty =
        DependencyProperty.Register("IsDefaultSortColumn", typeof(bool), 
        typeof(SortableGridViewColumn), new UIPropertyMetadata(false));



public class SortableListView : ListView

    public SortableListView()
                       

         

   SortableGridViewColumn lastSortedOnColumn = null;
   ListSortDirection lastDirection = ListSortDirection.Ascending;   



    public void Sort(string sortBy,  ListSortDirection direction)
            
        ICollectionView dataView = CollectionViewSource.GetDefaultView
            (this.ItemsSource);
        //Check if dataView isn't null
        if (dataView != null)
        
            dataView.SortDescriptions.Clear();
            SortDescription sd1 = new SortDescription("@isenabled", direction);
            dataView.SortDescriptions.Add(sd1);

            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        

    


    private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
    
        GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;

        if (headerClicked != null &&
            headerClicked.Role != GridViewColumnHeaderRole.Padding)
        
             // attempt to cast to the sortableGridViewColumn object.
            SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn;

            // ensure that the column header is the correct type and a sort property has been set.
            if (sortableGridViewColumn != null && !String.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName))
            

                ListSortDirection direction;
                bool newSortColumn = false;

                // determine if this is a new sort, or a switch in sort direction.
                if (lastSortedOnColumn == null
                    || String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName)
                    || !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase))
                
                    newSortColumn = true;
                    direction = ListSortDirection.Ascending;
                
                else
                
                    if (lastDirection == ListSortDirection.Ascending)
                    
                        direction = ListSortDirection.Descending;
                    
                    else
                    
                        direction = ListSortDirection.Ascending;
                    
                

                // get the sort property name from the column's information.
                string sortPropertyName = sortableGridViewColumn.SortPropertyName;

                // Sort the data.
                Sort(sortPropertyName, direction);

                lastSortedOnColumn = sortableGridViewColumn;
                lastDirection = direction;
            
        
    

    protected override void OnInitialized(EventArgs e)       
    
        base.OnInitialized(e);
        // add the event handler to the GridViewColumnHeader. This strongly ties this ListView to a GridView.
        this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(GridViewColumnHeaderClickedHandler));
        // cast the ListView's View to a GridView
        GridView gridView = this.View as GridView;

        if (gridView != null)
        
            // determine which column is marked as IsDefaultSortColumn. Stops on the first column marked this way.1
            SortableGridViewColumn sortableGridViewColumn = null;

            foreach (GridViewColumn gridViewColumn in gridView.Columns)
            
                sortableGridViewColumn = gridViewColumn as SortableGridViewColumn;
                if (sortableGridViewColumn != null)
                
                    if (sortableGridViewColumn.IsDefaultSortColumn)
                    
                        break;
                    
                    sortableGridViewColumn = null;
                
            

            // if the default sort column is defined, sort the data 
            if (sortableGridViewColumn != null)
            
                lastSortedOnColumn = sortableGridViewColumn;
                Sort(sortableGridViewColumn.SortPropertyName, ListSortDirection.Ascending);
            
                   
    



XAML如下图:

    **<local:SortableListView x:Name="ListViewControl" Grid.Row="0"   ItemContainerStyle="DynamicResource StretchedContainerStyle" 
    ItemTemplateSelector="DynamicResource myControlTemplateSelector" 
    IsSynchronizedWithCurrentItem="True"        
    ItemsSource="Binding Source=StaticResource dataProvider,
    XPath=//CONFIGURATION">           
        <ListView.View >
            <GridView >                    
                <local:SortableGridViewColumn  Header="ID" HeaderContainerStyle="StaticResource CustomHeaderStyle" 
        DisplayMemberBinding="Binding XPath=./@id"
        IsDefaultSortColumn="True"
        SortPropertyName="@id"/>
                <local:SortableGridViewColumn  Header="VALUE" HeaderContainerStyle="StaticResource CustomHeaderStyle" 
        CellTemplateSelector="DynamicResource myControlTemplateSelector" 
        SortPropertyName="@value"/>
                <local:SortableGridViewColumn  Header="DATATYPE" HeaderContainerStyle="StaticResource CustomHeaderStyle" 
        DisplayMemberBinding="Binding XPath=./@data_type"
        SortPropertyName="@data_type"/>
                <local:SortableGridViewColumn  Header="DESCRIPTION" HeaderContainerStyle="StaticResource CustomHeaderStyle"  
        DisplayMemberBinding="Binding XPath=./@description"
        SortPropertyName="@description"
        Width="Binding ElementName=ListViewControl, Path=ActualWidth"/>


            </GridView>
        </ListView.View>
    </local:SortableListView>**
    <StackPanel Grid.Row="1">
        <Button Grid.Row="1" HorizontalAlignment="Stretch"  Height="34" HorizontalContentAlignment="Stretch" >
            <StackPanel  HorizontalAlignment="Stretch"  VerticalAlignment="Center"  Orientation="Horizontal"  FlowDirection="RightToLeft" Height="30">
                <Button  Grid.Row="1" Content ="Apply" Padding="0,0,0,0 " Margin="6,2,0,2" Name="btn_Apply" HorizontalAlignment="Right"  VerticalContentAlignment="Center"   HorizontalContentAlignment="Center" Width="132"  IsTabStop="True" Click="btn_ApplyClick"  Height="24" />
            </StackPanel >
        </Button> 
    </StackPanel >

</Grid>  

【问题讨论】:

【参考方案1】:

我终于能够解决它。 我不得不在 ListView ItemSource 上使用转换器。然后在 Convert Function 上对 List 进行排序。

下面是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Threading;
using System.Collections.ObjectModel;

namespace ...MiscellaneousCAESettings

    /// <summary>
    /// Interaction logic for ConfigDataView.xaml
    /// </summary>
    public partial class ConfigDataView : UserControl, IConfigDataViewControl 
    

    ConfigDataViewPresenter _presenter = null;
    public static string _currDataType = "";
    public static string _min = "" ;
    public static string _max = "";        

    public string Min 
    
        get
        
         return _min;
        
        set
        
            _min = value ;
        
    

    public string Max
    
        get
        
            return _max;
        
        set
        
            _max = value;
        
    

    public string CurrDataType 
    
        get
        
            return _currDataType;
        
        set
        
            _currDataType = value;
        
    

    public ConfigDataView()
                
        InitializeComponent();

        //To give the classic windows look
        Uri uri = new Uri("PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component\\themes/classic.xaml", UriKind.Relative);
        this.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);            
    

    private void txtBoxGotFocus(object sender, RoutedEventArgs e)
    
        Min = "" ;
        Max = "" ;

        TextBox txtbox = e.Source as TextBox;            
        this.ListViewControl.SelectedItem = txtbox.DataContext;

        //index         
        int index = this.ListViewControl.Items.IndexOf(this.ListViewControl.SelectedItem);

        System.ComponentModel.ICollectionView dataView = CollectionViewSource.GetDefaultView(this.ListViewControl.ItemsSource);

        object stCurr = (dataView.CurrentPosition ) ;

        //Check if the "data_type" attribute exists
        if (((XmlElement)dataView.CurrentItem).Attributes["data_type"] != null)
        
            CurrDataType = ((XmlElement)dataView.CurrentItem).Attributes["data_type"].Value;
        

        //Check if the "min" attribute exists
        if (((XmlElement)dataView.CurrentItem).Attributes["min"] != null)
        
            Min = ((XmlElement)dataView.CurrentItem).Attributes["min"].Value;
        

        //Check if the "min" attribute exists
        if (((XmlElement)dataView.CurrentItem).Attributes["max"] != null)
        
            Max = ((XmlElement)dataView.CurrentItem).Attributes["max"].Value;
        

    


    #region IConfigDataViewControl Members

    public void LoadRootConfigData(string xmlFileName, string xmlFileContent, string xmlXPath)
    
        try
        
            XmlDocument configFileDoc = new XmlDocument();
            configFileDoc.LoadXml(xmlFileContent);

            XmlDataProvider xmldp = (XmlDataProvider)this.TryFindResource("dataProvider");
            xmldp.Document = configFileDoc;
            if (string.IsNullOrEmpty(xmlXPath))
            
                xmldp.XPath = @"//node()[1]/node()[@value]";
            
            else
            
                xmldp.XPath = xmlXPath;
            

            Binding bnd = new Binding();
            bnd.Source = xmldp;                
            bnd.Converter = new SortList();                 

            ListViewControl.SetBinding(ItemsControl.ItemsSourceProperty, bnd);


        
        catch (Exception ex)
        
            MessageBox.Show(ex.Message);
        
    

    public void LoadCategoryConfigData(string xmlFile, string xmlFileContent, string CategoryNodeName)
    
        try
                        
            XmlDocument configFileDoc = new XmlDocument();
            configFileDoc.LoadXml(xmlFileContent);

            XmlDataProvider xmldp = (XmlDataProvider)this.TryFindResource("dataProvider");
            xmldp.Document = configFileDoc;
            xmldp.XPath = @"//CONTEXT[@id='" + CategoryNodeName + @"']/CONFIGURATION";

            Binding bnd = new Binding();
            bnd.Source = xmldp;
            bnd.Converter = new SortList();
            ListViewControl.SetBinding(ItemsControl.ItemsSourceProperty, bnd);                
         
        catch(Exception ex)
        
            MessageBox.Show(ex.Message);  
                  
            

    public void AttachPresenter(ConfigDataViewPresenter cfgpresenter)
    
        _presenter = cfgpresenter;
    

    #endregion


    private void btn_ApplyClick(object sender, RoutedEventArgs e)
                
        XmlDataProvider odp = (XmlDataProvider)this.TryFindResource("dataProvider");

        XmlDocument configFileDoc = new XmlDocument();
        configFileDoc =odp.Document;

        _presenter.Save(configFileDoc.InnerXml );             
    


public class TextBoxMinMaxValidation : ValidationRule


    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    
        try
        
            //Check for min max string length if it is a "Text" data type
            if (ConfigDataView._currDataType.ToLower() == "text")
            
                int minLength = Convert.ToInt32(ConfigDataView._min);
                int maxLength = Convert.ToInt32(ConfigDataView._max);

                int strLength = value.ToString().Length;
                bool isValidLength = true;

                isValidLength = ((strLength >= minLength) && (strLength <= maxLength));

                if (!isValidLength)
                
                    return new ValidationResult(false, string.Format("The input String Length is out of range. The String Length should be between 0 to 1", minLength, maxLength));
                
                else
                
                    return new ValidationResult(true, null);
                
            

            //Check for min max string length if it is a "Numeric" data type
            if (ConfigDataView._currDataType.ToLower() != "numeric")
            
                return new ValidationResult(true, null);
            

            int min = Convert.ToInt32(ConfigDataView._min);
            int max = Convert.ToInt32(ConfigDataView._max);

            int res ;
            bool isNumber = int.TryParse(value.ToString(), out res);
            bool isValidRange = true;

            if (!isNumber)
            
                return new ValidationResult(false, "The input string is in incorrect format. Should be a Number.");
            

            isValidRange = ((res >= min) && (res <= max));

            if (!isValidRange)
            
                return new ValidationResult(false, string.Format("The input integer is out of range. The number should be between 0 to 1", min, max));
            


        
        catch
        

        

        return new ValidationResult(true, null);
    
    

public class ControlTemplateSelector : DataTemplateSelector

    public const String XML_TAG_DATATYPE = "data_type";
    public const String DATATYPE_DROPDOWN = "Dropdown";
    public const String DATATYPE_BOOLEAN = "Boolean";

    public override DataTemplate SelectTemplate(object item,
      DependencyObject container)
    
        FrameworkElement window = (container as FrameworkElement);
        try
                         
            XmlNode node = (XmlNode)item;
            String dataType = "";
            if (node.Attributes[XML_TAG_DATATYPE] != null)
            
                dataType = (string)node.Attributes.GetNamedItem(XML_TAG_DATATYPE).Value;
            
            if (dataType == DATATYPE_DROPDOWN)
            

                return window.FindResource("dropDownTemplate") as DataTemplate; 
            
            if (dataType == DATATYPE_BOOLEAN)
            
                return window.FindResource("booldropDownTemplate") as DataTemplate; 
            
        
        catch (Exception ex)
        
            MessageBox.Show("Select template Exception" + ex.Message ); 
        
        return window.FindResource("textTemplate") as DataTemplate;
    
 

public class boolConverter : IValueConverter

    public const String XML_TAG_VALUE = "value";
    public const String XML_TAG_ID = "id";

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    
        Boolean boolVal = false;
        try
        
            boolVal = System.Convert.ToBoolean(value);
        
        catch
        
            string strVal = value.ToString();
            int iVal = int.Parse(strVal);
            boolVal = System.Convert.ToBoolean(iVal);
        

        if (boolVal == true)
        
            return 1;
        
        else
        
            return 0;
        


    

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
               
        Boolean boolVal = false;
        try
        
            boolVal = System.Convert.ToBoolean(value);
        
        catch
        
            string strVal = value.ToString();
            int iVal = int.Parse(strVal);
            boolVal = System.Convert.ToBoolean(iVal);
        
        return boolVal;

    


public class SortableGridViewColumn : GridViewColumn

    public string SortPropertyName
    
        get  return (string)GetValue(SortPropertyNameProperty); 
        set  SetValue(SortPropertyNameProperty, value); 
    

    // Using a DependencyProperty as the backing store for SortPropertyName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SortPropertyNameProperty =
        DependencyProperty.Register("SortPropertyName", typeof(string), 
        typeof(SortableGridViewColumn), new UIPropertyMetadata(""));

    public bool IsDefaultSortColumn
    
        get  return (bool)GetValue(IsDefaultSortColumnProperty); 
        set  SetValue(IsDefaultSortColumnProperty, value); 
    
    public static readonly DependencyProperty IsDefaultSortColumnProperty =
        DependencyProperty.Register("IsDefaultSortColumn", typeof(bool), 
        typeof(SortableGridViewColumn), new UIPropertyMetadata(false));



public class SortableListView : ListView

    public SortableListView()
    
        // add the event handler to the GridViewColumnHeader. This strongly ties this ListView to a GridView.
        this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(GridViewColumnHeaderClickedHandler));
         

   SortableGridViewColumn lastSortedOnColumn = null;
   ListSortDirection lastDirection = ListSortDirection.Ascending;

    public void Sort(string sortBy,  ListSortDirection direction)
    

        ICollectionView dataView = CollectionViewSource.GetDefaultView
           (this.ItemsSource);

        //Check if dataView isn't null
        if (dataView != null)
                        
            dataView.SortDescriptions.Clear();

            SortDescription sd1 = new SortDescription("@isenabled", direction);
            dataView.SortDescriptions.Add(sd1);

            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);                                           

            dataView.Refresh();
        

    


    private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
    
        GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;

        if (headerClicked != null &&
            headerClicked.Role != GridViewColumnHeaderRole.Padding)
        
             // attempt to cast to the sortableGridViewColumn object.
            SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn;

            // ensure that the column header is the correct type and a sort property has been set.
            if (sortableGridViewColumn != null && !String.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName))
            

                ListSortDirection direction;                   

                // determine if this is a new sort, or a switch in sort direction.
                if (lastSortedOnColumn == null
                    || String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName)
                    || !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase))
                                        
                    direction = ListSortDirection.Descending;
                
                else
                
                    if (lastDirection == ListSortDirection.Ascending)
                    
                        direction = ListSortDirection.Descending;
                    
                    else
                    
                        direction = ListSortDirection.Ascending;
                    
                

                // get the sort property name from the column's information.
                string sortPropertyName = sortableGridViewColumn.SortPropertyName;

                // Sort the data.
                Sort(sortPropertyName, direction);

                lastSortedOnColumn = sortableGridViewColumn;
                lastDirection = direction;
            
        
           


public class SortList : IValueConverter
     
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
         
        //If the value is null tell binding engine to do nothing
        if (value == null)
        
            return Binding.DoNothing;  
        

        ListCollectionView view = (ListCollectionView)
           CollectionViewSource.GetDefaultView(value);


        SortDescription sort_isdisabled =
            new SortDescription("@isenabled",
                ListSortDirection.Ascending);

        view.SortDescriptions.Add(sort_isdisabled);

        SortDescription sort_id =
            new SortDescription("@id",
                ListSortDirection.Ascending);

        view.SortDescriptions.Add(sort_id);

        return view;
    

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    
        return Binding.DoNothing;
    

XAML 如下: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:.....MiscellaneousCAESettings" Horizo​​ntalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" >

<ControlTemplate x:Key="validationTemplate">
  <DockPanel>
        <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
        <AdornedElementPlaceholder/>
  </DockPanel>
</ControlTemplate>

<Style x:Key="textBoxInError" TargetType="x:Type TextBox">
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="Binding RelativeSource=x:Static RelativeSource.Self,
                        Path=(Validation.Errors)[0].ErrorContent"/>
    </Trigger>
  </Style.Triggers>
</Style>

    <DataTemplate x:Key="textTemplate">
        <TextBox  HorizontalAlignment= "Stretch" 
    IsEnabled="Binding XPath=./@isenabled"        
    Validation.ErrorTemplate="StaticResource validationTemplate"
    GotFocus="txtBoxGotFocus"
    Style="StaticResource textBoxInError">
            <TextBox.Text>
                <Binding XPath="./@value" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>           
                        <local:TextBoxMinMaxValidation>
            <local:TextBoxMinMaxValidation.DataType>
                          <local:DataTypeCheck 
                                Datatype="Binding Source=StaticResource dataProvider, XPath='/[@id=CustomerServiceQueueName]'"/>
                        </local:TextBoxMinMaxValidation.DataType>              
                         <local:TextBoxMinMaxValidation.ValidRange>
            <local:Int32RangeChecker 
                                Minimum="Binding Source=StaticResource dataProvider, XPath=./@min" 
                                Maximum="Binding Source=StaticResource dataProvider, XPath=./@max"/>                                            
             </local:TextBoxMinMaxValidation.ValidRange>
                        </local:TextBoxMinMaxValidation>
                    </Binding.ValidationRules> 
                </Binding >
            </TextBox.Text> 
        </TextBox>
    </DataTemplate>
    <DataTemplate x:Key="dropDownTemplate">
        <ComboBox  Name="cmbBox" HorizontalAlignment="Stretch"
           SelectedIndex="Binding XPath=./@value"
           ItemsSource="Binding XPath=.//OPTION/@value"
           IsEnabled="Binding XPath=./@isenabled"
         />
    </DataTemplate>
<DataTemplate x:Key="booldropDownTemplate">
        <ComboBox  Name="cmbBox" HorizontalAlignment="Stretch"
           SelectedIndex="Binding XPath=./@value,  Converter=StaticResource boolconvert">
            <ComboBoxItem>True</ComboBoxItem>
            <ComboBoxItem>False</ComboBoxItem>                                         
        </ComboBox>
    </DataTemplate>
    <local:ControlTemplateSelector 
        x:Key="myControlTemplateSelector"/>

    <Style x:Key="StretchedContainerStyle" TargetType="x:Type ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="Template" Value="DynamicResource ListBoxItemControlTemplate1"/>
    </Style>
    <ControlTemplate x:Key="ListBoxItemControlTemplate1" TargetType="x:Type ListBoxItem">
        <Border SnapsToDevicePixels="true" x:Name="Bd" Background="TemplateBinding Background" BorderBrush="DynamicResource x:Static SystemColors.ActiveBorderBrushKey" Padding="TemplateBinding Padding" BorderThickness="0,0.5,0,0.5">
            <GridViewRowPresenter SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" VerticalAlignment="TemplateBinding VerticalContentAlignment"/>
        </Border>            
    </ControlTemplate>


    <Style x:Key="CustomHeaderStyle" TargetType="x:Type GridViewColumnHeader">
        <Setter Property="Background" Value="LightGray" />            
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="Padding" Value="2,0,2,0"/>                 
    </Style>

</UserControl.Resources>
<Grid x:Name="GridViewControl" Height="Auto">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="34"/>
    </Grid.RowDefinitions>

    <ListView x:Name="ListViewControl" Grid.Row="0"   ItemContainerStyle="DynamicResource StretchedContainerStyle" 
    ItemTemplateSelector="DynamicResource myControlTemplateSelector" 
    IsSynchronizedWithCurrentItem="True"
    ItemsSource="Binding Source=StaticResource dataProvider, 
    XPath=//CONFIGURATION">           

        <ListView.View >
            <GridView >                    
                <GridViewColumn  Header="ID" HeaderContainerStyle="StaticResource CustomHeaderStyle" DisplayMemberBinding="Binding XPath=./@id"/>
                <GridViewColumn  Header="VALUE" HeaderContainerStyle="StaticResource CustomHeaderStyle" CellTemplateSelector="DynamicResource myControlTemplateSelector" />
                <GridViewColumn  Header="DATATYPE" HeaderContainerStyle="StaticResource CustomHeaderStyle" DisplayMemberBinding="Binding XPath=./@data_type"/>
                <GridViewColumn  Header="DESCRIPTION" HeaderContainerStyle="StaticResource CustomHeaderStyle"  
        DisplayMemberBinding="Binding XPath=./@description" 
        Width="Binding ElementName=ListViewControl, Path=ActualWidth"/>


            </GridView>
        </ListView.View>

    </ListView>
    <StackPanel Grid.Row="1">
        <Button Grid.Row="1" HorizontalAlignment="Stretch"  Height="34" HorizontalContentAlignment="Stretch" >
            <StackPanel  HorizontalAlignment="Stretch"  VerticalAlignment="Center"  Orientation="Horizontal"  FlowDirection="RightToLeft" Height="30">
                <Button  Grid.Row="1" Content ="Apply" Padding="0,0,0,0 " Margin="6,2,0,2" Name="btn_Apply" HorizontalAlignment="Right"  VerticalContentAlignment="Center"   HorizontalContentAlignment="Center" Width="132"  IsTabStop="True" Click="btn_ApplyClick"  Height="24" />
            </StackPanel >
        </Button> 
    </StackPanel >

</Grid>    

【讨论】:

以上是关于WPF 列表视图加载时排序的主要内容,如果未能解决你的问题,请参考以下文章

当列表视图中的扩展器折叠时,无法让 WPF 列表视图折叠

如何通过单击另一个列表视图 WPF 中的元素来刷新列表视图

将核心数据加载到数组中,然后填充表视图,重新排序数组对象不会持久

WPF数据视图学习

删除或插入核心数据后表视图未加载

在运行时将新表重新加载/替换到 WPF DataGrid 中