Я пытаюсь создать кнопку ToggleSwitch wpf, но не могу найти способ доступа к стилю с помощью кода. Идея состоит в том, чтобы изменить границу и цвет фона элементов управления, присутствующих внутри window.resource.style. А именно "ToggleButton" и "Slider". Я пытался использовать FindName, но это ничего не возвращает, потому что это не элемент управления (?). Я на самом деле не эксперт по wpf или xaml, но в PowerShell иногда нужно решать конкретные задачи, и вот я здесь. Любая помощь будет оценена.
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
$syncHash = [hashtable]::Synchronized(@{})
$newRunspace = [runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$psCmd = [PowerShell]::Create().AddScript({
[xml]$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"
x:Name="ToggleSwitch" Title="TogleSwitch..."
Width = "335" Height = "130" >
<Window.Resources>
<Style x:Key="ToggleSwitch" TargetType="{x:Type CheckBox}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="Orange" />
<Setter Property="BorderThickness" Value="1,1,1,1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Name="ControlToggle" TargetType="{x:Type CheckBox}">
<ControlTemplate.Resources>
<Storyboard x:Key="OnChecking">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="45"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnUnchecking">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(FrameworkElement.Margin)">
<SplineThicknessKeyFrame KeyTime="00:00:00.3000000" Value="1,1,1,1"/>
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<DockPanel x:Name="dockPanel">
<Border x:Name="ToggleButton" Background="LightGray" BorderBrush="Gray" BorderThickness="2,2,2,2" CornerRadius="14,14,14,14" Height="30" Width="70" Margin="0,0,0,0">
<Grid Margin="0,0,0,0" Width="60" Height="25" Background="Transparent" >
<Border x:Name="slider" Background="Gray" BorderBrush="White" HorizontalAlignment="Left" Width="15" Height="15" BorderThickness="0,0,0,0" CornerRadius="30,30,30,30" RenderTransformOrigin="0.5,0.5" Margin="0,0,0,0">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Border.RenderTransform>
</Border>
</Grid>
</Border>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource OnUnchecking}" x:Name="OnUnchecking_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource OnChecking}" x:Name="OnChecking_BeginStoryboard"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<CheckBox HorizontalAlignment="Center" Style="{DynamicResource ToggleSwitch}" VerticalAlignment="Top" Width="200" Height="30" Margin="0,5,0,0"/>
</Grid>
</Window>
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()
While (!($syncHash.Window.IsInitialized)) {
Start-Sleep -S 1
}
Приветствия.
Пример желаемого выхода: