2

当我尝试将x:Name属性添加到自定义用户控件时遇到问题。这是用户控件的代码,ListView当用户单击列标题时,它会覆盖项目以提供排序:

ListViewSorter.xaml

<ListView x:Class="GenericProjectLibrary.Helpers.ListViewSorter"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ListView.Resources>

        <Style TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="HorizontalContentAlignment"
                    Value="Left" />
        </Style>

        <Style TargetType="Grid" x:Key="HeaderGridStyle">
            <Setter Property="Height" Value="20" />
        </Style>

        <Style TargetType="TextBlock" x:Key="HeaderTextBlockStyle">
            <Setter Property="Margin" Value="5,0,0,0" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>

        <Style TargetType="Path" x:Key="HeaderPathStyle">
            <Setter Property="StrokeThickness" Value="1" />
            <Setter Property="Fill" Value="Gray" />
            <Setter Property="Width" Value="20" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="Margin" Value="5,0,5,0" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
        </Style>

        <DataTemplate x:Key="HeaderTemplateDefault">
            <Grid Style="{StaticResource HeaderGridStyle}">
                <TextBlock Text="{Binding }" Style="{StaticResource HeaderTextBlockStyle}" />
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="HeaderTemplateArrowUp">
            <Grid Style="{StaticResource HeaderGridStyle}">
                <Path Data="M 7,3 L 13,3 L 10,0 L 7,3" Style="{StaticResource HeaderPathStyle}" />
                <TextBlock Text="{Binding }" Style="{StaticResource HeaderTextBlockStyle}" />
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="HeaderTemplateArrowDown">
            <Grid Style="{StaticResource HeaderGridStyle}">
                <Path Data="M 7,0 L 10,3 L 13,0 L 7,0"  Style="{StaticResource HeaderPathStyle}" />
                <TextBlock Text="{Binding }" Style="{StaticResource HeaderTextBlockStyle}" />
            </Grid>
        </DataTemplate>

    </ListView.Resources>

    <ListView.View>
        <GridView></GridView>
    </ListView.View>
</ListView>

ListViewSorter.xaml.cs

namespace GenericProjectLibrary.Helpers
{
    /// <summary>
    /// Interaction logic for ListViewSorter.xaml
    /// Original article: https://stackoverflow.com/questions/994148/best-way-to-make-wpf-listview-gridview-sort-on-column-header-clicking
    /// Auto-Generate Columns: https://social.technet.microsoft.com/wiki/contents/articles/17635.the-equivalent-to-autogeneratecolumns-for-a-listviewgridview.aspx
    /// </summary>
    public partial class ListViewSorter : ListView
    {
        #region Initialization & Cleanup

        GridViewColumnHeader _lastHeaderClicked = null;

        /// <summary>
        /// Default ctor
        /// </summary>
        public ListViewSorter()
        {
            InitializeComponent();
            this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(GridViewColumnHeaderClicked));
        }

        #endregion Initialization & Cleanup

       [snip]
    }
}

我正在尝试这样实现ListViewSorter

<Window x:Class="GenericProjectLibrary.Views.PayrollDatabaseMappingWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:h="clr-namespace:GenericProjectLibrary.Helpers"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d"
        SizeToContent="WidthAndHeight">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\Dictionary\StylesDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
    </Window.Resources>

    <StackPanel Name="StackPanel_PayrollDatabaseMappingGrid">

        <h:ListViewSorter ItemsSource="{Binding Model.DatabaseInfo.PayrollDatabaseMappings, UpdateSourceTrigger=PropertyChanged}"
                          Margin="10"                          
                          ScrollViewer.CanContentScroll="True">
            <ListView.View>
                <GridView>

                [snip]

                </GridView>
            </ListView.View>
        </h:ListViewSorter>


        <StackPanel Name="StackPanel_AddEditDeleteButtons"
                    Orientation="Horizontal"
                    HorizontalAlignment="Right"
                    Margin="5">

            <!-- 'Add Payroll Database Mapping' button -->
            <Button Name="AddPayrollDatabaseMappingsButton"
                    Content="Add Database Mapping"
                    Width="150"
                    HorizontalAlignment="Right"
                    Margin="5">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <ei:CallMethodAction TargetObject="{Binding}"
                                                     MethodName="AddPayrollDatabaseMappingButtonClicked" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>

            <!-- 'Edit Payroll Database Mapping' button -->
            <Button Name="EditPayrollDatabaseMappingsButton"
                    Content="Edit Database Mapping"
                    Width="150"
                    HorizontalAlignment="Right"
                    Margin="5"
                    IsEnabled="{Binding ElementName=ListView_PayrollDatabaseMappings, Path=SelectedItems.Count,
                                        Converter={StaticResource EqualToBooleanConverter}, ConverterParameter=1}">
                 <i:Interaction.Triggers>
                     <i:EventTrigger EventName="Click">
                         <ei:CallMethodAction TargetObject="{Binding}"
                                                     MethodName="EditPayrollDatabaseMappingButtonClicked" />
                     </i:EventTrigger>
                 </i:Interaction.Triggers>
             </Button>

             <!-- 'Delete Payroll Database Mapping' button -->
             <Button Name="DeletePayrollDatabaseMappingsButton"
                     Content="Delete Database Mapping"
                     Width="150"
                     HorizontalAlignment="Right"
                     Margin="5"
                     IsEnabled="{Binding ElementName=ListView_PayrollDatabaseMappings, Path=SelectedItems.Count,
                                        Converter={StaticResource GreaterThanEqualToBooleanConverter}, ConverterParameter=1}">
                  <i:Interaction.Triggers>
                      <i:EventTrigger EventName="Click">
                          <ei:CallMethodAction TargetObject="{Binding}"
                                                     MethodName="DeletePayrollDatabaseMappingButtonClicked" />
                      </i:EventTrigger>
                  </i:Interaction.Triggers>
             </Button>

         </StackPanel>    
     </StackPanel>
</Window>

我正在尝试根据在ListViewSorter. 为此,我尝试绑定到ListViewSorter按元素名称,但是当我向其中添加Name属性时,ListViewSorter我得到一个编译错误Because 'ListViewSorter' is implemented in the same assembly, you must set the x:Name attribute rather than the Name attribute。当我进行此更改时,x:Name我得到了错误The type name 'Helpers' does not exist in the type 'GenericProjectLibrary'

我在 StackOverflow 上看到过类似的问题,但这些都与命名空间与视图/文件名相同的问题有关。我不相信这是这里的问题。

4

0 回答 0