wrappanel作为itemscontrol的itempanel
-
01-10-2019 - |
题
我仍然在愚弄WPF和学习时学习。现在尝试建立一个动态的控件分组(主要是按钮,但可能包括复选框和其他组件)。
我不知道什么是最好的方法,因此我尝试创建ItemScontrol样式,然后将项目添加到包裹内部的项目中。很快就意识到这些物品不会包裹,因为除非我将其作为ItemShost,否则它们实际上不在包裹内部。像这样:
<Style x:Key="ButtonPanelGroup" TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border CornerRadius="5"
BorderBrush="{StaticResource DarkColorBrush}"
BorderThickness="1"
Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<WrapPanel IsItemsHost="True" FlowDirection="LeftToRight">
<ItemsPresenter />
</WrapPanel>
<ContentPresenter ContentSource="Name" Grid.Row="1" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
请注意,这是一项正在进行的工作,我仍然需要实施许多样式效果。在这里我使用它:
<UniformGrid Rows="1">
<ItemsControl Name="Group1" Style="{StaticResource ButtonPanelGroup}">
<Button>Button1</Button>
<Button>Button2</Button>
<CheckBox>TickBox</CheckBox>
</ItemsControl>
<ItemsControl Name="Group2" Style="{StaticResource ButtonPanelGroup}">
<Button>Button3</Button>
<Button>Button4</Button>
<Button>Button5</Button>
</ItemsControl>
<ItemsControl Name="Group3" Style="{StaticResource ButtonPanelGroup}">
<Button>Button6</Button>
<Button>Button7</Button>
<Button>Button8</Button>
</ItemsControl>
</UniformGrid>
另请注意,这仍然是一项正在进行的工作,因为统一格里德将不是去这里的路,而且利润可能会带来痛苦(是否有重叠的边距?),因此输入会受到赞赏。
现在解决真正的问题。这不起作用,我会遇到错误:
不能将'itempresenter'对象添加到“ wrappanel”中。无法明确修改儿童的集合集合,用作ItemScontrol的ItemSpanel。 ItemScontrol生成面板的子元素。对象“ system.windows.controls.itemspresenter”上的错误。
因此,做这样的事情的最佳方法是什么(希望能够将按钮和其他控件扔入ItemControl,并且阵容Up Une Nive)。将控件放入某种类型和迭代的集合中会更好。
很想做得很好,但是我的WPF技能仍然缺乏。是否有WPF的书籍超出基础知识,并展示了Pro的真正成就?
解决方案
您可能想看看 itemspanel 财产:
获取或设置定义控制项目布局的面板的模板。
例子:
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
您可以按以下方式设置它:
<Style TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
其他提示
不要忘记对线索属性的定义isitemshost =“ true”。否则,您的物品Scontrol不会显示您的物品。
<ListBox ItemsSource="{Binding MyItemsSource}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate >
<WrapPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListBox>
这是Slow DataGrid / Xceed DataGrid和Wrappanel解决方案的另一个简单替代方法。当大量数据或整个表仅用于编辑时,可能会很有用。使用itemscontrol + grid.issharedSizesCope =“ true”
更多信息在这里: https://wpf.2000things.com/tag/issharedsizescope/+关于绩效的ItemScontrol vailutualization: 虚拟化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>