如何在WP7或WPF中绑定单选按钮 [英] How to Bind The Radio Button in WP7 or WPF

查看:100
本文介绍了如何在WP7或WPF中绑定单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

我有两个单选按钮(男,女),我必须按属性(性别)进行绑定.但是我的性别属性返回整数值(男性为0,女性为1).

因此,请建议我如何将数据库中的数据绑定到表单?

谢谢

Hi Experts,

I have two Radio Button(Male,Female) which i have to bind By Property (Gender). But My Gender Property Returns the Integer Value (0 For Male and 1 For Female).

So Please Suggest Me how to bind the data from database to form?

Thanks

推荐答案

问候,例如:_Anubhava;

我发现 项目#358 –将RadioButton绑定到枚举类型 http://wpf.2000things.com/2011/08/05/358-binding-a-radiobutton-to-an-枚举类型/来自Sean Sexton的关于WPF您应该了解的2,000件事" .在他的示例和下面的示例中,执行到WPF的RadioButton到DataBinding的绑定时要牢记两个关键点:

1.)您可能要考虑使用在System.Windows.Data命名空间中实现IValueConverter接口的类型转换器.

2.)不要为绑定到窗口DataContext内的属性或数据元素的RadioButton指定GroupName属性.

包含使用值转换器的RadioButtons和DataBinding设置的窗口的XAML如下:

Greetings, eg_Anubhava;

I found that Item #358 – Binding a RadioButton to an Enumerated Type at http://wpf.2000things.com/2011/08/05/358-binding-a-radiobutton-to-an-enumerated-type/ from Sean Sexton''s "2,000 Things You Should Know About WPF" to be of tremendous help. In both his example and mine below, there are a couple of key points to keep in mind when performing DataBinding to a RadioButton to WPF:

1.) You may want to consider using a Type Converter that implements the IValueConverter interface in the System.Windows.Data namespace.

2.) Don''t specify a GroupName property for the RadioButton that is being bound to your porperty or data element inside your window''s DataContext.

The XAML for the Window containing the RadioButtons and the DataBinding settings that use the value converter is as follows:

<Window x:Class="WPFRadioButtonBinding.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:loc="clr-namespace:WPFRadioButtonBinding"

        Title="Gender" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" SizeToContent="WidthAndHeight">
    <Window.Resources>
        <!-- Define a XAML Static Resource to EnumToBooleanConverter / IValueConverter -->
        <loc:EnumToBooleanConverter x:Key="EnumToBoolConverter" />
    </Window.Resources>

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Label Margin="6">The Person's Gender?</Label>

        <!-- RadioButton Data Bindings:                                                       -->
        <!--    Path: The SelectedGender AutoProperty inside the MainWindow class.            -->
        <!--    Converter: The Window Static Resource to the EnumToBooleanConverter           -->
        <!--    ConverterParameter: The Gender Enumerated Data Value in the Main Window Class -->
        <RadioButton Name="MaleRadioButton" Margin="20, 6"

         IsChecked="{Binding Path=SelectedGender, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static loc:Gender.Male}}">_Male</RadioButton>

        <RadioButton Name="FemaleRadioButton" Margin="20, 6"

         IsChecked="{Binding Path=SelectedGender, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static loc:Gender.Female}}">_Female</RadioButton>

        <Button Name="AnswerButton" Margin="6" HorizontalAlignment="Right" Width="65" Height="25" Click="AnswerButton_Click">_Answer...</Button>
    </StackPanel>
</Window>



该窗口后面的C#代码类似于以下内容:



The C# code behind for the Window would resemble the following:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WPFRadioButtonBinding
{
    public enum Gender
    {
        Male,
        Female
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Gender SelectedGender { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            //
            // Initialize the Default Gender Value...
            //
            SelectedGender = Gender.Male;

            //
            // Set the Data Context to this MainWindow class for allowing
            // the Gender to be changed through Data Binding...
            //
            this.DataContext = this;

            return;
        }

        private void AnswerButton_Click(object sender, RoutedEventArgs e)
        {
            string confirmGender =
            string.Format("The Selected Gender Is: {0}", SelectedGender.ToString());

            MessageBox.Show(confirmGender, "Selected Gender", MessageBoxButton.OK,
                            MessageBoxImage.Information);

            return;
        }
    }
}



...并使用Sean的 EnumToBooleanConverter 根据单击的RadioButton来检查并对数据绑定的枚举Gender值进行任何更改:



... and using Sean''s EnumToBooleanConverter to check for and perform any changes to the data bound enumerated Gender value based on which RadioButton was clicked:

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

namespace WPFRadioButtonBinding
{
    public class EnumToBooleanConverter : IValueConverter
    {
        // Convert enum [value] to boolean, true if matches [param]
        public object Convert(object value, Type targetType, object param, CultureInfo culture)
        {
            return value.Equals(param);
        }

        // Convert boolean to enum, returning [param] if true
        public object ConvertBack(object value, Type targetType, object param, CultureInfo culture)
        {
            return (bool)value ? param : Binding.DoNothing;
        }
    }
}



我希望这对您有所帮助和兴趣...



I hope this was of help and interest to you...


这篇关于如何在WP7或WPF中绑定单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆