0

我是 WPF 的新手,在这里我试图在我的窗口上选中一个特定的单选按钮时设置我的 viewModel 的简单字符串属性。

class ViewModel
{
  string LanguageSettings {get;set;}
}

XAML 如下所示:

 <RadioButton Name="OptionEnglish" GroupName="LanguageOptions" IsChecked="{Binding LanguageSettings, Converter={StaticResource Converter}, ConverterParameter=English}" Content="English" HorizontalAlignment="Right" Width="760" />
        <RadioButton Name="OptionChinese" GroupName="LanguageOptions" IsChecked="{Binding LanguageSettings, Converter={StaticResource Converter}, ConverterParameter=Chinese}" Content="Chinese" />

我已经实现了 IValueConverted,如下所示:

 public class BoolInverterConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                return !(bool)value;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                return !(bool)value;
            }
            return value;
        }

        #endregion
    }

可能我没有正确理解 IValueConverter 的实用性。我想如果我适当地改变它,它可能会起作用。

All I want here is that when English is selected I want Language Settings to be set as English and same for Chinese. 有什么简单的方法吗?有没有直接的方法来设置该属性?

所以我有两个选择

1.更改我的 BoolConverterImplementation 2. 找到另一种更简单的方法。

有任何想法吗?

4

4 回答 4

2

不需要转换器的方法:

<RadioButton Content="Chinese" IsChecked="{Binding IsChinese}"/>
<RadioButton Content="English" IsChecked="{Binding IsEnglish}"/>

视图模型:

public class LanguageSelectorViewModel
{
    private bool _isChinese;
    private bool _isEnglish;

    public bool IsChinese
    {
        get { return _isChinese; }
        set
        {
            _isChinese = value;

            if (value)
                SelectedLanguage = "Chinese";
        }
    }

    public bool IsEnglish
    {
        get { return _isEnglish; }
        set
        {
            _isEnglish = value;

            if (value)
                SelectedLanguage = "English";
        }
    }

    public string SelectedLanguage { get; set; }
}
于 2013-09-18T21:12:13.740 回答
1

您应该考虑实施 IValueConverter。以下是我相信您正在寻找的一个起始示例:

public class StringMatchConverter : IValueConverter
{
    public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
    {
        return value == parameter;
    }

    public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

示例用法:

<!-- Somewhere in resources -->
<conv:StringMatchConverter x:Key="Conv" />

<!-- Somewhere in application -->
<RadioButton Name="OptionEnglish" GroupName="LanguageOptions" IsChecked="{Binding LanguageSettings,Converter={StaticResource Conv}, ConverterParameter=English}" Content="English" HorizontalAlignment="Right" Width="760" />
<RadioButton Name="OptionChinese" GroupName="LanguageOptions" IsChecked="{Binding LanguageSettings,Converter={StaticResource Conv}, ConverterParameter=Chinese}" Content="Chinese" />
于 2013-09-18T20:53:55.703 回答
0

一种方法是创建一个附加属性:

public class MyAttachedProperty
{
    public static readonly DependencyProperty IsCheckedToStrProperty =
        DependencyProperty.RegisterAttached("IsCheckedToStr", typeof (string), typeof (MyAttachedProperty), new PropertyMetadata(default(string,IsCheckedToStr)))

        private static void IsCheckedToStr(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RadioButton radio = d as RadioButton;
            radio.Checked+=radio_Checked;

        }

        private static void radio_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton radio = sender as RadioButton;

            if (radio.IsChecked == true)
            {
                SetIsCheckedToStr(radio, radio.Content.ToString());
            }
        }

    public static void SetIsCheckedToStr(UIElement element, string value)
    {
        element.SetValue(IsCheckedToStrProperty, value);
    }

    public static string GetIsCheckedToStr(UIElement element)
    {
        return (string) element.GetValue(IsCheckedToStrProperty);
    }
}

然后你可以像这样在你的 xaml 中使用它:

<RadioButton Name="OptionEnglish" GroupName="LanguageOptions" local:MyAttachedProperty.IsCheckedStr="{Binding LanguageSettings}" Content="English" HorizontalAlignment="Right" Width="760" />
<RadioButton Name="OptionChinese" GroupName="LanguageOptions" local:MyAttachedProperty.IsCheckedStr="{Binding LanguageSettings}" Content="Chinese" />
于 2013-09-18T21:14:46.673 回答
0

我不确定,但如果我理解你是正确的,你也许可以使用IValueConverter

于 2013-09-18T20:43:04.053 回答