WrapPanel as ItemPanel for ItemsControl

后端 未结 3 1099
既然无缘
既然无缘 2020-11-28 09:38

Still fooling around with WPF and learning as I go. Trying now to build a dynamic grouping of controls (mostly Buttons but might include CheckBoxes and others).

I ha

相关标签:
3条回答
  • 2020-11-28 10:16

    Here's another simple alternative to slow DataGrid / xceed datagrid and WrapPanel solution. Might be useful when a lot of data or whole table is just for editing. Using ItemsControl + Grid.IsSharedSizeScope="True"

    More info here: https://wpf.2000things.com/tag/issharedsizescope/ + On ItemsControl virutualization for performance: Virtualizing an ItemsControl?

    <Grid Grid.IsSharedSizeScope="True" Margin="0,30,0,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
    
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="50" Width="Auto" SharedSizeGroup="Id" />
                <ColumnDefinition MinWidth="50" Width="Auto" SharedSizeGroup="Time"  />
            </Grid.ColumnDefinitions>
            <Border Grid.Column="0" >
                <TextBlock VerticalAlignment="Center" TextWrapping="NoWrap" Text="Header1" />
            </Border>
            <Border Grid.Column="1" >
                <TextBlock VerticalAlignment="Center" TextWrapping="NoWrap" Text="Header2" />
            </Border>
        </Grid>
    
        <ItemsControl Grid.Row="1" ItemsSource="{Binding RunInstance.ConcentrationGradient.Steps}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition MinWidth="50" Width="Auto" SharedSizeGroup="Id" />
                            <ColumnDefinition MinWidth="50" Width="Auto" SharedSizeGroup="Time" />
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0">
                            <TextBlock VerticalAlignment="Center" TextWrapping="NoWrap" Text="{Binding Index, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
                        </Border>
                         <Border Grid.Column="1">
                            <TextBlock VerticalAlignment="Center" TextWrapping="NoWrap" Text="{Binding Time, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
                        </Border>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
    
    0 讨论(0)
  • 2020-11-28 10:17

    Don't forget definition of clue property IsItemsHost="True". Otherwise your ItemsControl won't show your items.

    <ListBox ItemsSource="{Binding MyItemsSource}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate >
                    <WrapPanel IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ListBox>
    
    0 讨论(0)
  • 2020-11-28 10:19

    You might want to take a look at the ItemsPanel property:

    Gets or sets the template that defines the panel that controls the layout of items.

    Example:

    <ItemsControl>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    And you can set it in a Style as follows:

    <Style TargetType="ItemsControl">
        <Setter Property="ItemsPanel">
          <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    0 讨论(0)
提交回复
热议问题