user2980291
user2980291

Reputation: 13

WPF - Using grid in listbox item

To begin with I am beginner to WPF, so please explain it with simple words.

I want to make custom listview item with quite a lot of elements. I decided to use grid to align it nicely. Unfortunately when i tried to see if everything is resizing ok, the background(listview item) resizes while grid doesnt. I think it is because grid isnt properly attached to listview item. So question is how can i attach grid to listview item so I can scale content properly?

Here is code (its a bit messy because of my tries to resolve problem):

<Window
    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" mc:Ignorable="d" x:Class="SurfManager.MainWindow"
    Title="MainWindow" Height="503" Width="933">
<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
</Window.Triggers>
<Grid>
    <TabControl>
        <TabItem Header="Obecne Wypożyczenia" Background="White" Foreground="Black" BorderBrush="#FF8C8E94" OpacityMask="White">
            <Grid Background="#FFE5E5E5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <ListView BorderThickness="0" Height="440">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn/>
                        </GridView>
                    </ListView.View>
                    <ListViewItem>
                        <ListViewItem.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF2AD618" Offset="0"/>
                                <GradientStop Color="#FF6CFF5D" Offset="0.582"/>
                            </LinearGradientBrush>
                        </ListViewItem.Background>
                        <Grid Height="82.96" Width="859.733">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="41*"/>
                                <RowDefinition Height="42*"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="0*"/>
                                <ColumnDefinition Width="180*"/>
                                <ColumnDefinition Width="283*"/>
                                <ColumnDefinition Width="387*"/>
                                <ColumnDefinition Width="140*"/>
                            </Grid.ColumnDefinitions>
                            <Label Content="Imię:"  Margin="8.001,10,0,38" Grid.RowSpan="2" Grid.Column="1" Grid.ColumnSpan="2" />
                            <Label Content="Nazwisko:"  Margin="8.001,5.451,0,3.529" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
                            <Label Content="Początek:" HorizontalAlignment="Left" Margin="0.978,10,0,0" VerticalAlignment="Top" Height="27.96" Width="59.733" Grid.Column="2"/>
                            <Label Content="Koniec:" HorizontalAlignment="Left" Margin="0.978,1.92,0,0" VerticalAlignment="Top" Height="33" Grid.Row="1" Width="48.107" Grid.Column="2"/>
                            <Label Content="Pozostało" HorizontalAlignment="Left" Margin="37,10,0,0" VerticalAlignment="Top" Height="25.96" Width="61.79" Grid.Column="3"/>
                            <Label Content="Cena:" HorizontalAlignment="Left" Margin="37,8.96,0,0" VerticalAlignment="Top" Grid.Row="1" Height="25.96" Width="39.204" Grid.Column="3"/>
                            <Label Content="Deska" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,5.02,0,0" Height="25.96" Width="41.853" Grid.Column="4"/>
                            <Label Content="Żagiel" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="9.746,6.02,0,0" Height="25.96" Width="42.107" Grid.Column="4" Grid.Row="1"/>
                        </Grid>
                    </ListViewItem>
                </ListView>
            </Grid>
        </TabItem>
        <TabItem Header="Baza Klientow">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
        <TabItem Header="Administracja sprzętem" HorizontalAlignment="Left" Height="19.96" VerticalAlignment="Top" Width="144.32" Margin="-2,-2,-7,0">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
    </TabControl>
</Grid>
</Window>

Upvotes: 1

Views: 2607

Answers (2)

Rang
Rang

Reputation: 1372

ListView is often used like this:

use binding:

<ListView BorderThickness="0" >
      <ListView.View>
              <GridView>
                 <GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding Name}"/>
                 <GridViewColumn Header="Address" Width="200" DisplayMemberBinding="{Binding Address}"/>
               </GridView>
</ListView.View>

or like this:

 <ListView BorderThickness="0" >
     <ListViewItem>
          <Grid >
             ....
          </Grid>
      </ListViewItem>
  </ListView>

are you sure you need set both ListView.View and ListViewItem ? It will looks like : enter image description here

btw,you should not set grid's width and height,most important thing is your grid's content looks confused. you must set those lables in correct Grid.Col or Grid.Row, why you set ColumnSpan/RowSpan ? If you are sure it's correct?

Upvotes: 1

pushpraj
pushpraj

Reputation: 13679

here is what you need

add this style resource in your list view and remove width from your grid.

    <ListView BorderThickness="0"
                Height="440">
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.Resources>

by modifying default teplate from list view item will make the content re sizable

also remove the following as not necessary

<ListView.View>
    <GridView>
        <GridViewColumn/>
    </GridView>
</ListView.View>

Upvotes: 2

Related Questions