WPF绑定到单选按钮如果单选按钮复制到同一属性。,。,。没有更新模型

时间:2013-12-06 13:03:51

标签: c# wpf mvvm

Main View

这是使用IValueConverter将单选按钮绑定到枚举属性的视图(正常工作)

如果我将这些单选按钮复制为XAML以下

 <Grid.Resources>
            <l:enumBoolConverter x:Key="ebc" />
            <l:EnumStringConverter x:Key="es" />
        </Grid.Resources>
        <StackPanel>
            <StackPanel>
                <RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=Yes}">Yes</RadioButton>
                <RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=No}">No</RadioButton>
                <RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=Maybe}">Maybe</RadioButton>
                <Separator />
                <TextBox Text="{Binding YesOrNo, Converter={StaticResource es}, Mode=OneWay}" />
            </StackPanel>
            <StackPanel>
                <RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=Yes}">Yes</RadioButton>
                <RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=No}">No</RadioButton>
                <RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=Maybe}">Maybe</RadioButton>
                <Separator />
                <TextBox Text="{Binding YesOrNo, Converter={StaticResource es}, Mode=OneWay}" />
            </StackPanel>
        </StackPanel>

我可以执行更改选项从“是”到“否”但是当我从“否”更改为“是”时,存在不会更新属性或视图的问题。

为什么..

我想是的,因为“No”选项的第二个单选按钮再次被调用来设置属性“YesOrNo”,而我将First更改为“Yes”

是这样的.. ???什么是操作组...到一个属性的解决方案..并获得所有绑定的更新..?

价值转换的CS代码

[ValueConversion(typeof(YesOrNoOptions), typeof(bool))]
    class enumBoolConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var ps = parameter as string;
            if (string.IsNullOrEmpty(ps))
                return DependencyProperty.UnsetValue;
            if (!Enum.IsDefined(typeof(YesOrNoOptions), value))
                return DependencyProperty.UnsetValue;

            var param = Enum.Parse(value.GetType(), ps);

            var ret = param.Equals(value);

            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var ps = parameter as string;
            if (string.IsNullOrEmpty(ps))
            {
                return DependencyProperty.UnsetValue;
            }
            return Enum.Parse(targetType, ps);
        }
    }

    [ValueConversion(typeof(YesOrNoOptions), typeof(string))]
    internal class EnumStringConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Enum v = (Enum)value;
            return v.ToString();
        }

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

ViewModel的CS代码

public enum YesOrNoOptions
    {
        Yes,
        No,
        Maybe
    }
    public class DemDC : INotifyPropertyChanged
    {
        public DemDC()
        {
            _yt = YesOrNoOptions.Yes;
        }
        public YesOrNoOptions _yt;
        public YesOrNoOptions YesOrNo
        {
            get
            {
                return _yt;
            }
            set
            {
                _yt = value;
                Notify("YesOrNo");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void Notify(string pname)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(pname));
        }
    }

提前致谢...

所有视图的逻辑应该如何相同不是吗?

从“是”到“否”,两个面板都更新正常.. 但是当从“否”返回“是”时,它会保留在“否”选项中。

因为第二个单选按钮将“No”值设置为枚举... ???操作“是”不是吗???

如果发生这种情况..?选择“是”到“否”时为什么不发生?????

1 个答案:

答案 0 :(得分:1)

要绕过此问题,您可以在更新属性时禁用属性更改:

private bool suppress;
private YesOrNoOptions _yt;
public YesOrNoOptions YesOrNo
{
    get
    {
        return _yt;
    }
    set
    {
        if (_yt != value && !suppress)
        {
            _yt = value;
            suppress = true;
            Notify("YesOrNo");
            suppress = false;
        }
    }
}