wpf中的listview如何改变GridViewColumn以及Title的水平对齐方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf中的listview如何改变GridViewColumn以及Title的水平对齐方式相关的知识,希望对你有一定的参考价值。

RT,貌似xaml中找不到对应属性
GridViewColumnHeader可以改,但是仍旧无法改变GridViewColumn的水平对齐方式,根本没有对应属性啊

参考技术A DataType.Name 是一个 String.
ComboBoxItem 是一个 Control.

你的例子中, ComboBox 的 SelectedValue 的类型应该对应 ComboBoxItem, 而你企图强制指定它为一个 String, 显然不行.
概念不清 :(

试试这个:
在 根元素加入:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
然后重新写过 ComboBox:
<ComboBox SelectedValue="">
<sys:String>String</sys:String>
<sys:String>Boolean</sys:String>
<sys:String>Byte[]</sys:String>
</ComboBox>

then, you got it.

----
补充说明一下, 我这个与你的 ComboBox 的不同之处:
我的示例中, ComboBox 的每个 Item 是 String. SelectedValue 就是一个 string, 只是这个 String 默认的由 ComboBoxItem 来承载.
你的示例中, ComboBox 的每个 Item 是 ComboBoxItem, 这个 ComboBoxItem 的Content 值是 String. SelectedValue 是一个 ComboBoxItem. (由於它是 ComboBoxItem, 所以 wpf 不会再用一个 ComboBoxItem 来承载它.)

直观一点就是:
<ComboBox x:Name="cb1" SelectedIndex="0">
<ComboBoxItem Content="String"/>
</ComboBox>
<ComboBox x:Name="cb2" SelectedIndex="0">
<sys:String>String</sys:String>
</ComboBox>
MessageBox.Show(cb1.SelectedValue.GetType().ToString());
MessageBox.Show(cb2.SelectedValue.GetType().ToString());
答案应该是:
System.Windows.Controls.ComboBoxItem 和
System.String

Hope help.
参考技术B 首先, 说明你提出该问题的原因: 不了解 wpf 中控件的 style 及 Template, 你可以从
http://www.interact-sw.co.uk/iangblog/2007/02/14/wpfdefaulttemplate 得到一些有用的帮助.

其次, 如果你了解了上述的原因, 你就可以应付这种问题. 因此,解决此问题的方法是 定义一个 Style 或者修改缺省 Template, 由于 设置 Style 比较容易, 因此给出一个途径:
在你的 GridViewColumn可以应用到 的资源范围内(例如在 Window.Resources 中) 设置 Style:
<Window.Resources>
....
<Style TargetType="x:Type GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</Window.Resources>
上面的 style 指定 GridViewColum 的 Header 里的内容水平对齐方式是 Left.
在 Style 标签中,没有指定 x:Key, 因为在此资源范围内,所有 GridViewColumn Header 都用此 Style.

------------
By the way:
tkkmx61 给出的答案盗用我 回答的另一个问题 的答案, 与此问题无关, 在此表示抗议.本回答被提问者采纳

WPF ListView 使用GridView 带有Header 以及点击header排序 sort

ListView:

        <ListView   x:Name="lvFiles" VerticalAlignment="Stretch" Background="Transparent" Width="Auto" AllowDrop="{Binding IsAllowDrop}" Margin="20,0,20,30"
            ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
            ButtonBase.Click="ButtonBaseClickedHandler" ItemContainerStyle="{StaticResource ListViewItemStyle}" ItemsSource="{Binding ContentItemsView}"
            dnd:DragDropHelper.IsDragSource="True"
            local:FileViewStyle.SortDefaultStyle="{StaticResource NormalSortHeaderTemplate}"
            local:FileViewStyle.SortAscStyle="{StaticResource AscSortHeaderTemplate}"
            local:FileViewStyle.SortDescStyle="{StaticResource DescSortHeaderTemplate}">
            <ListView.View>
                <GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeader}">                    
                    <GridViewColumn Header="{StaticResource IDS_NAME}" x:Name="nameColumn"
                        HeaderStringFormat="Name" HeaderTemplate="{StaticResource NormalSortHeaderTemplate}"
                        Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}, Converter={StaticResource AutoListViewColumnWidthConverter}}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate >
                                <DockPanel>
                                    <Image  x:Name="img"  Width="20" Height="20" Source="{Binding Icon,Converter={StaticResource ImgPathToImageConverter}}"
                                        SnapsToDevicePixels="True" UseLayoutRounding="True"
                                        RenderTransformOrigin="0.5,0.5" Margin="0,0,5,0"></Image>
                                    <customControl:EnhancedTextBlock Text="{Binding Name}" VerticalAlignment="Center"  TextTrimming="CharacterEllipsis" />
                                </DockPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn  HeaderStringFormat="Type" HeaderTemplate="{StaticResource NormalSortHeaderTemplate}"
                        Width="80" Header="{StaticResource IDS_TYPE}" local:RangeColumn.MinWidth="80">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate >
                                    <customControl:EnhancedTextBlock Text="{Binding Type}" VerticalAlignment="Center"  TextTrimming="CharacterEllipsis" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn  HeaderStringFormat="Size" HeaderTemplate="{StaticResource NormalSortHeaderTemplate}"
                        Width="80"  Header="{StaticResource IDS_SIZE}"   local:RangeColumn.MinWidth="80">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate >
                                <customControl:EnhancedTextBlock Text="{Binding SizeStr}" VerticalAlignment="Center"  TextTrimming="CharacterEllipsis" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn HeaderStringFormat="Modified" HeaderTemplate="{StaticResource NormalSortHeaderTemplate}"
                        Width="120" Header="{StaticResource IDS_MODIFIED}" local:RangeColumn.MinWidth="120">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate >
                                <customControl:EnhancedTextBlock Text="{Binding LastModifyTimeString}" VerticalAlignment="Center"  TextTrimming="CharacterEllipsis" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
ListViewItemStyle:
        <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListViewItem}}">
            <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
            <Setter Property="Height" Value="22" />
            <EventSetter Event="RequestBringIntoView" Handler="ListViewItem_RequestBringIntoView"></EventSetter>
            <!--<Setter Property="helper:DragSelectionHelper.IsDragSelecting" Value="False"/>-->
            <!--<Style.Triggers>
                <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                    <Setter Property="helper:DragSelectionHelper.IsDragSelecting" Value="True" />
                </Trigger>
            </Style.Triggers>-->
        </Style>

NormalSortHeaderTemplate:

    <DataTemplate x:Key="NormalSortHeaderTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <customControl:EnhancedTextBlock Grid.Column="0" Text="{Binding}"
                VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="Left"
                FontWeight="{TemplateBinding TextBlock.FontWeight}" Foreground="{TemplateBinding TextBlock.Foreground}"/>
        </Grid>
    </DataTemplate>

 

AscSortHeaderTemplate:

    <DataTemplate x:Key="AscSortHeaderTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <customControl:EnhancedTextBlock Grid.Column="0" Text="{Binding}"
                VerticalAlignment="{TemplateBinding VerticalAlignment}"
                FontWeight="{TemplateBinding TextBlock.FontWeight}" Foreground="{TemplateBinding TextBlock.Foreground}" />
            <Path Grid.Column="1" x:Name="arrow" Margin="0,-6,6,0" VerticalAlignment="{TemplateBinding VerticalAlignment}"
                StrokeThickness="1" Fill="#FF4C4A4A"  Data="M 5,10 L 15,10 L 10,5 L 5,10" />
        </Grid>
    </DataTemplate>

 

DescSortHeaderTemplate:

    <DataTemplate x:Key="DescSortHeaderTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <customControl:EnhancedTextBlock Grid.Column="0" Text="{Binding}"  VerticalAlignment="{TemplateBinding VerticalAlignment}"
                FontWeight="{TemplateBinding TextBlock.FontWeight}" Foreground="{TemplateBinding TextBlock.Foreground}" />
            <Path Grid.Column="1" x:Name="arrow" Margin="0,-6,6,0" VerticalAlignment="{TemplateBinding VerticalAlignment}"
                StrokeThickness="1" Fill="#FF4C4A4A" Data="M 5,5 L 10,10 L 15,5 L 5,5" />
        </Grid>
    </DataTemplate>

 

GridViewColumnHeader:

    <Style x:Key="GridViewColumnHeader" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="#646464"/>
        <Setter Property="Background" Value="#f2f2f2" />
        <Setter Property="Height" Value="22" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="GridViewColumnHeader">
                    <Grid Background="{TemplateBinding Background}">
                        <Border Name="HeaderBorder">
                            <ContentPresenter x:Name="HeaderContent" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                VerticalAlignment="Center"
                                Margin="10,0,0,0" RecognizesAccessKey="True"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                ContentTemplate="{TemplateBinding ContentTemplate}">
                            </ContentPresenter>
                        </Border>
                        <Thumb x:Name="PART_HeaderGripper" HorizontalAlignment="Right" Style="{StaticResource GridViewColumnHeaderGripper}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ButtonBaseClickedHandler:

#region sort
        /// <summary>
        /// last sort header
        /// </summary>
        GridViewColumnHeader _lastHeaderClicked = null;

        /// <summary>
        /// last sort direction
        /// </summary>
        ListSortDirection _lastDirection = ListSortDirection.Ascending;

        void ButtonBaseClickedHandler(object sender, RoutedEventArgs e)
        {
            #region sort
            try
            {
                GridViewColumnHeader headerClicked =
                      e.OriginalSource as GridViewColumnHeader;

                ListSortDirection direction;

                if (headerClicked != null)
                {
                    if (headerClicked.Column.HeaderStringFormat == null) return;
                    string header = headerClicked.Column.HeaderStringFormat as string;
                    if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                    {
                        if (headerClicked != _lastHeaderClicked)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else
                        {
                            if (_lastDirection == ListSortDirection.Ascending)
                            {
                                direction = ListSortDirection.Descending;
                            }
                            else
                            {
                                direction = ListSortDirection.Ascending;
                            }
                        }


                        Sort(header, direction);

                        if (direction == ListSortDirection.Ascending)
                        {
                            SetSortStyle(headerClicked.Column, true);
                        }
                        else
                        {
                            SetSortStyle(headerClicked.Column, false);
                        }

                        // Remove arrow from previously sorted header
                        if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
                        {
                            //_lastHeaderClicked.Column.HeaderTemplate = null;
                            _lastHeaderClicked.Column.HeaderTemplate = FileViewStyle.GetSortDefaultStyle(lvFiles);
                        }


                        _lastHeaderClicked = headerClicked;
                        _lastDirection = direction;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            #endregion

        }

        void SetSortStyle(GridViewColumn column, bool isAsc)
        {
            DataTemplate dataTemplate = null;

            if (isAsc)
            {
                dataTemplate = FileViewStyle.GetSortAscStyle(lvFiles);
            }
            else
            {
                dataTemplate = FileViewStyle.GetSortDescStyle(lvFiles);
            }

            if (dataTemplate != null)
            {
                column.HeaderTemplate = dataTemplate;
            }
        }

        void Sort(string sortBy, ListSortDirection direction)
        {
            if (string.IsNullOrEmpty(sortBy)) return;

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

            dataView.SortDescriptions.Clear();
            SortDescription sortByType = new SortDescription("IsFolder", direction);
            dataView.SortDescriptions.Add(sortByType);
            SortDescription sortByProperty = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sortByProperty);
            dataView.Refresh();
        }
        #endregion

 

附上其他细节:

        public FileManageListView()
        {
            InitializeComponent();
            this.lvFiles.MouseDoubleClick += FilesView_MouseDoubleClick;
            lvFiles.KeyDown += LvFiles_KeyDown;
            lvFiles.SelectionChanged += LvFiles_SelectionChanged;
        }

        private void LvFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var removedItem in e.RemovedItems.Cast<FileItemViewModel>())
            {
                removedItem.IsSelected = false;
            }
            foreach (var addedItem in e.AddedItems.Cast<FileItemViewModel>())
            {
                addedItem.IsSelected = true;
            }
            FileManagerViewModel.Instance.RaiseCanExecuteChanged();
        }
        private void Grid_DragOver(object sender, DragEventArgs e)
        {
            try
            {
                var itemViewModel = (sender as Grid).DataContext as FileItemViewModel;
                FileManagerViewModel.Instance.DragOverItem = itemViewModel;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        private void LvFiles_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                var deleteCommand = FileManagerViewModel.Instance.DeleteCommand as ICommand;
                if (deleteCommand.CanExecute(null))
                {
                    deleteCommand.Execute(null);
                }

            }
        }

        private void ListViewItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
        {
            e.Handled = true;
        }

 

以上是关于wpf中的listview如何改变GridViewColumn以及Title的水平对齐方式的主要内容,如果未能解决你的问题,请参考以下文章

删除 WPF ListView/GridView 高亮铬

WPF如何改变GridView字体大小

wpf中的listview如何改变GridViewColumn以及Title的水平对齐方式

WPF ListView 使用GridView 带有Header 以及点击header排序 sort

C#WPF,ListView和GridView,动态列以及数据绑定问题

在列标题单击时使 WPF ListView/GridView 排序的最佳方法?