Estou tentando criar um botão ToggleSwitch do wpf, mas não consigo encontrar como acessar o estilo com o código por trás. A ideia é alterar as cores de borda e fundo dos controles, presentes dentro do window.resource.style. Ou seja, "ToggleButton" e "slider". Eu tentei usar FindName, mas isso não retorna nada porque não é um controle (?). Eu realmente não sou um especialista em wpf ou xaml, mas em powershell às vezes é preciso atender a necessidades específicas e aqui estou eu. Qualquer ajuda seria apreciada.
[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
}
Felicidades.
Exemplo de saída desejada:
Tags powershell wpf