MahApps ToggleSwitch 自定义 - 在 'System.Windows.Controls.Border' 的名称范围内找不到 'KnobTranslateTransform' 名称
MahApps ToggleSwitch customize - 'KnobTranslateTransform' name cannot be found in the name scope of 'System.Windows.Controls.Border'
我会从 MahApps 自定义 ToggleSwitch。为此,我创建了一个项目并使用 Visual Blend 2019 打开它。
我只是做了:
- 右键单击 ToggleSwitch。
- Select“编辑模板”
- Select“编辑副本”
很遗憾,我得到:“'KnobTranslateTransform' 名称无法在 'System.Windows.Controls.Border' 的名称范围内找到。”
我是不是遗漏了一些参考资料?或者我是否必须使用一些额外的东西来设置 KnobTranslateTransform?
感谢您的帮助。
这是我的参考资料:
Solution Explorer
这是我的 xaml 文件:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ToggleAirCustomSwitch"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="ToggleAirCustomSwitch.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="ToggleSwitchAirStyle" TargetType="{x:Type mah:ToggleSwitch}">
<Setter Property="ContentDirection" Value="RightToLeft"/>
<Setter Property="FontFamily" Value="{DynamicResource MahApps.Fonts.Family.ToggleSwitch}"/>
<Setter Property="FontSize" Value="{DynamicResource MahApps.Font.Size.ToggleSwitch}"/>
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContentForeground}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type mah:ToggleSwitch}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding mah:ControlsHelper.CornerRadius}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ToggleStates">
<VisualStateGroup.Transitions>
<VisualTransition From="Dragging" GeneratedDuration="0" GeneratedEasingFunction="{x:Null}" To="On">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="24">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase Exponent="9"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobBounds">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OuterBorder">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOn">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOff">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualTransition>
<VisualTransition From="Dragging" GeneratedDuration="0" GeneratedEasingFunction="{x:Null}" To="Off">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase Exponent="9"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualTransition>
<VisualTransition From="On" GeneratedDuration="0" GeneratedEasingFunction="{x:Null}" To="Off">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase Exponent="9"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualTransition>
<VisualTransition From="Off" GeneratedDuration="0" GeneratedEasingFunction="{x:Null}" To="On">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="24">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase Exponent="9"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobBounds">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OuterBorder">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOn">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOff">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Dragging"/>
<VisualState x:Name="Off">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform"/>
</Storyboard>
</VisualState>
<VisualState x:Name="On">
<Storyboard>
<DoubleAnimation Duration="0" To="24" Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform"/>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobBounds">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OuterBorder">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOn">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOff">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="ContentStates">
<VisualState x:Name="OffContent">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OffContentPresenter"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="OffContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>True</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="OnContent">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OnContentPresenter"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="OnContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>True</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentPresenter x:Name="HeaderContentPresenter" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" TextElement.Foreground="{DynamicResource MahApps.Brushes.ToggleSwitch.HeaderForeground}" TextElement.FontWeight="{TemplateBinding mah:HeaderedControlHelper.HeaderFontWeight}" TextElement.FontStretch="{TemplateBinding mah:HeaderedControlHelper.HeaderFontStretch}" TextElement.FontSize="{TemplateBinding mah:HeaderedControlHelper.HeaderFontSize}" TextElement.FontFamily="{TemplateBinding mah:HeaderedControlHelper.HeaderFontFamily}" HorizontalAlignment="{TemplateBinding mah:HeaderedControlHelper.HeaderHorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding mah:HeaderedControlHelper.HeaderMargin}" Grid.Row="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding mah:HeaderedControlHelper.HeaderVerticalContentAlignment}"/>
<Grid HorizontalAlignment="Left" MinWidth="{DynamicResource ToggleSwitchThemeMinWidth}" Grid.Row="1" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="{DynamicResource ToggleSwitchContentSpaceMargin}"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="{DynamicResource ToggleSwitchPreContentMargin}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="{DynamicResource ToggleSwitchPostContentMargin}"/>
</Grid.RowDefinitions>
<Grid x:Name="SwitchAreaGrid" Background="{DynamicResource MahApps.Brushes.ToggleSwitch.ContainerBackground}" Grid.ColumnSpan="3" Margin="0,5" Grid.RowSpan="3"/>
<ContentPresenter x:Name="OffContentPresenter" ContentTemplate="{TemplateBinding OffContentTemplate}" Content="{TemplateBinding OffContent}" Grid.Column="2" ContentStringFormat="{TemplateBinding OffContentStringFormat}" TextElement.Foreground="{TemplateBinding Foreground}" TextElement.FontSize="{TemplateBinding FontSize}" TextElement.FontFamily="{TemplateBinding FontFamily}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding ContentPadding}" Opacity="0" Grid.Row="0" Grid.RowSpan="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<ContentPresenter x:Name="OnContentPresenter" ContentTemplate="{TemplateBinding OnContentTemplate}" Content="{TemplateBinding OnContent}" Grid.Column="2" ContentStringFormat="{TemplateBinding OnContentStringFormat}" TextElement.Foreground="{TemplateBinding Foreground}" TextElement.FontSize="{TemplateBinding FontSize}" TextElement.FontFamily="{TemplateBinding FontFamily}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding ContentPadding}" Opacity="0" Grid.Row="0" Grid.RowSpan="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="2" ContentStringFormat="{TemplateBinding ContentStringFormat}" TextElement.Foreground="{TemplateBinding Foreground}" TextElement.FontSize="{TemplateBinding FontSize}" TextElement.FontFamily="{TemplateBinding FontFamily}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding ContentPadding}" Grid.Row="0" Grid.RowSpan="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Grid x:Name="Switch" Grid.Column="0" HorizontalAlignment="Left" Height="20" Grid.Row="1" Width="44">
<Rectangle x:Name="OuterBorder" Fill="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOff}" RadiusY="10" RadiusX="10" Stroke="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOff}" StrokeThickness="{DynamicResource ToggleSwitchOffStrokeThickness}"/>
<Rectangle x:Name="SwitchKnobBounds" Fill="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOn}" Opacity="0" RadiusY="10" RadiusX="10" Stroke="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOn}" StrokeThickness="{DynamicResource ToggleSwitchOnStrokeThickness}"/>
<Grid x:Name="SwitchKnob" HorizontalAlignment="Left" Height="20" Width="20">
<Grid.RenderTransform>
<TranslateTransform/>
</Grid.RenderTransform>
<Ellipse x:Name="SwitchKnobOn" Fill="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOn}" Height="10" Opacity="0" Width="10"/>
<Ellipse x:Name="SwitchKnobOff" Fill="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOff}" Height="10" Width="10"/>
</Grid>
</Grid>
<Thumb x:Name="SwitchThumb" Grid.ColumnSpan="3" Grid.Column="0" Grid.Row="0" Grid.RowSpan="3">
<Thumb.Template>
<ControlTemplate TargetType="{x:Type Thumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ContentDirection" Value="LeftToRight">
<Setter Property="Grid.Column" TargetName="ContentPresenter" Value="0"/>
<Setter Property="Grid.Column" TargetName="OffContentPresenter" Value="0"/>
<Setter Property="Grid.Column" TargetName="OnContentPresenter" Value="0"/>
<Setter Property="Grid.Column" TargetName="Switch" Value="2"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOffPointerOver}"/>
<Setter Property="Stroke" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOffPointerOver}"/>
<Setter Property="Background" TargetName="SwitchAreaGrid" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContainerBackgroundPointerOver}"/>
<Setter Property="Fill" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOnPointerOver}"/>
<Setter Property="Stroke" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOnPointerOver}"/>
<Setter Property="Fill" TargetName="SwitchKnobOff" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOffPointerOver}"/>
<Setter Property="Fill" TargetName="SwitchKnobOn" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOnPointerOver}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Fill" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOffPressed}"/>
<Setter Property="Stroke" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOffPressed}"/>
<Setter Property="StrokeThickness" TargetName="OuterBorder" Value="0"/>
<Setter Property="Background" TargetName="SwitchAreaGrid" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContainerBackgroundPressed}"/>
<Setter Property="Fill" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOnPressed}"/>
<Setter Property="Stroke" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOnPressed}"/>
<Setter Property="Fill" TargetName="SwitchKnobOff" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOffPressed}"/>
<Setter Property="Fill" TargetName="SwitchKnobOn" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOnPressed}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="ContentPresenter" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContentForegroundDisabled}"/>
<Setter Property="TextElement.Foreground" TargetName="HeaderContentPresenter" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.HeaderForegroundDisabled}"/>
<Setter Property="TextElement.Foreground" TargetName="OffContentPresenter" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContentForegroundDisabled}"/>
<Setter Property="TextElement.Foreground" TargetName="OnContentPresenter" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContentForegroundDisabled}"/>
<Setter Property="Fill" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOffDisabled}"/>
<Setter Property="Stroke" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOffDisabled}"/>
<Setter Property="Background" TargetName="SwitchAreaGrid" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContainerBackgroundDisabled}"/>
<Setter Property="Fill" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOnDisabled}"/>
<Setter Property="Stroke" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOnDisabled}"/>
<Setter Property="Fill" TargetName="SwitchKnobOff" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOffDisabled}"/>
<Setter Property="Fill" TargetName="SwitchKnobOn" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOnDisabled}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource MahApps.Templates.ValidationError}"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="mah:HeaderedControlHelper.HeaderFontFamily" Value="{DynamicResource MahApps.Fonts.Family.ToggleSwitch.Header}"/>
<Setter Property="mah:HeaderedControlHelper.HeaderFontSize" Value="{DynamicResource MahApps.Font.Size.ToggleSwitch.Header}"/>
<Setter Property="mah:HeaderedControlHelper.HeaderMargin" Value="{DynamicResource ToggleSwitchTopHeaderMargin}"/>
</Style>
</Window.Resources>
<Grid>
<mah:ToggleSwitch x:Name="RibbAirOnOff_ToggleSwitch"
IsOn="{Binding ToggleSwitchAirIsChecked}"
IsEnabled="{Binding ToggleSwitchAirIsEnabled, Mode=OneWay}"
VerticalAlignment="Center"
OnCommand="{Binding AirOnCommand}"
OffCommand="{Binding AirOffCommand}"
HorizontalAlignment="Center" Grid.Column="1" Style="{DynamicResource ToggleSwitchAirStyle}"/>
</Grid>
</Window>
Blend 未正确提取样式。在下一行中,缺少名称 x:Name="KnobTranslateTransform"
的赋值,因此该元素没有名称,无法找到。
<Grid.RenderTransform>
<TranslateTransform x:Name="KnobTranslateTransform" />
</Grid.RenderTransform>
它应该看起来像这样:
<Grid.RenderTransform>
<TranslateTransform x:Name="KnobTranslateTransform"/>
</Grid.RenderTransform>
我想样式和模板中可能还有其他错误。我建议您直接从 GitHub 存储库中复制样式,因为 Blend 在这里似乎并不可靠。 ToggleSwitch
的样式可以在 here.
中找到
我会从 MahApps 自定义 ToggleSwitch。为此,我创建了一个项目并使用 Visual Blend 2019 打开它。 我只是做了:
- 右键单击 ToggleSwitch。
- Select“编辑模板”
- Select“编辑副本”
很遗憾,我得到:“'KnobTranslateTransform' 名称无法在 'System.Windows.Controls.Border' 的名称范围内找到。”
我是不是遗漏了一些参考资料?或者我是否必须使用一些额外的东西来设置 KnobTranslateTransform?
感谢您的帮助。
这是我的参考资料: Solution Explorer
这是我的 xaml 文件:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ToggleAirCustomSwitch"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="ToggleAirCustomSwitch.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="ToggleSwitchAirStyle" TargetType="{x:Type mah:ToggleSwitch}">
<Setter Property="ContentDirection" Value="RightToLeft"/>
<Setter Property="FontFamily" Value="{DynamicResource MahApps.Fonts.Family.ToggleSwitch}"/>
<Setter Property="FontSize" Value="{DynamicResource MahApps.Font.Size.ToggleSwitch}"/>
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContentForeground}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type mah:ToggleSwitch}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding mah:ControlsHelper.CornerRadius}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ToggleStates">
<VisualStateGroup.Transitions>
<VisualTransition From="Dragging" GeneratedDuration="0" GeneratedEasingFunction="{x:Null}" To="On">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="24">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase Exponent="9"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobBounds">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OuterBorder">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOn">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOff">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualTransition>
<VisualTransition From="Dragging" GeneratedDuration="0" GeneratedEasingFunction="{x:Null}" To="Off">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase Exponent="9"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualTransition>
<VisualTransition From="On" GeneratedDuration="0" GeneratedEasingFunction="{x:Null}" To="Off">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase Exponent="9"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualTransition>
<VisualTransition From="Off" GeneratedDuration="0" GeneratedEasingFunction="{x:Null}" To="On">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="24">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase Exponent="9"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobBounds">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OuterBorder">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOn">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOff">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="Dragging"/>
<VisualState x:Name="Off">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform"/>
</Storyboard>
</VisualState>
<VisualState x:Name="On">
<Storyboard>
<DoubleAnimation Duration="0" To="24" Storyboard.TargetProperty="X" Storyboard.TargetName="KnobTranslateTransform"/>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobBounds">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OuterBorder">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOn">
<DiscreteDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SwitchKnobOff">
<DiscreteDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="ContentStates">
<VisualState x:Name="OffContent">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OffContentPresenter"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="OffContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>True</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="OnContent">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OnContentPresenter"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="OnContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>True</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentPresenter x:Name="HeaderContentPresenter" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" TextElement.Foreground="{DynamicResource MahApps.Brushes.ToggleSwitch.HeaderForeground}" TextElement.FontWeight="{TemplateBinding mah:HeaderedControlHelper.HeaderFontWeight}" TextElement.FontStretch="{TemplateBinding mah:HeaderedControlHelper.HeaderFontStretch}" TextElement.FontSize="{TemplateBinding mah:HeaderedControlHelper.HeaderFontSize}" TextElement.FontFamily="{TemplateBinding mah:HeaderedControlHelper.HeaderFontFamily}" HorizontalAlignment="{TemplateBinding mah:HeaderedControlHelper.HeaderHorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding mah:HeaderedControlHelper.HeaderMargin}" Grid.Row="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding mah:HeaderedControlHelper.HeaderVerticalContentAlignment}"/>
<Grid HorizontalAlignment="Left" MinWidth="{DynamicResource ToggleSwitchThemeMinWidth}" Grid.Row="1" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="{DynamicResource ToggleSwitchContentSpaceMargin}"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="{DynamicResource ToggleSwitchPreContentMargin}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="{DynamicResource ToggleSwitchPostContentMargin}"/>
</Grid.RowDefinitions>
<Grid x:Name="SwitchAreaGrid" Background="{DynamicResource MahApps.Brushes.ToggleSwitch.ContainerBackground}" Grid.ColumnSpan="3" Margin="0,5" Grid.RowSpan="3"/>
<ContentPresenter x:Name="OffContentPresenter" ContentTemplate="{TemplateBinding OffContentTemplate}" Content="{TemplateBinding OffContent}" Grid.Column="2" ContentStringFormat="{TemplateBinding OffContentStringFormat}" TextElement.Foreground="{TemplateBinding Foreground}" TextElement.FontSize="{TemplateBinding FontSize}" TextElement.FontFamily="{TemplateBinding FontFamily}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding ContentPadding}" Opacity="0" Grid.Row="0" Grid.RowSpan="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<ContentPresenter x:Name="OnContentPresenter" ContentTemplate="{TemplateBinding OnContentTemplate}" Content="{TemplateBinding OnContent}" Grid.Column="2" ContentStringFormat="{TemplateBinding OnContentStringFormat}" TextElement.Foreground="{TemplateBinding Foreground}" TextElement.FontSize="{TemplateBinding FontSize}" TextElement.FontFamily="{TemplateBinding FontFamily}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding ContentPadding}" Opacity="0" Grid.Row="0" Grid.RowSpan="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="2" ContentStringFormat="{TemplateBinding ContentStringFormat}" TextElement.Foreground="{TemplateBinding Foreground}" TextElement.FontSize="{TemplateBinding FontSize}" TextElement.FontFamily="{TemplateBinding FontFamily}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding ContentPadding}" Grid.Row="0" Grid.RowSpan="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Grid x:Name="Switch" Grid.Column="0" HorizontalAlignment="Left" Height="20" Grid.Row="1" Width="44">
<Rectangle x:Name="OuterBorder" Fill="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOff}" RadiusY="10" RadiusX="10" Stroke="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOff}" StrokeThickness="{DynamicResource ToggleSwitchOffStrokeThickness}"/>
<Rectangle x:Name="SwitchKnobBounds" Fill="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOn}" Opacity="0" RadiusY="10" RadiusX="10" Stroke="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOn}" StrokeThickness="{DynamicResource ToggleSwitchOnStrokeThickness}"/>
<Grid x:Name="SwitchKnob" HorizontalAlignment="Left" Height="20" Width="20">
<Grid.RenderTransform>
<TranslateTransform/>
</Grid.RenderTransform>
<Ellipse x:Name="SwitchKnobOn" Fill="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOn}" Height="10" Opacity="0" Width="10"/>
<Ellipse x:Name="SwitchKnobOff" Fill="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOff}" Height="10" Width="10"/>
</Grid>
</Grid>
<Thumb x:Name="SwitchThumb" Grid.ColumnSpan="3" Grid.Column="0" Grid.Row="0" Grid.RowSpan="3">
<Thumb.Template>
<ControlTemplate TargetType="{x:Type Thumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ContentDirection" Value="LeftToRight">
<Setter Property="Grid.Column" TargetName="ContentPresenter" Value="0"/>
<Setter Property="Grid.Column" TargetName="OffContentPresenter" Value="0"/>
<Setter Property="Grid.Column" TargetName="OnContentPresenter" Value="0"/>
<Setter Property="Grid.Column" TargetName="Switch" Value="2"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOffPointerOver}"/>
<Setter Property="Stroke" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOffPointerOver}"/>
<Setter Property="Background" TargetName="SwitchAreaGrid" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContainerBackgroundPointerOver}"/>
<Setter Property="Fill" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOnPointerOver}"/>
<Setter Property="Stroke" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOnPointerOver}"/>
<Setter Property="Fill" TargetName="SwitchKnobOff" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOffPointerOver}"/>
<Setter Property="Fill" TargetName="SwitchKnobOn" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOnPointerOver}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Fill" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOffPressed}"/>
<Setter Property="Stroke" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOffPressed}"/>
<Setter Property="StrokeThickness" TargetName="OuterBorder" Value="0"/>
<Setter Property="Background" TargetName="SwitchAreaGrid" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContainerBackgroundPressed}"/>
<Setter Property="Fill" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOnPressed}"/>
<Setter Property="Stroke" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOnPressed}"/>
<Setter Property="Fill" TargetName="SwitchKnobOff" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOffPressed}"/>
<Setter Property="Fill" TargetName="SwitchKnobOn" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOnPressed}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="ContentPresenter" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContentForegroundDisabled}"/>
<Setter Property="TextElement.Foreground" TargetName="HeaderContentPresenter" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.HeaderForegroundDisabled}"/>
<Setter Property="TextElement.Foreground" TargetName="OffContentPresenter" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContentForegroundDisabled}"/>
<Setter Property="TextElement.Foreground" TargetName="OnContentPresenter" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContentForegroundDisabled}"/>
<Setter Property="Fill" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOffDisabled}"/>
<Setter Property="Stroke" TargetName="OuterBorder" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOffDisabled}"/>
<Setter Property="Background" TargetName="SwitchAreaGrid" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.ContainerBackgroundDisabled}"/>
<Setter Property="Fill" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.FillOnDisabled}"/>
<Setter Property="Stroke" TargetName="SwitchKnobBounds" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.StrokeOnDisabled}"/>
<Setter Property="Fill" TargetName="SwitchKnobOff" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOffDisabled}"/>
<Setter Property="Fill" TargetName="SwitchKnobOn" Value="{DynamicResource MahApps.Brushes.ToggleSwitch.KnobFillOnDisabled}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource MahApps.Templates.ValidationError}"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="mah:HeaderedControlHelper.HeaderFontFamily" Value="{DynamicResource MahApps.Fonts.Family.ToggleSwitch.Header}"/>
<Setter Property="mah:HeaderedControlHelper.HeaderFontSize" Value="{DynamicResource MahApps.Font.Size.ToggleSwitch.Header}"/>
<Setter Property="mah:HeaderedControlHelper.HeaderMargin" Value="{DynamicResource ToggleSwitchTopHeaderMargin}"/>
</Style>
</Window.Resources>
<Grid>
<mah:ToggleSwitch x:Name="RibbAirOnOff_ToggleSwitch"
IsOn="{Binding ToggleSwitchAirIsChecked}"
IsEnabled="{Binding ToggleSwitchAirIsEnabled, Mode=OneWay}"
VerticalAlignment="Center"
OnCommand="{Binding AirOnCommand}"
OffCommand="{Binding AirOffCommand}"
HorizontalAlignment="Center" Grid.Column="1" Style="{DynamicResource ToggleSwitchAirStyle}"/>
</Grid>
</Window>
Blend 未正确提取样式。在下一行中,缺少名称 x:Name="KnobTranslateTransform"
的赋值,因此该元素没有名称,无法找到。
<Grid.RenderTransform>
<TranslateTransform x:Name="KnobTranslateTransform" />
</Grid.RenderTransform>
它应该看起来像这样:
<Grid.RenderTransform>
<TranslateTransform x:Name="KnobTranslateTransform"/>
</Grid.RenderTransform>
我想样式和模板中可能还有其他错误。我建议您直接从 GitHub 存储库中复制样式,因为 Blend 在这里似乎并不可靠。 ToggleSwitch
的样式可以在 here.