【问题标题】:Uniform Grid control with multiple Items C# WPF具有多个项目的统一网格控件 C# WPF
【发布时间】:2014-04-07 02:11:12
【问题描述】:

我需要一个像下面这样的窗口;

我试图实现这一目标,但无法获得 100% 的结果。我哪里做错了?

    <UniformGrid>
    <ItemsControl ItemsSource="{Binding Data}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
           <Border  BorderBrush="Black" Background="Gainsboro" BorderThickness="3" Margin="2" Width="100" >  
              <Grid FlowDirection="LeftToRight">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <Label HorizontalAlignment="Center" Content="{Binding Label1Text}" Grid.Row="1" Margin="2"/>

                <Label HorizontalAlignment="Center" Content="{Binding Label2Text}" Grid.Row="2" Margin="2"/>

                <Button HorizontalAlignment="Center" Content="Button1" Width="80" Height="80"
                        Command="{Binding DataContext.Command1, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                        CommandParameter="{Binding}"
                        Grid.Row="0"  Margin="2"/>
            </Grid>
        </Border>

        </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.Template>
            <ControlTemplate TargetType="ItemsControl">
                <ScrollViewer CanContentScroll="True">
                    <ItemsPresenter/>
                </ScrollViewer>
            </ControlTemplate>
    </ItemsControl.Template>

   </ItemsControl>
  </UniformGrid>

结果窗口;

【问题讨论】:

    标签: c# wpf uniformgrid


    【解决方案1】:

    尝试将ItemsPanel 模板更改为UniformGrid,而不是将ItemsControl 包装在UniformGrid 中。您也可以尝试使用WrapPanel 而不是UniformGrid

    .......
    <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                    <UniformGrid Columns="2"/>
                    <!--  <WrapPanel/> -->
            </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    .......
    

    【讨论】:

    • 我没听明白,我必须把“”改成“”吗??
    • 我的意思是,除了你已经拥有的&lt;ItemsControl.ItemTemplate&gt; 之外,尝试像上面那样设置&lt;ItemsControl.ItemsPanel&gt;
    • WrapPanel 不允许您设置列数
    【解决方案2】:

    您需要指定列数:

    <UniformGrid Columns="2">
    ....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-25
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      相关资源
      最近更新 更多