用于单选按钮的WPF转换器不起作用

时间:2011-02-28 21:35:57

标签: wpf binding radio-button converter

我有两个绑定到相同属性的单选按钮。单选按钮有2个转换器。但第二次复选框的转换仅在第一次发生。代码中是否有问题。

<RadioButton Margin="5,1" GroupName="groupValueOrTime" Name="radioButtonTimeDriven" VerticalAlignment="Top" IsChecked="{Binding Path=TriggerType ,Converter={StaticResource dailyTriggerConverter}}"  Grid.Column="0" Grid.Row="0" >Time Driven</RadioButton>
<RadioButton Margin="5,1" GroupName="groupValueOrTime" Name="radioButtonValueDriven" VerticalAlignment="Top" Grid.Column="0" Grid.Row="1" IsChecked="{Binding Path=TriggerType,Converter={StaticResource valueDrivenTriggerConverter}}" >Value Driven</RadioButton>

代码:

public class TriggerTypeDailyToBoolProperty:IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
       //convert from TriggerType to bool:
        int TriggerType=int.Parse(value.ToString());
        if (TriggerType == 0 || TriggerType == 1 || TriggerType == 2 || TriggerType == 3 || TriggerType == 4 || TriggerType == 5 || TriggerType == 6)
            return true;
        else
            return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool isChecked = (bool)value;
        return (isChecked? 0: -1);
    }

    #endregion
}

public class TriggerTypeValueDrivenToBoolProperty : IValueConverter
{
   #region IValueConverter Members

   public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
       //convert from TriggerType to bool:
       int TriggerType = int.Parse(value.ToString());
       if (TriggerType == 9)
           return true;
       else
           return false;
   }

   public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
       bool isChecked = (bool)value;
       return (isChecked ? 9 : -1);
   }

   #endregion
}

2 个答案:

答案 0 :(得分:1)

我认为你需要在视图模型中有两个不同的布尔属性。

请参阅下面描述viewmodel的示例。

public class TriggerViewModel : INotifyPropertyChanged
{
    private bool _triggerTypeValue;
    private bool _triggerTypeTime;
    public bool TriggerTypeValue
    {
        get
        {
            return _triggerTypeValue;
        }
        set
        {
            _triggerTypeValue = value;
            OnPropertychanged("TriggerTypeValue");
            SetTriggerTypeTime(!_triggerTypeValue);
        }
    }

    public bool TriggerTypeTime
    {
        get
        {
            return _triggerTypeTime;
        }
        set
        {
            _triggerTypeTime = value;
            OnPropertychanged("TriggerTypeTime");
            SetTriggerTypeValue(!_triggerTypeTime);
        }
    }

    public TriggerViewModel()
    {
        _triggerTypeValue = false;
        _triggerTypeTime = true;
    }

    private void SetTriggerTypeTime(bool value)
    {
        _triggerTypeTime = value;
        OnPropertychanged("TriggerTypeTime");
    }

    private void SetTriggerTypeValue(bool value)
    {
        _triggerTypeValue = value;
        OnPropertychanged("TriggerTypeValue");
    }

    private void OnPropertychanged(string propertyName)
    {
        if(PropertyChanged!= null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

对于XAML

<RadioButton Margin="5,1" GroupName="groupValueOrTime" Name="radioButtonTimeDriven" 
             VerticalAlignment="Top" IsChecked="{Binding Path=TriggerType}"  
             Grid.Column="0" Grid.Row="0">
     Time Driven 
</RadioButton>

<RadioButton Margin="5,1" GroupName="groupValueOrTime" Name="radioButtonValueDriven" 
             VerticalAlignment="Top" Grid.Column="0" Grid.Row="1" 
             IsChecked="{Binding Path=TriggerType}">
      Value Driven
<RadioButton>

注意:此方法会删除转换器,并允许您将业务逻辑保留在中央视图模型中。

答案 1 :(得分:1)

组合分组,单选按钮和绑定不起作用。绑定的一个奇怪特性是,如果在代码中设置绑定目标属性的值,则会禁用绑定。假设绑定的设计似乎是你应该更改目标属性的唯一方法是使用UI或更改它绑定的源属性,如果一段代码显式设置了绑定的值目标属性,它知道它在做什么。

不幸的是,这意味着分组的单选按钮会在用户点击它们时在代码中设置彼此的值,从而破坏了它们的绑定。糟糕。

解决方案很简单:将单选按钮绑定到视图模型中的属性,消除分组,并将逻辑放在视图模型中以处理绑定属性的互斥性质,例如:

private bool _Option1;

public bool Option1
{
   get { return _Option1; }
   set
   {
      if (value != _Option1)
      {
         _Option1 = value;
         if (value)
         {
            Option2 = false;
            Option3 = false;
         }
      }
      OnPropertyChanged("Option1");
   }
}