【问题标题】:RaisePropertyChanged sets back the value of the radio buttonsRaisePropertyChanged 设置单选按钮的值
【发布时间】:2014-12-30 02:30:19
【问题描述】:

如果我想通过 RaisePropertyChanged 更新我的 UI,我会遇到一个奇怪的行为。 我使用this post 的第二个解决方案(Johnathan1):我实现了 RadioBoolToIntConverter。

我的虚拟机如下所示:

public int myFilterRadioButtonInt
        {
            get
            {
                return _Filter.FilterMyProperty ? 1 : 2;
            }
            set
            {
                if (value == 1)
                    _Filter.FilterMyProperty = true;
                else if (value == 2)
                    _Filter.FilterMyProperty = false;
                else
                    return;

                RaisePropertyChanged("myFilterRadioButtonInt");
            }
        }

转换器看起来像这样(this post 的 Jonathan1):

public class RadioBoolToIntConverter : IValueConverter

    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int integer = (int)value;
            if (integer==int.Parse(parameter.ToString()))
                return true;
            else
                return false;
        }

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

为了理解_Filter.FilterMyProperty 是模型的布尔值,如果显示或不显示我将被过滤的值,则该值负责。这使用 RadioBoolToIntConverter 绑定到 2 个 RadioButton:

<RadioButton IsChecked="{Binding Path=myFilterRadioButtonInt, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=1}">Show</RadioButton>
 <RadioButton IsChecked="{Binding Path=myFilterRadioButtonInt, Converter={StaticResource RadioBoolToIntConverter}, ConverterParameter=2}">Don't show</RadioButton>

绑定和切换 RadioButtons 工作正常。

问题是如果我通过代码设置_Filter.FilterMyProperty = true(设置一个标准过滤器来过滤这个值)然后做RaisePropertyChanged("myFilterRadioButtonInt")_Filter.FilterMyProperty将被设置为false

编辑:

RaisePropertyChanged("myFilterRadioButtonInt")(由 VM 中 Filter 属性的设置器调用)再次调用 myFilterRadioButtonInt 的设置器,它将设置 RadioBox 的当前值(在我的情况下,value 是 @987654334 @ 所以setter会将_Filter.FilterMyProperty设置回false

使用此方法无法通过代码更改 RadioBoxes 的值。我以为当我调用RaisePropertyChanged("myFilterRadioButtonInt") 时只会调用getter。 我该如何解决这个问题,为什么 RaisePropertyChanged("myFilterRadioButtonInt") 会调用 setter?

编辑:

在 ConvertBack() 中返回参数是问题所在。这是我的解决方案:

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    bool val;
    if (!bool.TryParse(value.ToString(), out val))
        return 0;

    int iParam;
    if (!int.TryParse(parameter.ToString(), out iParam))
        return 0;

    return val ? iParam : 0;
}

【问题讨论】:

  • RaisePropertyChanged("myFilterRadioButtonInt");不会更改 _Filter.FilterMyProperty 的值。发布转换器。我敢打赌转换器正在改变价值。
  • @Blam 将转换器添加到问题中。
  • 通常只调用getter,否则(如果调用setter)会有一些StackOverflow异常,显然有调用RaisePropertyChanged 在设置器中。所以我怀疑还有其他一些代码触发了setter,而不是RaisePropertyChanged()

标签: c# wpf xaml mvvm data-binding


【解决方案1】:

我发现这个article 提供了一致的解决方案。我在 OP 中尝试了该解决方案,但它对我来说并不能始终如一地工作——我有随机实例,其中选择的单选按钮没有正确反映。

ConvertBack 应该是:

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.Equals(false) ? DependencyProperty.UnsetValue : parameter;
    }

【讨论】:

  • 你,好先生,是我的英雄!我以为我会为此失去理智,但您的解决方案非常有效!
【解决方案2】:

为什么要将参数作为值返回

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

第一个按钮每次都将值设置为 true,第二个按钮每次都设置为 false。

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    Bool vBool = Bool.Parse(value);
    Int iParam = IntParse(parameter);
    if(iParm > 2 || iParem < 0) return false;
    if(vBool) return iParam;
    else if (iParam == 2) return 1;
    else return 2;
}

如果当前值正确,您也应该立即返回,而不是调用 RaisePropertyChanged。

【讨论】:

  • 返回参数是问题所在。我添加了问题的解决方案。
猜你喜欢
  • 2015-03-22
  • 2015-11-28
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 2013-07-24
  • 2014-05-12
  • 1970-01-01
相关资源
最近更新 更多