【问题标题】:problem for bind radiobutton to database将单选按钮绑定到数据库的问题
【发布时间】:2011-07-07 14:11:15
【问题描述】:

请关注我的代码:

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

我想从单选按钮绑定 ischecked 属性,但返回值不是 false 或 true。 值为字符串。请帮我如何绑定这个值? 在此先感谢

【问题讨论】:

    标签: c# wpf binding wpfdatagrid


    【解决方案1】:

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

    【讨论】:

    • 如何使用这个 IValueConverter?
    • @mo_al - 您需要将其定义为资源,然后在绑定中引用它(作为 StaticResource}。
    • 如何在 xaml 中调用转换器
    • 您不会在 xaml 中“调用”转换器,而是为它创建资源(并为其提供密钥),然后在绑定中使用它。我已将这些部分分开,以便您可以在我的答案中看到它们。请注意,您的数据上下文需要通知自身的任何更改(使用 INotifyPropertyChanged),以便 RadioButton 意识到它需要采取行动。
    • 正如我所说,您需要声明前缀-本地仅在将其添加到文件顶部时才有效(使用 xmlns:local={name of assembly}。)您需要添加一个适合您使用的前缀 - 即,如果您像我通常那样有一个名为转换器的命名空间,那么:xmlns:converters={命名空间的完全限定名称} 将正常工作。重要的是声明前缀。
    【解决方案2】:

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

    【讨论】:

    • 我可以写这个方法,但我不知道如何在xaml中使用这个方法。
    • @mo_al 我已经包含了一些 XAML;看看有没有帮助。
    猜你喜欢
    • 2011-10-17
    • 2012-06-13
    • 2015-03-31
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多