<ControlTemplate.Triggers>用法
时间: 2023-06-08 15:08:55 浏览: 44
<ControlTemplate.Triggers> 元素表示在控件模板中指定的触发器集合。这些触发器在控件模板被应用时启动,并且允许你在控件状态更改时对控件应用样式。例如,你可以在控件模板中使用触发器来更改控件的背景颜色或文本颜色,以响应控件的状态更改。
相关问题
<ItemsControl x:Class="HDSProject.Control.CustomFont" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HDSProject.Control" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="itemCtrl" ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}" FontSize="20" Foreground="Teal" > <ItemsControl.ItemContainerStyle> <Style TargetType="ContentPresenter"> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/> </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
好的,可以使用Thumb来实现拖动自定义控件的功能。具体步骤如下:
1. 在自定义控件的XAML文件中添加Thumb控件。
```xaml
<ItemsControl x:Class="HDSProject.Control.CustomFont" ...>
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Grid>
<Thumb x:Name="PART_Thumb" DragDelta="Thumb_DragDelta" />
...
</Grid>
</ControlTemplate>
</ItemsControl.Template>
...
</ItemsControl>
```
2. 在自定义控件的代码文件中添加Thumb_DragDelta事件处理程序。
```csharp
public class CustomFont : ItemsControl
{
private Thumb _thumb;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_thumb = GetTemplateChild("PART_Thumb") as Thumb;
}
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Canvas.SetLeft(this, Canvas.GetLeft(this) + e.HorizontalChange);
Canvas.SetTop(this, Canvas.GetTop(this) + e.VerticalChange);
}
}
```
3. 在Thumb_DragDelta事件处理程序中实现自定义控件的拖动功能。您可以使用Canvas.SetLeft和Canvas.SetTop方法来设置自定义控件的位置。
希望这个解决方案能够解决您的问题。
xcteed中AnchorablePaneTabPanel的用法
AnchorablePaneTabPanel 是 Xceed.Wpf.AvalonDock 控件库中的一个控件,用于在 DockingManager 中显示 AnchorablePane 的标签页。
使用 AnchorablePaneTabPanel 的步骤如下:
1. 将 AnchorablePaneTabPanel 添加到 DockingManager 的 AnchorablePaneStyle 属性中:
```xml
<xcad:DockingManager>
<xcad:DockingManager.AnchorablePaneStyle>
<Style TargetType="{x:Type xcad:AnchorablePane}">
<Setter Property="Title" Value="{Binding Model.Title}" />
<Setter Property="xcad:DockingManager.ShowHeader" Value="True" />
<Setter Property="xcad:DockingManager.ShowCloseButton" Value="True" />
<Setter Property="xcad:DockingManager.ShowMaximizeButton" Value="False" />
<Setter Property="xcad:DockingManager.ShowMinimizeButton" Value="False" />
<Setter Property="xcad:DockingManager.IconSource" Value="{Binding Model.IconSource}" />
<Setter Property="xcad:DockingManager.ControlStyle">
<Setter.Value>
<Style TargetType="{x:Type xcad:LayoutAnchorableControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type xcad:LayoutAnchorableControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<xcad:AnchorablePaneTabPanel Grid.Row="0" />
<ContentPresenter Grid.Row="1"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
</xcad:DockingManager.AnchorablePaneStyle>
</xcad:DockingManager>
```
2. 在 AnchorablePane 中添加 AnchorableContent,并设置 Title 和 Content 属性:
```xml
<xcad:DockingManager>
...
<xcad:LayoutRoot>
<xcad:LayoutPanel Orientation="Horizontal">
<xcad:LayoutAnchorablePane DockWidth="200">
<xcad:LayoutAnchorable>
<xcad:LayoutAnchorable.Title>
<TextBlock Text="My Anchorable" />
</xcad:LayoutAnchorable.Title>
<xcad:LayoutAnchorable.Content>
<TextBlock Text="I am an Anchorable." />
</xcad:LayoutAnchorable.Content>
</xcad:LayoutAnchorable>
</xcad:LayoutAnchorablePane>
...
</xcad:LayoutPanel>
</xcad:LayoutRoot>
</xcad:DockingManager>
```
3. 在 AnchorablePaneTabPanel 中使用模板绑定来设置 TabItem 的样式:
```xml
<xcad:AnchorablePaneTabPanel>
<xcad:AnchorablePaneTabPanel.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="Border"
BorderThickness="1,1,1,0"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
Margin="0,0,-4,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,2,12,2"
RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource ActiveTabBackgroundBrush}" />
<Setter Property="Foreground" Value="{StaticResource ActiveTabForegroundBrush}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource HoverTabBackgroundBrush}" />
<Setter Property="Foreground" Value="{StaticResource HoverTabForegroundBrush}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="False" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Border" Property="Background" Value="{StaticResource HoverTabBackgroundBrush}" />
<Setter Property="Foreground" Value="{StaticResource HoverTabForegroundBrush}" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</xcad:AnchorablePaneTabPanel.ItemContainerStyle>
</xcad:AnchorablePaneTabPanel>
```
其中,ActiveTabBackgroundBrush、ActiveTabForegroundBrush、HoverTabBackgroundBrush、HoverTabForegroundBrush 是自定义的 Brush,用于设置不同状态下 TabItem 的背景和前景色。