123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System.Windows.Media;
- namespace System.Windows.Forms
- {
- public class CheckBox : ToggleButtonBase
- {
- private static System.Windows.Controls.ControlTemplate toggleButtonTemplate = (System.Windows.Controls.ControlTemplate)System.Windows.Markup.XamlReader.Parse(
- "<ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' TargetType =\"CheckBox\">" +
- "<ToggleButton IsChecked=\"{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}\" >" +
- "<Image Name=\"image\" Width=\"16\" Height=\"16\" />" +
- "</ToggleButton>" +
- "</ControlTemplate>");
- private static System.Windows.Controls.ControlTemplate checkBoxTemplate = (System.Windows.Controls.ControlTemplate)System.Windows.Markup.XamlReader.Parse(
- "<ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' TargetType =\"CheckBox\">" +
- "<CheckBox IsChecked=\"{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}\" >" +
- "<ContentPresenter/>" +
- "</CheckBox>" +
- "</ControlTemplate>");
- private System.Windows.Controls.TextBlock textBlock;
- protected new System.Windows.Controls.CheckBox control { get; }
- private Appearance appearance;
- public Appearance Appearance
- {
- get => appearance;
- set
- {
- if (value != appearance)
- {
- appearance = value;
- control.Template = value == Appearance.Button ? toggleButtonTemplate : checkBoxTemplate;
- control.ApplyTemplate();
- }
- }
- }
- protected override void UpdateControlImage(ImageSource image)
- {
- if (Appearance == Appearance.Button)
- {
- var img = control.Template.FindName("image", control) as System.Windows.Controls.Image;
- img.Source = image;
- img.Visibility = image != null ? Visibility.Visible : Visibility.Collapsed;
- }
- }
- protected override void OnEnabledChanged(EventArgs e)
- {
- base.OnEnabledChanged(e);
- textBlock.Opacity = Enabled ? 1 : 0.5;
- }
- public CheckBox()
- {
- control = new();
- SetControl(control);
- textBlock = new();
- textBlock.TextWrapping = TextWrapping.Wrap;
- control.Content = textBlock;
- }
- }
- }
|