使用IValueConverter将RadioButton绑定到Page属性

时间:2016-06-01 10:14:32

标签: c#

我在使用WP8.1应用程序中的IValueConverter将属性绑定到RadioButton时遇到问题。 这是我的代码。

主页中的

public sealed partial class MainPage : Page
{
    private ObservableDictionary defaultViewModel = new ObservableDictionary();
    public ObservableDictionary DefaultViewModel
    {
        get { return this.defaultViewModel; }
    }

    private int radioValue;
    public int RadioValue
    {
        get { return radioValue; }
        set
        {
            if (value != -1)
            {
                radioValue = value;
                /*some important action*/
            }
        }
    }
...
public MainPage()
{
    this.InitializeComponent();
    ...
    RadioValue = 1;
    this.DefaultViewModel["RadioValue"] = RadioValue;

}

然后在RadioValueConverter.cs文件中:

class RadioValueConverter : IValueConverter
{
    public Object Convert(Object value, Type targetType, Object parameter, string language)
    {
        if (value == null || parameter == null)
        {
            return false;
        }
        if (value is int)
            return Int32.Parse((string)parameter) == (int)value;
        else return parameter.Equals(value);
    }

    public Object ConvertBack(Object value, Type targetType, Object parameter, string language)
    {
        if (parameter == null || (bool)value == false)
        {
            return -1;
        }
        if (parameter is string)
            return Int32.Parse((string)parameter);
        else return parameter;
    }
}

最后在XAML中:

...
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Mode=Self}}"

...

<Page.Resources>
    <ResourceDictionary>
        <local:RadioValueConverter x:Key="RadioValueConverter"/>
    </ResourceDictionary>
</Page.Resources>

...

<RadioButton
     GroupName="RadioValue"
     IsChecked="{Binding RadioValue, ConverterParameter=1, Converter={StaticResource RadioValueConverter}, Mode=TwoWay}"
            >

问题是RadioValue set方法永远不会从RadioValueConverter调用,因此它不会更改其值并且不会执行我需要的任务。 谁能告诉我我的代码有什么问题? 提前谢谢,问候。

1 个答案:

答案 0 :(得分:0)

您将DataContext设置为ObservableDictionary。当您尝试绑定到属性RadioValue时,WPF反射将查找DefaultViewModel.RadioValue而不是DefaultViewModel["RadioValue"]。 WPF反射不会查找MainPage.RadioValue,因为您更改了DataContext

DefaultViewModel.RadioValue显然ObservableDictionary不存在ObservableDictionary

如果要将DataContext用作/// <summary> /// A class that you can set as your DataContext /// </summary> public class YourViewModel : INotifyPropertyChanged { private int _radioValue; /// <summary> /// The RadioValue that your binding will find /// </summary> public int RadioValue { get { return _radioValue; } set { if ( value != -1 ) { _radioValue = value; /*some important action*/ OnPropertyChanged(); } } } /// <summary> /// Implementation of INotifyPropertyChanged event /// </summary> public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// Implementation of INotifyPropertyChanged /// </summary> /// <param name="propertyName">The name of the property that has changed</param> protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = null ) { PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) ); } }

,则必须将DataContext替换为您自己的班级

类似的东西:

Constructor

修改:

要将MainPage设置为您自己的班级,您可以在this.DataContext=new YourViewModel(); <span>{{row.entity.completeStatus}}</span> <uib-progressbar value='row.entity.completeStatus'></uib-progressbar>中执行此操作: completeStatus