wpf中bool按钮三种方式

今天用到了bool按钮,学习了下

1,这可通过自己绘制。然后适用于一个bool按钮

xmal 代码

Background="Transparent"
Width="60" Height="24" MouseLeftButtonUp="LayoutRoot_MouseLeftButtonUp">
BorderThickness="1" Margin="4,2"
Padding="0" CornerRadius="8" Background="#e74c3c">
Fill="#2ecc71" RadiusX="8" RadiusY="8"
Visibility="Collapsed"/>

BorderBrush="#aaaaaa"
BorderThickness="1"
HorizontalAlignment="Left" CornerRadius="15" Width="24" Height="24">
Fill="#FFF1F1F1"
StrokeThickness="0"
Width="22" RadiusX="15" RadiusY="15"/>

Background="Transparent"
Width="60" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="116,161,0,0" MouseLeftButtonUp="LayoutRoot_MouseLeftButtonUp">
BorderThickness="1" Margin="4,2"
Padding="0" CornerRadius="8" Background="#e74c3c">
Fill="#2ecc71" RadiusX="8" RadiusY="8"
Visibility="Collapsed"/>

BorderBrush="#aaaaaa"
BorderThickness="1"
HorizontalAlignment="Left" CornerRadius="15" Width="24" Height="24">
Fill="#FFF1F1F1"
StrokeThickness="0"
Width="22" RadiusX="15" RadiusY="15"/>

 

cs里面的代码

public static readonly DependencyProperty IsCheckedProperty =
DependencyProperty.Register("IsChecked", typeof(bool), typeof(text), new PropertyMetadata(default(bool), OnIsCheckedChanged));

public event RoutedEventHandler Checked;

public event RoutedEventHandler UnChecked;

public bool IsChecked
{
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}

private static void OnIsCheckedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
(obj as text).OnIsCheckedChanged(args);
}
private void OnIsCheckedChanged(DependencyPropertyChangedEventArgs args)
{
fillRectangle.Visibility = IsChecked ? Visibility.Visible : Visibility.Collapsed;
slideBorder.HorizontalAlignment = IsChecked ? HorizontalAlignment.Right : HorizontalAlignment.Left;

if (IsChecked && Checked != null)
{
Checked(this, new RoutedEventArgs());
}
if (!IsChecked && UnChecked != null)
{
UnChecked(this, new RoutedEventArgs());
}
}

private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs args)
{
args.Handled = true;
IsChecked ^= true;
}

 

 

2,这个使用与多个bool按钮

xaml里面的代码

       

样式

 

3.像第二种

xmal代码

       

样式

















 

你可能感兴趣的:(wpf中bool按钮三种方式)