<Window x:Class="KrakenZ_Tweaker.MainWindow"
ResizeMode="CanMinimize"
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:KrakenZ_Tweaker"
mc:Ignorable="d"
Title="KrakenZ Tweaker" Height="700" Width="1000" >
<Grid Background="#FF4E4E4E" Margin="0,0,0,0" >
<StackPanel HorizontalAlignment="Left" Background="#FF121820" Height="700" Width="300">
<Image HorizontalAlignment="Center" Margin="0,30,0,12" Height="125" Width="140" Source="/KrakenZ-Tweaker-Logo.ico"/>
<TextBlock Margin="0,0,0,9" TextWrapping="Wrap" Text=" KrakenZ Tweaker" FontFamily="Montserrat" FontSize="30" Foreground="White" Height="34" Width="257"/>
<Separator Width="187" Height="8" Background="Gray"/>
<Button x:Name="Restore_Point" Margin="10" Height="47" Width="279" BorderBrush="{x:Null}" Foreground="White" FontFamily="Bahnschrift" FontSize="25" Content="Restore Point" Cursor="Hand" Click="Restore_Point_Click">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#175EBD"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkGoldenrod"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="8"/>
</Style>
</Button.Resources>
</Button>
<Button x:Name="Disable_Unnecessary_Services" Margin="10" Height="47" Width="279" BorderBrush="{x:Null}" Foreground="White" FontFamily="Bahnschrift" FontSize="25" Content="Disable Services" Cursor="Hand" Click="Disable_Unnecessary_Services_Click">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#121820"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkGoldenrod"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="8"/>
</Style>
</Button.Resources>
</Button>
<Button x:Name="Clear_Junk_Files" Margin="10" Height="47" BorderBrush="#121820" Width="279" Foreground="White" FontFamily="Bahnschrift" FontSize="25" Content="Clear Junk Files" Cursor="Hand" Click="Clear_Junk_Files_Click">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#121820"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkGoldenrod"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="8"/>
</Style>
</Button.Resources>
</Button>
<Button x:Name="Useful_Optimization" Margin="10" Height="47" Width="279" BorderBrush="{x:Null}" Foreground="White" FontFamily="Bahnschrift" FontSize="25" Content="Useful Optimizations" Cursor="Hand" Click="Useful_Optimization_Click">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#121820"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkGoldenrod"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="8"/>
</Style>
</Button.Resources>
</Button>
</StackPanel>
<StackPanel x:Name="User_Control" Margin="308,2,6,0"/>
</Grid>
這是我的 MainWindow.XAML 代碼。
private void Restore_Point_Click(object sender, RoutedEventArgs e)
{
Restore_Point.Background = (Brush)(new BrushConverter().ConvertFrom("#175EBD"));
Disable_Unnecessary_Services.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
Clear_Junk_Files.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
Useful_Optimization.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
User_Control.Children.Remove(UC2);
User_Control.Children.Remove(UC3);
User_Control.Children.Remove(UC4);
User_Control.Children.Add(defaultUC);
}
private void Disable_Unnecessary_Services_Click(object sender, RoutedEventArgs e)
{
Disable_Unnecessary_Services.Background = (Brush)(new BrushConverter().ConvertFrom("#175EBD"));
Restore_Point.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
Clear_Junk_Files.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
Useful_Optimization.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
User_Control.Children.Remove(defaultUC);
User_Control.Children.Remove(UC3);
User_Control.Children.Remove(UC4);
User_Control.Children.Add(UC2);
}
private void Clear_Junk_Files_Click(object sender, RoutedEventArgs e)
{
Clear_Junk_Files.Background = (Brush)(new BrushConverter().ConvertFrom("#175EBD"));
Disable_Unnecessary_Services.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
Restore_Point.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
Useful_Optimization.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
User_Control.Children.Remove(defaultUC);
User_Control.Children.Remove(UC2);
User_Control.Children.Remove(UC4);
User_Control.Children.Add(UC3);
}
private void Useful_Optimization_Click(object sender, RoutedEventArgs e)
{
Useful_Optimization.Background = (Brush)(new BrushConverter().ConvertFrom("#175EBD"));
Clear_Junk_Files.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
Disable_Unnecessary_Services.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
Restore_Point.Background = (Brush)(new BrushConverter().ConvertFrom("#121820"));
User_Control.Children.Remove(defaultUC);
User_Control.Children.Remove(UC2);
User_Control.Children.Remove(UC3);
User_Control.Children.Add(UC4);
}
這些是所有按鈕的事件處理程式。這里發生的問題是我只能使用 IsMouseOver 屬性一次。單擊任何按鈕后,我無法使用此屬性。我認為這個問題是由于按鈕單擊的事件處理程式造成的,但我不確定這一點。請幫忙。提前致謝。
uj5u.com熱心網友回復:
通過Control.Background顯式/本地設定屬性,您可以覆寫/洗掉樣式觸發器。這種行為是需要的,并通過遵循依賴屬性的通用 WPF 值優先規則來實作。此外,您的代碼包含大量重復代碼,您應該始終避免這些重復代碼。
要保持這種特定于控制的(視覺)行為,您不能在 a 中定義相應的觸發器Style,而是在ControlTemplate.
如果像您的情況一樣,Button具有“已單擊”切換狀態,您通常更喜歡 a ToggleButton(或者在您的情況下甚至是 a RadioButton)。
這將使您能夠僅通過觸發器更方便地處理單擊狀態。
這將幫助您更好地理解您的代碼,因為現在可以在一個地方實作視覺效果(而不是將其分散到多種樣式和代碼隱藏中)。
出于這個原因,您通常應該避免為控制元件定義多個樣式(就像您所做的那樣),如果這些樣式不是全域的或本地定義Style的:使用Button.StyleandButton.Resources屬性定義兩個資源會使您的代碼難以閱讀。更好地將外觀相關配置直接移動到相應的模板元素(因為您已經覆寫了ControlTemplate)。
而且由于所有模板本質上都是相同的,因此您可以通過將它們提取到ResourceDictionary公共范圍內(例如Window.Resources)來輕松避免重復代碼。
您的固定和改進代碼如下所示:
<Window>
<Window.Resources>
<Style x:Key="RadioButtonStyle"
TargetType="RadioButton">
<Setter Property="Background"
Value="#121820" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border x:Name="Border"
CornerRadius="8"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<!--
Note that the order of the triggers is important.
To allow the IsMouseOver trigger to have a higher priority than the IsChecked trigger,
the IsMouseOver trigger must be defined after the IsChecked trigger.
Trigger priority is interpreted low to high from top to down
-->
<ControlTemplate.Triggers>
<Trigger Property="IsChecked"
Value="True">
<Setter TargetName="Border"
Property="Background"
Value="#175EBD" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="Border"
Property="Background"
Value="DarkGoldenrod" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel>
<RadioButton x:Name="Restore_Point"
Style="{StaticResource RadioButtonStyle}"
Margin="10"
Height="47"
Width="279"
BorderBrush="{x:Null}"
Foreground="White"
FontFamily="Bahnschrift"
FontSize="25"
Content="Restore Point"
Cursor="Hand"
Click="Restore_Point_Click" />
<RadioButton x:Name="Disable_Unnecessary_Services"
Style="{StaticResource RadioButtonStyle}"
Margin="10"
Height="47"
Width="279"
BorderBrush="{x:Null}"
Foreground="White"
FontFamily="Bahnschrift"
FontSize="25"
Content="Restore Point"
Cursor="Hand"
Click="Disable_Unnecessary_Services_Click" />
<RadioButton x:Name="Clear_Junk_Files"
Style="{StaticResource RadioButtonStyle}"
Margin="10"
Height="47"
Width="279"
BorderBrush="{x:Null}"
Foreground="White"
FontFamily="Bahnschrift"
FontSize="25"
Content="Restore Point"
Cursor="Hand"
Click="Clear_Junk_Files_Click" />
</StackPanel>
</Window>
由于按鈕的背景現在完全在 XAML 中處理,您現在可以清理事件處理程式。
現在Button.Click事件處理程式將如下所示:
private void Restore_Point_Click(object sender, RoutedEventArgs e)
{
User_Control.Children.Remove(UC2);
User_Control.Children.Remove(UC3);
User_Control.Children.Remove(UC4);
User_Control.Children.Add(defaultUC);
}
private void Disable_Unnecessary_Services_Click(object sender, RoutedEventArgs e)
{
User_Control.Children.Remove(defaultUC);
User_Control.Children.Remove(UC3);
User_Control.Children.Remove(UC4);
User_Control.Children.Add(UC2);
}
private void Clear_Junk_Files_Click(object sender, RoutedEventArgs e)
{
User_Control.Children.Remove(defaultUC);
User_Control.Children.Remove(UC2);
User_Control.Children.Remove(UC4);
User_Control.Children.Add(UC3);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/513322.html
標籤:C#wpf
