WPF MVVM 绑定RadioButton数据

来源:互联网 发布:淘宝推广教程视频 编辑:程序博客网 时间:2024/06/05 14:49

一、 M层,创建实体

    public class NotificationBase : INotifyPropertyChanged    {        public event PropertyChangedEventHandler PropertyChanged;        public void NotifyPropertyChanged(string propertyName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }    }

    [DataContract]    public abstract class BaseModel : NotificationBase    {    }

    [DataContract]    public class BuildProductsModel : BaseModel    {        /// <summary>        /// 是否翻转        /// </summary>        private string isreversal;        [DataMember]        public string IsReversal        {            get { return isreversal; }            set            {                isreversal = value;                NotifyPropertyChanged("IsReversal");            }        }    }

二、VM层

    public class PageBuildProductsModel : BaseModel    {        public BuildProductsModel ProductsModel { get; set; }        public PageBuildProductsModel()        {            ProductsModel = new BuildProductsModel();            ProductsModel.IsReversal = "翻转";        }    }


三、V层

    public partial class Page_BuildProducts : Page    {        public Page_BuildProducts()        {            InitializeComponent();            this.DataContext = new PageBuildProductsModel();        }    }

<Page x:Class="AutomaticConfigurationAPP.Pages.Page_BuildProducts"      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"       xmlns:local="clr-namespace:AutomaticConfigurationCore.Commons;assembly=AutomaticConfigurationCore"      mc:Ignorable="d"       d:DesignHeight="300" d:DesignWidth="600">    <Page.Resources>        <local:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />    </Page.Resources>    <Grid Margin="5" x:Name="grid1">        <WrapPanel Grid.Column="3" Grid.Row="2" VerticalAlignment="Center">            <RadioButton Content="翻转" Margin="0,0,10,0" IsChecked="{Binding Path=ProductsModel.IsReversal,Mode=TwoWay,Converter={StaticResource EnumToBooleanConverter},ConverterParameter=翻转}"/>            <RadioButton Content="不翻转" Margin="0,0,10,0" IsChecked="{Binding Path=ProductsModel.IsReversal,Mode=TwoWay,Converter={StaticResource EnumToBooleanConverter},ConverterParameter=不翻转}"/>        </WrapPanel>    </Grid></Page>
    public class EnumToBooleanConverter : IValueConverter    {        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)        {            return value == null ? false : value.Equals(parameter);        }        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)        {            return value != null && value.Equals(true) ? parameter : Binding.DoNothing;        }    }