简体   繁体   English

如何在 wpf 中的 listView(无标题)中右对齐数据?

[英]How to Right-align the data in listView (no header) in wpf?

I have 1 listview with 1 column and no header, how the data is displayed to the right of the column, I have referenced this here , but it is ineffective in my project (sorry my english is not very good).我有 1 个列表视图,1 列,没有 header,数据如何显示在列的右侧,我在这里引用了这个,但在我的项目中无效(对不起,我的英语不是很好)。 Here is the my xaml code:这是我的 xaml 代码:

<Window x:Class="ListViewWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ListViewWPF"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Window.Resources>
    <Style x:Key="RedTextBoxStyle" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="true">
                <Setter Property="Background" Value="RosyBrown"/>
                <Setter Property="IsReadOnly" Value="True"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition ></ColumnDefinition>
        <ColumnDefinition ></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <ListView ItemsSource="{Binding Mydata}" SelectedItem="{Binding seleItems}" Grid.RowSpan="3" Grid.ColumnSpan="2">
        <ListView.Resources>
            <Style TargetType="GridViewColumnHeader">
                <Setter Property="Visibility" Value="Collapsed" />
            </Style>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Value1}" TextAlignment="Right"></TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    <Button Grid.Row="0" Grid.Column="2" Margin="20" Content="1" FontSize="25"></Button>
    <Button Grid.Row="1" Grid.Column="2" Margin="20" Content="2" FontSize="25"></Button>
</Grid>
</Window>

I hope to receive your comments!我希望收到您的意见!

Ironically, there's a whole MSDN article on this subject - How to: Change the Horizontal Alignment of a Column in a ListView - and yet they leave out a very important piece of information.具有讽刺意味的是,有一篇关于这个主题的完整 MSDN 文章 - How to: Change the Horizontal Alignment of a Column in a ListView - 但是他们遗漏了一条非常重要的信息。

If you take a look at the doc on GridViewColumn.Width , you'll see two important things:如果您查看GridViewColumn.Width上的文档,您会看到两件重要的事情:

  1. It's a double , not a GridLength (so no star sizing)它是double ,而不是GridLength (所以没有星号)
  2. This quote:这段话:

The default is NaN, which automatically sizes to the largest column item that is not the column header.默认值为 NaN,它会自动调整为不是 header 列的最大列项。

So even though your items are stetching to fill the column, your column is only as wide as your longest item, not the width of the whole ListView .因此,即使您的项目正在填充列,您的也仅与最长项目一样宽,而不是整个ListView的宽度。

Now, you could do something like in this answer with code, or you would try binding to ListView.ActualWidth , but really, there's a much simpler solution:现在,您可以使用代码在此答案中执行类似的操作,或者尝试绑定到ListView.ActualWidth ,但实际上,有一个更简单的解决方案:

Simple Solution: Just use ListBox instead.简单的解决方案:只需使用ListBox代替。

<ListBox ItemsSource="{Binding Mydata}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
        
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value1}" HorizontalAlignment="Right"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

A ListBox is very similar- if not identical- to a single-column ListView with no header. ListBox与没有 header 的单列ListView非常相似(如果不相同)。 Except the items will actually size the way you want them to.除了这些项目实际上会按照您想要的方式调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM