日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

wpf 可以取消的单选checkbox

發布時間:2023/12/4 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 wpf 可以取消的单选checkbox 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

利用radioButton的groupName分組互斥。。再解決radiobutton的取消選擇的問題。給radiobutton加了一個像checkbox的樣式

2個方式:

?效果圖

第一種usecontrol:

xaml:

View Code <RadioButton x:Class="GEMS.Windows.Controls.UserControls.SingleCheckBox"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Click="RadioButton_Click_1" Unchecked="RadioButton_Unchecked_1" Style="{DynamicResource SingleCheckBox}"><RadioButton.Resources><Style x:Key="RadioButtonFocusVisual"><Setter Property="Control.Template"><Setter.Value><ControlTemplate><Border><Rectangle Margin="15,0,0,0"StrokeThickness="1"Stroke="#60000000"StrokeDashArray="1 2"/></Border></ControlTemplate></Setter.Value></Setter></Style><SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" /><SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" /><SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" /><SolidColorBrush x:Key="Normalborderbrush" Color="#5d7fad" /><Style x:Key="SingleCheckBox" TargetType="{x:Type RadioButton}"><Setter Property="GroupName" Value="Single"/><Setter Property="SnapsToDevicePixels" Value="true"/><Setter Property="OverridesDefaultStyle" Value="true"/><Setter Property="Foreground" Value="#071f3b"/><Setter Property="FontFamily" Value="Arial"></Setter><Setter Property="FontSize" Value="14"></Setter><Setter Property="FocusVisualStyle" Value="{StaticResource RadioButtonFocusVisual}"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type RadioButton}"><BulletDecorator Background="Transparent"><BulletDecorator.Bullet><Border x:Name="Border" Width="20" Height="20" CornerRadius="4" Background="#ffffff" BorderThickness="1" BorderBrush="{StaticResource Normalborderbrush}"><Path Width="14" Height="11" Margin="5,2,0,0" x:Name="CheckMark" SnapsToDevicePixels="False" Stroke="#173e78" StrokeThickness="2" Data="M 0 5 L 3 10 10 0" /><!--<Path Width="14" Height="11" Margin="5,2,0,0" x:Name="CheckMark" SnapsToDevicePixels="False" Stroke="{StaticResource Glyphbrush}" StrokeThickness="2" Data="M 0 0 L 7 7 M 0 7 L 7 0" /> cross--></Border></BulletDecorator.Bullet><ContentPresenter Margin="4,0,0,0"VerticalAlignment="Center"HorizontalAlignment="Left"RecognizesAccessKey="True"/></BulletDecorator><ControlTemplate.Triggers><Trigger Property="IsChecked" Value="false"><Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/></Trigger><Trigger Property="IsChecked" Value="{x:Null}"><Setter TargetName="CheckMark" Property="Data" Value="M 0 7 L 7 0" /></Trigger><Trigger Property="IsMouseOver" Value="true"><Setter TargetName="Border" Property="Background" Value="#ffffff" /></Trigger><Trigger Property="IsPressed" Value="true"><Setter TargetName="Border" Property="Background" Value="#ffffff" /></Trigger><Trigger Property="IsEnabled" Value="false"><Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" /><Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" /><Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></RadioButton.Resources> </RadioButton>

?

xaml的后臺cs

View Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;namespace GEMS.Windows.Controls.UserControls {/// <summary>/// Interaction logic for SingleCheckBox.xaml/// </summary>public partial class SingleCheckBox : RadioButton{private bool hasCheck;/// <summary>/// /// </summary>public bool HasCheck{get { return hasCheck; }set { hasCheck = value; }}public SingleCheckBox(){InitializeComponent();}private void RadioButton_Click_1(object sender, RoutedEventArgs e){if (this.HasCheck == false){this.HasCheck = true;this.IsChecked = true;}else{this.HasCheck = false;this.IsChecked = false;}}private void RadioButton_Unchecked_1(object sender, RoutedEventArgs e){this.HasCheck = false;}} }

?

第2種customercontrol

注意的是在generic里面需要去引用singleCheckBox.xaml

View Code <ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/GEMS.Windows.Controls.CustomControls;component/Themes/CustomRichTextBox.xaml"/><ResourceDictionary Source="/GEMS.Windows.Controls.CustomControls;component/Themes/ColorPicker.xaml"/><ResourceDictionary Source="/GEMS.Windows.Controls.CustomControls;component/Themes/MultipleDropDownBox.xaml"/><ResourceDictionary Source="/GEMS.Windows.Controls.CustomControls;component/Themes/SingleCheckBox.xaml"/></ResourceDictionary.MergedDictionaries> </ResourceDictionary>

?

?在singleCheckBox.xaml中定義樣式

View Code <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:GEMS.Windows.Controls.CustomControls"><Style x:Key="RadioButtonFocusVisual"><Setter Property="Control.Template"><Setter.Value><ControlTemplate><Border><Rectangle Margin="15,0,0,0"StrokeThickness="1"Stroke="#60000000"StrokeDashArray="1 2"/></Border></ControlTemplate></Setter.Value></Setter></Style><SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" /><SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" /><SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" /><SolidColorBrush x:Key="Normalborderbrush" Color="#5d7fad" /><Style TargetType="{x:Type local:SingleCheckBox}"><Setter Property="GroupName" Value="Single"/><Setter Property="SnapsToDevicePixels" Value="true"/><Setter Property="Foreground" Value="#071f3b"/><Setter Property="FontFamily" Value="Arial"></Setter><Setter Property="FontSize" Value="14"></Setter><Setter Property="Background" Value="Red"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:SingleCheckBox}"><BulletDecorator Background="Transparent"><BulletDecorator.Bullet><Border x:Name="Border" Width="20" Height="20" CornerRadius="4" Background="#ffffff" BorderThickness="1" BorderBrush="Black"><Path Width="14" Height="11" Margin="5,2,0,0" x:Name="CheckMark" SnapsToDevicePixels="False" Stroke="#173e78" StrokeThickness="2" Data="M 0 5 L 3 10 10 0" /></Border></BulletDecorator.Bullet><ContentPresenter Margin="4,0,0,0"VerticalAlignment="Center"HorizontalAlignment="Left"RecognizesAccessKey="True"/></BulletDecorator><ControlTemplate.Triggers><Trigger Property="IsChecked" Value="false"><Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/></Trigger><Trigger Property="IsChecked" Value="{x:Null}"><Setter TargetName="CheckMark" Property="Data" Value="M 0 7 L 7 0" /></Trigger><Trigger Property="IsMouseOver" Value="true"><Setter TargetName="Border" Property="Background" Value="#ffffff" /></Trigger><Trigger Property="IsPressed" Value="true"><Setter TargetName="Border" Property="Background" Value="#ffffff" /></Trigger><Trigger Property="IsEnabled" Value="false"><Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" /><Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" /><Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style> </ResourceDictionary>

在singleCheckBox.cs中
注意為了通知wpf正在提供一個信陽市,需要在自定義控件類的靜態構造中調用overrideMetadata()。。我們使用DefaultStyleKeyProperty來調用這個方式

?

?

static SingleCheckBox()

{

?

DefaultStyleKeyProperty.OverrideMetadata(

?

typeof(SingleCheckBox), newFrameworkPropertyMetadata(typeof(SingleCheckBox

)));

?

?

}

?

?

?

?

?

View Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls;namespace GEMS.Windows.Controls.CustomControls {public class SingleCheckBox:RadioButton{private bool hasCheck;/// <summary>/// /// </summary>public bool HasCheck{get { return hasCheck; }set { hasCheck = value; }}public SingleCheckBox(){this.Unchecked += SingleCheckBox_Unchecked;this.Click += SingleCheckBox_Click;}void SingleCheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e){this.HasCheck = false;}static SingleCheckBox(){DefaultStyleKeyProperty.OverrideMetadata(typeof(SingleCheckBox), new FrameworkPropertyMetadata(typeof(SingleCheckBox)));// }void SingleCheckBox_Click(object sender, System.Windows.RoutedEventArgs e){if (this.HasCheck == false){this.HasCheck = true;this.IsChecked = true;}else{this.HasCheck = false;this.IsChecked = false;}}} }

?

?

轉載于:https://www.cnblogs.com/FaDeKongJian/archive/2013/02/21/2920416.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的wpf 可以取消的单选checkbox的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。