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

I have Grid with many columns. Every column is StackPanel with different column number and with different number in TextBlock. To decrease amount of code I want to use ItemsContol or something like this to build grid columns. But one problem - How can I bind ColumnNumber to ItemsControl Item? Possibly there is solution Bind Grid.Row / Grid.Column inside a DataTemplate But another problem - How can set amount of Columns?


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>

This is what I have now. But I still don't know how to set Amount of columns that should be created to it ItemsControl (Currently it's just binded to some collection Cells but it should be just number as amount of columns).

Community
  • 1
  • 1
demo
  • 6,038
  • 19
  • 75
  • 149

1 Answers1

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

you can modify the code as you want..or if you don't use the binding you can simply do like this

<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>
Sulyman
  • 440
  • 5
  • 14