0

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). Here is the my xaml code :

<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!

kaylum
  • 13,833
  • 2
  • 22
  • 31
CongSyIT
  • 61
  • 6

1 Answers1

1

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.

If you take a look at the doc on GridViewColumn.Width, you'll see two important things:

  1. It's a double, not a GridLength (so no star sizing)
  2. This quote:

The default is NaN, which automatically sizes to the largest column item that is not the column 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.

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:

Simple Solution: Just use ListBox instead.

<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. Except the items will actually size the way you want them to.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
  • thank you @Keith Stein. "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." . it awakened me! – CongSyIT Feb 27 '21 at 05:40