ItemsPanelTemplate内不能多次设置
时间: 2024-01-03 13:04:00 浏览: 131
WPF Canvas 效果 手风琴效果(AD)
非常抱歉,我的回答有误。正确的做法是在ItemsControl的模板(Template)中添加Thumb元素,而不是在ItemsPanelTemplate中添加。
以下是示例代码,在ItemsControl的模板中添加一个Thumb元素:
```
<Style TargetType="local:CustomFont">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomFont">
<Grid>
<ItemsControl x:Name="itemCtrl"
ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource TemplatedParent}}"
FontSize="20" Foreground="Teal">
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}">
<TextBlock Text="{Binding}" />
</Border>
<Rectangle Height="1" Fill="{Binding Foreground, ElementName=itemCtrl}" Margin="0,0,0,5">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Thumb Width="20" Height="20" DragDelta="Thumb_DragDelta" Background="Gray" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
在代码中,Thumb元素被添加到了Grid控件中,并设置了水平和垂直对齐方式。Thumb的DragDelta事件仍然绑定到了名为Thumb_DragDelta的方法中。
请注意,这里的Style目标类型是local:CustomFont,而不是ItemsControl。这是因为我们要为自定义控件CustomFont添加Thumb元素,而不是为所有ItemsControl添加Thumb元素。
阅读全文