将单选按钮绑定到数据库的问题

发布于 2024-11-18 16:44:50 字数 318 浏览 2 评论 0原文

请遵循我的代码:

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

我想绑定单选按钮的 ischecked 属性,但返回值不是 false 或 true。 该值是字符串。请帮我如何绑定这个值? 提前致谢

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-11-25 16:44:50

使用 IValueConverter。

鉴于此窗口包含您的单选按钮和关联的绑定:

<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>

这些更改是添加本地命名空间引用(或转换器所在的任何命名空间):

xmlns:local="clr-namespace:WpfApplication1"

创建转换器资源:

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

并使用转换器资源:

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

转换器看起来像这样,只是从字符串转换为布尔值。

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";
            }
    }
}

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";
            }
    }
}
尴尬癌患者 2024-11-25 16:44:50

您必须定义一个值转换器来从字符串转换为布尔值,并将其与您的 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);
    }
}

在 XAML 中,使用

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

在父元素的资源中定义 stringToBoolConverter 的位置。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文