繁体   English   中英

对网格列使用ItemsControl

[英]Use ItemsControl for Grid Columns

<Grid Name="WeightGrid" Grid.RowSpan="2" SnapsToDevicePixels="True"  Grid.ColumnSpan="2" Margin="{Binding WeigntGridMargin}" MouseDown="WeigntGridWrap_OnMouseDown" MouseMove="WeigntGridWrap_OnMouseMove" MouseUp="WeigntGridWrap_OnMouseUp">
      <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="100" />
            ...
      </Grid.ColumnDefinitions>
      <Grid.RenderTransform>
            <TranslateTransform x:Name="weightTT"/>
      </Grid.RenderTransform>
      <StackPanel Grid.Column="0" Width="100" Height="75">
          <StackPanel.Background>
                   <ImageBrush ImageSource="/Size.WPF;component/Assets/ruller-bg.png" />
          </StackPanel.Background>
          <TextBlock Margin="0, 42, 0, 0" Foreground="#c0b6d1" HorizontalAlignment="Center" FontSize="18">0</TextBlock>
       </StackPanel>
       <StackPanel Grid.Column="1" Width="100" Height="75">
              <StackPanel.Background>
                          <ImageBrush ImageSource="/Size.WPF;component/Assets/ruller-bg.png" />
              </StackPanel.Background>
              <TextBlock Margin="0, 42, 0, 0" Foreground="#c0b6d1" HorizontalAlignment="Center" FontSize="18">1</TextBlock>
        </StackPanel>
        <StackPanel Grid.Column="2" Width="100" Height="75">
               <StackPanel.Background>
                          <ImageBrush ImageSource="/Size.WPF;component/Assets/ruller-bg.png" />
               </StackPanel.Background>
               <TextBlock Margin="0, 42, 0, 0" Foreground="#c0b6d1" HorizontalAlignment="Center" FontSize="18">2</TextBlock>
         </StackPanel>
         ...
</Grid>

我有很多列的网格。 每列都是StackPanel具有不同的列号和TextBlock不同编号。 为了减少代码量,我想使用ItemsContol或类似的东西来构建网格列。 但是有一个问题-如何将ColumnNumber绑定到ItemsControl项目? 可能在DataTemplate内有解决方案Bind Grid.Row / Grid.Column但是另一个问题-如何设置列数?


UPD:

<ItemsControl Name="WeightItemsControl" ItemsSource="{Binding Cells}" Grid.RowSpan="2" Grid.ColumnSpan="2">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <Grid Name="WeightGrid"  SnapsToDevicePixels="True" Margin="{Binding WeigntGridMargin}" MouseDown="WeigntGridWrap_OnMouseDown" MouseMove="WeigntGridWrap_OnMouseMove" MouseUp="WeigntGridWrap_OnMouseUp">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                </Grid>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemContainerStyle>
                            <Style>
                                <Setter Property="Grid.Row" Value="{Binding GridRow}" />
                                <Setter Property="Grid.Column" Value="{Binding GridColumn}" />
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                    </ItemsControl>

这就是我现在所拥有的。 但是我仍然不知道如何设置应该为其创建的列数ItemsControl (当前它只是绑定到一些集合Cells但应该只是列数)。

 <ItemsControl Name="icTodoList">
                        <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                        <Grid Margin="0,0,0,5">
                                                <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*" />
                                                        <ColumnDefinition Width="100" />
                                                </Grid.ColumnDefinitions>
                                                <TextBlock Text="{Binding Title}" />
                                                <ProgressBar Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding Completion}" />
                                        </Grid>
                                </DataTemplate>
                        </ItemsControl.ItemTemplate>
                </ItemsControl>

您可以根据需要修改代码..或者,如果不使用绑定,则可以像这样

<ItemsControl>
                        <system:String>ItemsControl Item #1</system:String>
                        <system:String>ItemsControl Item #2</system:String>
                        <system:String>ItemsControl Item #3</system:String>
                        <system:String>ItemsControl Item #4</system:String>
                        <system:String>ItemsControl Item #5</system:String>
                </ItemsControl>

暂无
暂无

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

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