zoukankan      html  css  js  c++  java
  • WPF RadioButton 绑定枚举

    定义枚举类型

      public enum CoordinateEnum
        {
            X=0,Y,Z,RX,RY,RZ
        }

    定义枚举转换Convert

    public class EnumConvert : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return value != null && value.Equals(parameter);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
            }
        }

    定义ViewModel

    public class MainViewModel : INotifyPropertyChanged
        {
            private CoordinateEnum translateAxis = CoordinateEnum.X;
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public CoordinateEnum TranslateAxis
            {
                get { return translateAxis; }
                set
                {
                    if (translateAxis != value)
                    {
                        translateAxis = value;
                        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("TranslateAxis"));
                    }
                }
            }
        }

    WPF XAML定义

    <Window.Resources>
            <local:EnumConvert x:Key="convert" />
        </Window.Resources>
        <Grid>
            <StackPanel>
                <RadioButton Content="X" IsChecked="{Binding TranslateAxis, Mode=TwoWay, Converter={StaticResource convert}, ConverterParameter={x:Static local:CoordinateEnum.X}}" />
                <RadioButton Content="Y" IsChecked="{Binding TranslateAxis, Mode=TwoWay, Converter={StaticResource convert}, ConverterParameter={x:Static local:CoordinateEnum.Y}}" />
                <RadioButton Content="Z" IsChecked="{Binding TranslateAxis, Mode=TwoWay, Converter={StaticResource convert}, ConverterParameter={x:Static local:CoordinateEnum.Z}}" />
                <RadioButton Content="RX" IsChecked="{Binding TranslateAxis, Mode=TwoWay, Converter={StaticResource convert}, ConverterParameter={x:Static local:CoordinateEnum.RX}}" />
                <RadioButton Content="RY" IsChecked="{Binding TranslateAxis, Mode=TwoWay, Converter={StaticResource convert}, ConverterParameter={x:Static local:CoordinateEnum.RY}}" />
                <RadioButton Content="RZ" IsChecked="{Binding TranslateAxis, Mode=TwoWay, Converter={StaticResource convert}, ConverterParameter={x:Static local:CoordinateEnum.RZ}}" />
            </StackPanel>
        </Grid>

    设置Context

     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new MainViewModel();
            }
        }
  • 相关阅读:
    FMDB的简单使用
    SQLite3的基本使用
    KVC与KVO的实现原理
    数据存储与IO(二)
    数据存储与IO(一)
    cocoapods卸载与安装的各种坑
    Core Data的一些常见用法
    UITextField限制中英文字数和光标定位以及第三方输入限制问题
    prompt-tuning paper reading
    ACL2021 事件抽取相关论文阅读
  • 原文地址:https://www.cnblogs.com/houzf/p/15743830.html
Copyright © 2011-2022 走看看