关于WPF的RadioButton绑定不同类型

1.绑定布尔值

2.绑定枚举值

3.绑定数字

4.通过行为绑定

View层代码:


    
        
    
    
    
        
        
            
                
            
            
            
            
        
        
        
            
                
            
            
            
            
            
        
        
        
            
                
            
            
                
                    
                        
                            1
                        
                    
                
            
            
                
                    
                        
                            2
                        
                    
                
            
            
                
                    
                        
                            3
                        
                    
                
            
            
        
        
        
            
                
            
            
            
            
        
    

MainWindowViewModel.cs:

using GalaSoft.MvvmLight;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
using WPFApplication.Enums;

namespace WPFApplication
{
    public class MainWindowViewModel : ViewModelBase
    {
        private bool isChecked;
        public bool IsChecked
        {
            get
            {
                return isChecked;
            }
            set
            {
                isChecked = value;
                RaisePropertyChanged(nameof(IsChecked));
            }
        }
        private UserStateEnum userState;

        public UserStateEnum UserState
        {
            get { return userState; }
            set
            {
                userState = value;
                RaisePropertyChanged(nameof(UserState));
            }
        }
        private int number = 1;
        public int Number
        {
            get
            {
                return number;
            }
            set
            {
                number = value;
                RaisePropertyChanged(nameof(Number));
            }
        }
        private UserStateEnum userStateBehavior;

        public UserStateEnum UserStateBehavior
        {
            get { return userStateBehavior; }
            set
            {
                userStateBehavior = value;
                RaisePropertyChanged(nameof(UserStateBehavior));
            }
        }
    }
}

SelectedItemBehavior.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;

namespace WPFApplication
{
    public class SelectedItemBehavior : Behavior
    {
        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register(nameof(SelectedItem), typeof(object), typeof(SelectedItemBehavior), new PropertyMetadata(null, OnPropertyChangedCallback));
        private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var behavior = (SelectedItemBehavior)d;
            foreach (var child in behavior.Children)
            {
                var rb = child as RadioButton;
                rb.IsChecked = rb.Tag.Equals(e.NewValue);
            }
        }
        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (s, e) =>
            {
                //订阅回调函数
                foreach (var children in Children)
                {
                    children.Checked += OnRadioButtonChangeChecked;
                    children.IsChecked = children.Tag.Equals(SelectedItem);
                }
            };
        }

        private void OnRadioButtonChangeChecked(object sender, RoutedEventArgs e)
        {

            var radio = sender as RadioButton;
            if (radio == null) return;
            SelectedItem = radio.Tag;
            //双向绑定(bind)
            BindingOperations.GetBindingExpression(this, SelectedItemProperty).UpdateSource();
        }
        private IEnumerable Children
        {
            get
            {
                if (AssociatedObject is Panel panel)
                    return panel.Children.OfType();
                if (AssociatedObject is ItemsControl control)
                    return control.Items.OfType();
                return Enumerable.Empty();
            }
        }

    }
}

UserStateEnum.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFApplication.Enums
{
    public enum UserStateEnum
    {
        Normal,
        Freeze,
        Logout
    }
}

BooleanInverterConvert.cs

using System;
using System.Globalization;
using System.Windows.Data;

namespace WPFApplication.ValueConvert
{
    public class BooleanInverterConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool b && !b) return true;
            return false;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool b && !b) return true;
            return false;
        }
    }
}

ComparisonConverter.cs

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WPFApplication.ValueConvert
{
    public class ComparisonConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool b && b) return parameter;
            return Binding.DoNothing;
        }
    }
}

你可能感兴趣的:(WPF,wpf)