首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ListView中将列数据居中?

问如何在ListView中将列数据居中?
EN

Stack Overflow用户
提问于 2009-11-02 01:20:53
回答 4查看 4.2K关注 0票数 2

我仍然在学习WPF,但我真的对一些应该非常简单的东西感到困惑。我想要做的是将第三列和第四列的内容居中。当我运行此命令时,列将左对齐:

代码语言:javascript
复制
<ListView Margin="0" x:Name="listMonitoredUrls" AlternationCount="1"
          ItemsSource="{Binding}"  >
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Description" DisplayMemberBinding="{Binding FriendlyDesc}"/>
            <GridViewColumn Header="Url" DisplayMemberBinding="{Binding Url}"/>
            <GridViewColumn Header="Frequency">
                <GridViewColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock Text="{Binding ScanFrequencyMinutes}"
                                   HorizontalAlignment="Center"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Next Scan"  >
                <GridViewColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock Text="{Binding TimeNextScanStr}"
                                   HorizontalAlignment="Center"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

我真的开始喜欢WPF了,但像这样的一些简单的事情似乎真的很难。

EN

回答 4

Stack Overflow用户

发布于 2009-11-02 01:34:59

尝试使用TextAlignment属性而不是HorizontalAlignment -应该可以这样做。据我所知,HorizontalAlignment="Center"会居中你的文本块,而不是其中的文本。

票数 0
EN

Stack Overflow用户

发布于 2009-11-02 03:25:44

这可能不太可能,但我不得不为列表框这样做,因为列表框中的项是由模板定义的。尝试在您的ListView上设置HorizontalContentAlignment="Stretch“。如果我没有设置,这些项只会占用它们所需的空间,并且会保持左对齐。

票数 0
EN

Stack Overflow用户

发布于 2012-02-07 05:40:49

我已经创建了一个解决方案,它在以下常见场景下工作:

代码语言:javascript
复制
<GridViewColumn Header="Some Property" DisplayMemberBinding="{Binding SomeProperty}" />

用户只想要一个包含文本的简单DisplayMemberBinding,而不必指定CellTemplate

新代码使用附加的属性,并变为:

代码语言:javascript
复制
<GridViewColumn Header="Some Property" DisplayMemberBinding="{Binding SomeProperty}" 
                ctrl:GridViewExtensions.IsContentCentered="True" />

附加属性代码:

代码语言:javascript
复制
public static class GridViewExtensions
{
    #region IsContentCentered

    [Category("Common")]
    [AttachedPropertyBrowsableForType(typeof(GridViewColumn))]
    public static bool GetIsContentCentered(GridViewColumn gridViewColumn)
    {
        return (bool)gridViewColumn.GetValue(IsContentCenteredProperty);
    }
    public static void SetIsContentCentered(GridViewColumn gridViewColumn, bool value)
    {
        gridViewColumn.SetValue(IsContentCenteredProperty, value);
    }

    public static readonly DependencyProperty IsContentCenteredProperty = 
        DependencyProperty.RegisterAttached(
            "IsContentCentered",
            typeof(bool), // type
            typeof(GridViewExtensions), // containing type
            new PropertyMetadata(default(bool), OnIsContentCenteredChanged)
            );

    private static void OnIsContentCenteredChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        OnIsContentCenteredChanged((GridViewColumn)d, (bool)e.NewValue);
    }
    private static void OnIsContentCenteredChanged(GridViewColumn gridViewColumn, bool isContentCentered)
    {
        if (isContentCentered == false) { return; }
        // must wait a bit otherwise GridViewColumn.DisplayMemberBinding will not yet be initialized, 
        new DispatcherTimer(TimeSpan.FromMilliseconds(100), DispatcherPriority.Normal, OnColumnLoaded, gridViewColumn.Dispatcher)
        {
            Tag = gridViewColumn
        }.Start();
    }

    static void OnColumnLoaded(object sender, EventArgs e)
    {
        var timer = (DispatcherTimer)sender;
        timer.Stop();

        var gridViewColumn = (GridViewColumn)timer.Tag;
        if (gridViewColumn.DisplayMemberBinding == null)
        {
            throw new Exception("Only allowed with DisplayMemberBinding.");
        }
        var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
        textBlockFactory.SetBinding(TextBlock.TextProperty, gridViewColumn.DisplayMemberBinding);
        textBlockFactory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
        var cellTemplate = new DataTemplate { VisualTree = textBlockFactory };
        gridViewColumn.DisplayMemberBinding = null; // must null, otherwise CellTemplate won't be recognized
        gridViewColumn.CellTemplate = cellTemplate;
    }

    #endregion IsContentCentered

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1657711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档