none
listview跟随窗体高宽变化而变化 RRS feed

答案

  • 我明白了你的意思,参考下面的TabControl中包含ListView的代码,这个代码是可以根据窗体大小自动适应的。

    <Grid>

            <Grid.RowDefinitions>

                <RowDefinition Height="80*" MinHeight="220"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="*" />

            </Grid.RowDefinitions>

     

            <DockPanel Grid.Row="0">

                <TabControl MinHeight="120" DockPanel.Dock="Top">

                    <TabItem Header="AAA">

                        <ListBox Background="Red"></ListBox>

                    </TabItem>

                    <TabItem Header="BBB"/>

                </TabControl>

            </DockPanel>

            <Label Content="Persons" Background="Green" Grid.Row="1"/>

            <Grid Background="Blue" Grid.Row="2"/>

        </Grid>

     


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年2月18日 11:25
    版主

全部回复

  • 你好,

    据我所知,如果你不设置ListView的高度与宽度,默认情况下就是自动适应的。你说的例子你可以参考下面的小例子:

    Xaml

    <Window x:Class="ListView.MainWindow"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            Title="MainWindow" Height="350" Width="525">

        <Grid>

            <ListView ItemsSource="{Binding}" Name="myListView"/>

        </Grid>

    </Window>

    Code-Behind:

    namespace ListView

    {

        public partial class MainWindow : Window

        {

            public MainWindow()

            {

                InitializeComponent();

                ObservableCollection<Object> People = new ObservableCollection<Object>();

                for (int i = 0; i < 50; i++)

                    People.Add(new Person());

                myListView.ItemsSource = People;

            }

        }

     

        public class Person

        {

            private static int count;

            public int ID { get; set; }

            public string Name { get; set; }

            public int Age { get; set; }

            public Person()

            {

                ID = count + 1;

                Name = "Name" + count.ToString();

                Age = 20 + count % 2;

                count++;

            }

        }

    }

    你运行这个就是自动适应窗体啊,如果你的问题没有解决,能够给我一点详细的信息么?

    另一方面,如果你想显示的定义成跟某一个控件的高和宽一样,你可以参考下面的代码,这里我用了Window给你演示如何去写:

    <ListView Width="{Binding ElementName=myWindow, Path=ActualWidth}" Height="{Binding ElementName=myWindow, Path=ActualHeight}"  ItemsSource="{Binding}" Name="myListView"/>

    这样就可以保证两个控件大小是一样的。

     

    Best regards,

     

     


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年2月18日 6:35
    版主
  •   是不是所有的空间不去设置高宽都会随窗体自动变化··我刚仔细的看了下我的程序,我的listview 外面 嵌套TabControl,然后 TabControl 外面嵌套了 Grid  我都没有设置高宽· 但是窗体没有 自适应高度

    <Window x:Class="ToolFrom.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Loaded="Window_Loaded"
        ResizeMode="CanMinimize"
        Background="{DynamicResource WindowBackgroundBrush}"
        Title="TenHoo" Height="620" Width="900" WindowStartupLocation="CenterScreen" WindowStyle="None" Topmost="False" ShowInTaskbar="True" Icon="/ToolFrom;component/IMG/logo.png">
    
      <Window.Resources>
        <DataTemplate x:Key="FileNameDataTemplate">
          <Border Name="bdrIdx" Grid.Column="0">
            <TextBlock Text="{Binding FileName}" />
          </Border>
        </DataTemplate>
        <DataTemplate x:Key="FileSizeDataTemplate">
          <Border Name="bdrIdx" Grid.Column="1">
            <TextBlock Text="{Binding FileSize}" />
          </Border>
        </DataTemplate>
    
        <DataTemplate x:Key="ProgressDataTemplate">
          <Border Name="bdrIdx" Grid.Column="2">
            <TextBlock Text="{Binding Progress}" />
          </Border>
        </DataTemplate>
        <DataTemplate x:Key="CompeltedDataTemplate">
          <Border Name="bdrIdx" Grid.Column="3">
            <TextBlock Text="{Binding Compelted}" />
          </Border>
        </DataTemplate>
    
    
        <DataTemplate x:Key="UsertimeDataTemplate">
          <Border Name="bdrIdx" Grid.Column="4">
            <TextBlock Text="{Binding Usertime}" />
          </Border>
        </DataTemplate>
        <DataTemplate x:Key="FiletypeDataTemplate">
          <Border Name="bdrIdx" Grid.Column="5">
            <TextBlock Text="{Binding Filetype}" />
          </Border>
        </DataTemplate>
    
    
        <DataTemplate x:Key="CreatetimeDataTemplate">
          <Border Name="bdrIdx" Grid.Column="6">
            <TextBlock Text="{Binding Createtime}" />
          </Border>
        </DataTemplate>
     
      </Window.Resources>
      <StackPanel Name="sp1" Margin="0,0,0,0">
      
        <!--标题栏-->
        <Grid Height="30" Margin="0,0,0,0" MouseLeftButtonUp="Window_MouseLeftButtonUp" MouseDown="Window_MouseDown" VerticalAlignment="Center">
         
    
          <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1.5" StartPoint="0.5,0">
              <GradientStop Color="#000000" Offset="0"/>
              <GradientStop Color="Black" Offset="1"/>
            </LinearGradientBrush>
          </Grid.Background>
          <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
            <Image  Margin="8,0,0,0" Source="/IMG/logo.png"></Image>
          <Label Margin="2,4,6,1" FontSize="16">互动世界</Label>
          </StackPanel>
          
          <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
          <Button HorizontalAlignment="Right" Margin="2,4,2,1" Width="30" FontSize="15" Name="btnmin" Click="Button_Click">—</Button>
            <Button HorizontalAlignment="Right" Margin="2,4,2,1" Width="30" FontSize="15" Name="btnmax" Click="Button_Click_1">口</Button>
            <Button HorizontalAlignment="Right" Margin="2,4,15,1" Width="30" FontSize="15" Name="btnclose" Click="btnclose_Click">X</Button>
          </StackPanel>
        </Grid>
     <Border Background="#333333" Height="5"></Border>
        
        <!--选项卡-->
    
    
        <Grid Name="tabgrid" Margin="0,0,0,10">
          <TabControl  VerticalAlignment="Stretch">
             <!--产品库-->
            <TabItem Margin="0,0,0,0"  >
              <TabItem.Header>
                <StackPanel Orientation="Horizontal">
                  <Image Height="22" Width="25" Source="/IMG/png/home.png"></Image>
                  <TextBlock Margin="2,0,0,0" FontSize="15" TextAlignment="Center">产品库</TextBlock>
                </StackPanel>
              
            </TabItem.Header>
    
    
    
            <Grid>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="150" />
                  <ColumnDefinition Width="2" />
                  <ColumnDefinition Width="80*" />
    
    
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions >
                  <RowDefinition Height="52" />
                  <RowDefinition Height="52"/>
                  <RowDefinition Height="52"/>
                  <RowDefinition Height="52"/>
                  <RowDefinition Height="52" />
                  <RowDefinition Height="52"/>
                  <RowDefinition Height="52" />
                  <RowDefinition Height="52" />
              
    
                </Grid.RowDefinitions>
    
                <TextBlock FontSize="12" FontWeight="Bold" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Button Name="tb1" Width="140" Height="50" Click="tb1_Click" >全  部</Button>
                </TextBlock>
                <TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Button Name="tb2" Width="140" Height="50" Click="tb2_Click">手  机</Button>
                </TextBlock>
                <TextBlock FontSize="12" FontWeight="Bold" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                 <Button Name="tb3" Width="140" Height="50" Click="tb3_Click">电  器</Button>
                </TextBlock>
                <TextBlock FontSize="12" FontWeight="Bold" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                  <Button Name="tb4" Width="140" Height="50" Click="tb4_Click">服  装</Button>
                </TextBlock>
                <TextBlock FontSize="12" FontWeight="Bold" Grid.Row="4" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                  <Button Name="tb5" Width="140" Height="50" Click="tb5_Click">环  保</Button>
                </TextBlock>
                <TextBlock FontSize="12" FontWeight="Bold" Grid.Row="5" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                  <Button Name="tb6" Width="140" Height="50" Click="tb6_Click">老人小孩</Button>
                </TextBlock>
    
               
                <StackPanel Margin="0,5,15,0" Height="42" Grid.Row="0" Grid.Column="2" >
                  <StackPanel.Background>
               <LinearGradientBrush EndPoint="0.5,1.5" StartPoint="0.5,0">
                    <GradientStop Color="#000000" Offset="0"/>
                    <GradientStop Color="#FFC8C8C8" Offset="1"/>
              </LinearGradientBrush>
               </StackPanel.Background>
                   
                  <Grid>
                     
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                       <Label Name="lb1"  Margin="30,10,10,10" HorizontalAlignment="Left" FontSize="14" Width="100" >全 部</Label>
                    </StackPanel>
                    
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                      <TextBox Name="tb_select" Margin="230,8,10,0" Width="180" Height="28"></TextBox>
                      <Button Margin="0,8,20,0" HorizontalAlignment="Right" Width="60" FontSize="12" Content="搜索" Click="Button_Click_2"></Button>
                      
                    </StackPanel>
                 </Grid>
                </StackPanel>
    
              <Grid Grid.Row="1" Grid.RowSpan="7" Margin="0,5,18,0" Grid.Column="2">
    
                <ListView Name="lv1" Grid.Column="1" FontSize="15" Grid.Row="1"  ItemsSource="{Binding}">
                  
                    <ListView.View>
                  <GridView >
                      <GridViewColumn Width="30"  >
                      
                        
                        <GridViewColumn.HeaderTemplate>
                          <DataTemplate>
                            <CheckBox Name="procheckall" IsThreeState="False" Content="全选"></CheckBox>
                          </DataTemplate>
    
                        </GridViewColumn.HeaderTemplate>
                        
                        <GridViewColumn.CellTemplate>
    
                          <DataTemplate>
                            <CheckBox Name="procheck" IsThreeState="False"></CheckBox>
                          </DataTemplate>
                        </GridViewColumn.CellTemplate>
                      </GridViewColumn>
                     
                    <GridViewColumn Header="图片">
                        <GridViewColumn.CellTemplate>
                          
                          <DataTemplate>
                            <Border Background="LightGray" Margin="0,5,0,5" Width="101" Height="101">
                               <Image Source="{Binding proimg}" Width="100" Height="100"></Image>
                            </Border>
                          </DataTemplate>
                        </GridViewColumn.CellTemplate>
                      </GridViewColumn>
    
                      <GridViewColumn Header="描述" Width="358" >
                        <GridViewColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Margin="0,5,0,5" Text="{Binding prodescription}" TextWrapping="WrapWithOverflow">
                              
                            </TextBlock>
                          </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        
                      </GridViewColumn>
    
                      <GridViewColumn Header="更新时间" DisplayMemberBinding="{Binding updatetime}" Width="200" >
                        <GridViewColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Margin="0,5,0,5" Text="{Binding updatetime}" >
    
                            </TextBlock>
                          </DataTemplate>
                        </GridViewColumn.CellTemplate>
                      </GridViewColumn>
                    </GridView>
                </ListView.View>
    
    
              </ListView>
              </Grid>
              
            </Grid>
      </TabItem>
              <!--转存-->
            <TabItem Height="35" Width="100" >
              <TabItem.Header>
                <StackPanel Orientation="Horizontal">
                  <Image Height="22" Width="25" Source="/IMG/png/back.png"></Image>
                  <TextBlock Margin="2,0,0,0" FontSize="15" TextAlignment="Center">转 存</TextBlock>
                </StackPanel>
              </TabItem.Header>
    
            </TabItem>
    
    
          </TabControl>
    
        </Grid>
      
      </StackPanel>
      
    </Window>
    
    2011年2月18日 8:24
  • 你好,

    我看了你的Xaml代码,我发现你的ListView放在了一个Grid的单元格里面,你设置了这个单元格的大小,这样ListView是不会超出这个单元格的,所以也就不会适应你窗口的大小。

     


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年2月18日 8:50
    版主
  • 但是那个单元格不设置大小的话 布局不会乱了么?
    2011年2月18日 9:49
  • 我明白了你的意思,参考下面的TabControl中包含ListView的代码,这个代码是可以根据窗体大小自动适应的。

    <Grid>

            <Grid.RowDefinitions>

                <RowDefinition Height="80*" MinHeight="220"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="*" />

            </Grid.RowDefinitions>

     

            <DockPanel Grid.Row="0">

                <TabControl MinHeight="120" DockPanel.Dock="Top">

                    <TabItem Header="AAA">

                        <ListBox Background="Red"></ListBox>

                    </TabItem>

                    <TabItem Header="BBB"/>

                </TabControl>

            </DockPanel>

            <Label Content="Persons" Background="Green" Grid.Row="1"/>

            <Grid Background="Blue" Grid.Row="2"/>

        </Grid>

     


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年2月18日 11:25
    版主