使用ItemsControl作为网格列

时间:2016-01-29 10:24:09

标签: c# wpf grid itemscontrol

<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项?可能有解决方案Bind Grid.Row / Grid.Column inside a DataTemplate但另一个问题 - 如何设置列的数量?

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,但它应该只是列数量的数量。)< / p>

1 个答案:

答案 0 :(得分:-1)

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