开发者

problem for bind radiobutton to database

Please following my code :

<Grid DataContext="{Binding ElementName=dataGrid_services, Path=SelectedItem}" 
 Width="766">
<RadioButton Content="visit" IsChecked="{Binding Path=type_services}"  
 FontFamily="Tahoma"/>

i want to bind ischecked property开发者_StackOverflow社区 from radiobutton but return value is not false or true. the value is string. please help me how to bind this value? thanks in advance


Use an IValueConverter.

Given this window containing your radiobutton and associated bindings:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525" x:Name="dataGrid_services">
<Window.Resources>
    <local:CheckedConverter x:Key="converter"/>
</Window.Resources>
<Grid DataContext="{Binding ElementName=dataGrid_services, Path=SelectedItem}"  Width="766">
    <RadioButton Content="visit" IsChecked="{Binding Path=type_services, Converter={StaticResource converter}}"   FontFamily="Tahoma"/>
</Grid>

The changes are adding a namespace reference for local (or whichever namespace your converter is in):

xmlns:local="clr-namespace:WpfApplication1"

creating the converter resource:

<Window.Resources>
    <local:CheckedConverter x:Key="converter"/>
</Window.Resources>

and using the converter resource:

IsChecked="{Binding Path=type_services, Converter={StaticResource converter}}"

The converter looks like this and simply converts from string to boolean.

public class CheckedConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string typeService = value as string;
            if (typeService == "Yes it is")
            {
                return true;
            }

            if (typeService == "Nope")
            {
                return false;
            }

        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool typeService = (bool)value;


            if (typeService)
            {
                return "Yes it is";
            }
            else
            {
                return "Nope";
            }
    }
}


You'll have to define a value converter to convert from string to boolean and use it with your RadioButton.

public class StringToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        return Convert.ToBool(value);
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        return Convert.ToString(value);
    }
}

and in XAML, use

<RadioButton Content="visit" IsChecked="{Binding Path=type_services, Converter={StaticResource stringToBoolConverter}}"  
 FontFamily="Tahoma"/>

where stringToBoolConverter is defined in the resources of a parent element.

<Window.Resources> 
    <local:StringToBoolConverter x:Key="stringToBoolConverter" /> 
</Window.Resources>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜