WPF Listview,对齐listview项目水平动态不在xaml中 [英] WPF Listview, align listview item horizontal dynamic not in xaml

查看:23
本文介绍了WPF Listview,对齐listview项目水平动态不在xaml中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态对齐列表视图中的列表视图项.

How can i dynamic align the listview items in a listview.

-- 不在 XAML 中 --

-- NOT IN XAML --

lv.View = GridView

GridView.Columns.Add(New GridViewColumn With {.Header = "CrewId", .DisplayMemberBinding = New Binding("CrewId")})
GridView.Columns.Add(New GridViewColumn With {.Header = "FirstName", .DisplayMemberBinding = New Binding("FirstName")})
GridView.Columns.Add(New GridViewColumn With {.Header = "LastName", .DisplayMemberBinding = New Binding("LastName")})
GridView.Columns.Add(New GridViewColumn With {.Header = "Country", .DisplayMemberBinding = New Binding("Country")})
GridView.Columns.Add(New GridViewColumn With {.Header = "Rank", .DisplayMemberBinding = New Binding("Rank")})

Try
    lv.ItemsSource = dsCrew.Tables(0).DefaultView
Catch ex As Exception
    MsgBox(ex.Message)
End Try

推荐答案

您可以简单地使用 Horizo​​ntalContentAlignment 属性来对齐所有 ListViewItem s:

You can simply use the HorizontalContentAlignment property to align all of the ListViewItems:

lv.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;

<小时>

更新>>>


UPDATE >>>

啊,这就是这个解决方案不起作用的原因……你有不止一列.当然,在你展示你的代码之前我不能说.好的,我找到了一种将所有列向右对齐的方法......我知道这不是你想要的,但我会在这里添加它,直到找到更好的解决方案:

Ahh, that's why this solution didn't work... you have more than one column. Of course I couldn't tell that before you showed your code. Ok, I found a way to align all of the columns to the right... I understand that this is not exactly what you're after, but I'll add this here until I find a better solution:

Style style = new Style(typeof(ListViewItem));
style.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty, 
    HorizontalAlignment.Right));
YourListView.ItemContainerStyle = style;

在此期间,您也许可以解决其余的问题.

You may be able to work out the rest in the meantime.

更新 2 >>>

好的,所以我已经在网上进行了很好的搜索,这是我为您找到的最接近的答案...有一篇名为 如何居中和向右在 Tracy Sells 的网站上的 WPF ListView 控件 中对齐 GridViewColumn 内的文本,该网站显示了如何在 XAML 中执行此操作...是的,我知道您希望在代码中使用它,但是您必须执行一些操作在这里工作:

OK, so I've had a good search online and this is the closest that I've come to an answer for you... there is an article called How to center and right align text inside a GridViewColumn in a WPF ListView Control on Tracy Sells' website which shows how to do this in XAML... yes, I know you wanted it in code, but you'll have to do some of the work here:

<ListView 
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <TextBlock Text="MyText" TextAlignment="Center" />   
            </GridViewColumn>
        </GridView>
    <ListView.View>
</ListView>

我们可以修改我的第二部分代码来实现ItemContainerStyle:

We can adapt my second bit of code to implement the ItemContainerStyle:

Style style = new Style(typeof(ListViewItem));
style.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty, 
    HorizontalAlignment.Stretch));
YourListView.ItemContainerStyle = style;

不幸的是,这是我所能做到的,因为我从未真正使用过 ListView.所以现在,您只需要找出如何在相关GridViewColumn 的内容上设置TextAlignment.我注意到在代码中做这种事情没有太多帮助,这仅仅是因为这不是通常的做法..无论哪种方式,祝你找到如何完成拼图的最后一部分.

That's as far as I can go unfortunately, as I've never actually used a ListView. So now, you just need to find out how to set the TextAlignment on the content of the relevant GridViewColumn. I noticed that there is not much help with doing this kind of stuff in code and that is simply because this is not the usual way of doing it. Either way, good luck with finding out how to accomplish the last part of the puzzle.

这篇关于WPF Listview,对齐listview项目水平动态不在xaml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆