WPF RadioButton更改未更新UI MVVM

时间:2011-01-23 13:06:11

标签: wpf mvvm

我有两个单选按钮用作使用MVVM的UI中的radioButton List。第一次加载用户控件时,会选择一个单选按钮,并在UI中显示相关控件...现在,当我更改单选按钮时,UI不会更新。

以下是示例XAML:

<Label Grid.Column="0" Grid.Row="3" Content="Exchange Details:" Margin="3" VerticalContentAlignment="Center" Style="{StaticResource NormalLabelStyle}"></Label>
 <Grid Grid.Column="1" Grid.Row="3" Width="200">
  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="Auto"/>
   <ColumnDefinition Width="20"/>
  <ColumnDefinition Width="Auto"/>
 </Grid.ColumnDefinitions>
<RadioButton GroupName="rdoExchange" Content="Basic" IsChecked="{Binding Path=ExchangeDetailsBasic}"  Grid.Column="0" VerticalContentAlignment="Center" VerticalAlignment="Center"></RadioButton>
<RadioButton GroupName="rdoExchange" Content="Advanced" IsChecked="{Binding Path=ExchangeDetailsAdvanced}" Grid.Column="2" VerticalContentAlignment="Center" VerticalAlignment="Center"></RadioButton
 </Grid> 

 <Label Grid.Column="3" Grid.Row="0" Content="Number of Mailbox Profiles:" VerticalContentAlignment="Center" Style="{StaticResource NormalLabelStyle}" Visibility="{Binding Path=IsAdvanced}" ></Label>
 <telerik:RadNumericUpDown Grid.Column="4" Grid.Row="0" Margin="3" Value="{Binding Path=NumberofMailboxProfiles}" IsInteger="True" Minimum="1" Maximum="4"  HorizontalAlignment="Left" Visibility="{Binding Path=IsAdvanced}">< /telerik:RadNumericUpDown>

以下是我的ViewModel代码:

 private enum ExchangeDetails{
        Basic,
        Advanced
 }

 private bool isBasicMode = true;

 public bool ExchangeDetailsBasic {
         get {
            return this.isBasicMode;
        }

        set {
            if (value) {
                this.applicationSpecificRequirements[ExchangeDetailsKey] = ExchangeDetails.Basic.ToString();
                if (!this.isBasicMode) {
                    this.CheckBasicOrAdvancedSelecteAndDisplayView();
                }
            }
        }
    }

 public bool ExchangeDetailsAdvanced {
        get {
            return !this.isBasicMode;
        }

        set {
            if (value) {
                this.applicationSpecificRequirements[ExchangeDetailsKey] = ExchangeDetails.Advanced.ToString();
                this.CheckBasicOrAdvancedSelecteAndDisplayView();
            }
        }
    }

    public Visibility IsAdvanced { get; private set; }

    private void CheckBasicOrAdvancedSelecteAndDisplayView() {
        this.isBasicMode = this.applicationSpecificRequirements.ContainsKey(ExchangeDetailsKey) ? (this.applicationSpecificRequirements[ExchangeDetailsKey].Equals(ExchangeDetails.Basic.ToString()) ? true : false) : true;
        this.IsAdvanced = this.isBasicMode ? Visibility.Collapsed : Visibility.Visible;
    }

4 个答案:

答案 0 :(得分:8)

单选按钮,组和绑定不会混合。令人惊讶的是,这是设计的。

有三种方法可以更改UI中绑定控件的值。一个是用户可以通过鼠标点击或按键自己完成。第二个是代码可以更改数据源的值,绑定将更新UI中的值。

第三种方法是在代码中明确设置值。如果这样做,您刚刚设置的控件上的绑定将被禁用。

这有点违反直觉。您希望新值被推送到数据源。设计假设是,如果您希望在数据源中更改值,则需要在数据源中更改它,并且您的代码正在操作UI,因为您想要它再受约束。这为您提供了一种手动覆盖绑定的简单方法 - 只需在代码中设置控件的值 - 这不会强迫您查找Binding对象并显式操作它。这有一定的意义。我想。

但它会产生单选按钮的问题。因为分组单选按钮会在代码中更改彼此的值。如果组中有三个单选按钮,并且一个单选按钮被检查,则单选按钮会找到组中的其他按钮并取消选中它们。如果你看一下Reflector中的代码,你可以看到这个。

所以会发生什么正是您所观察到的:您单击单选按钮并禁用绑定。

以下是你对此采取的措施 - 这实际上具有相当大的意义。不要使用组。您可以使用单选按钮,但仅限于其视觉样式。忽略他们的分组功能。

相反,在视图模型中实现使绑定的布尔属性互斥的逻辑,例如:

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

如果你考虑一下,无论如何这个逻辑真的不应该在视图中。因为它是逻辑,这就是视图模型的用途。因此,虽然这有点痛苦,但你可以安慰自己,从结构上来说这是正确的事情。

答案 1 :(得分:0)

如果在视图模型中实现了INotifyPropertyChanged,并且在XAML中设置了Binding Mode = TwoWay,则可以让绑定为您完成剩下的工作。

以下是使用您的一些代码的示例:

<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <RadioButton GroupName="rdoExchange" Content="Basic" 
                 IsChecked="{Binding Path=ExchangeDetailsBasic, Mode=TwoWay}"  
                 Grid.Column="0" 
                 VerticalContentAlignment="Center" 
                 VerticalAlignment="Center"/>
    <RadioButton GroupName="rdoExchange" Content="Advanced" 
                 IsChecked="{Binding Path=ExchangeDetailsAdvanced, Mode=TwoWay}" 
                 Grid.Column="1" 
                 VerticalContentAlignment="Center" 
                 VerticalAlignment="Center"/>
    <Label Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" 
           Content="Number of Mailbox Profiles:" 
           VerticalContentAlignment="Center" 
           Visibility="{Binding Path=IsAdvanced, Mode=TwoWay}" />
</Grid>

这是ViewModel:

public class MainViewModel : ViewModelBase
{
  public MainViewModel()
  {
  }

  private bool _isBasicMode = true;
  public bool ExchangeDetailsBasic
  {
    get
    {
      return this._isBasicMode;
    }

    set
    {
      this._isBasicMode = value;
      if (value)
      {
        ExchangeDetailsAdvanced = false;
        IsAdvanced = Visibility.Collapsed;
      }
      this.OnPropertyChanged("ExchangeDetailsBasic");
    }
  }

  private bool _isAdvancedMode = false;
  public bool ExchangeDetailsAdvanced
  {
    get
    {
      return this._isAdvancedMode;
    }

    set
    {
      _isAdvancedMode = value;
      if (value)
      {
        ExchangeDetailsBasic = false;
        IsAdvanced = Visibility.Visible;
      }
      this.OnPropertyChanged("ExchangeDetailsAdvanced");
    }
  }

  private Visibility _isAdvanced = Visibility.Collapsed;
  public Visibility IsAdvanced
  {
    get
    {
      return _isAdvanced;
    }
    set
    {
      _isAdvanced = value;
      this.OnPropertyChanged("IsAdvanced");
    }
  }
}

这是实现INotifyPropertyChanged的基类。

public abstract class ViewModelBase : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

  protected void OnPropertyChanged(string propertyName)
  {
     PropertyChangedEventHandler handler = PropertyChanged;

     if (handler != null)
     {
        handler(this, new PropertyChangedEventArgs(propertyName));
     }
  }
}

答案 2 :(得分:0)

我猜你错过了视图模型类的INotifyPropertyChanged实现。如果您使用了双向数据绑定,并且在选择更改时提升属性更改事件,则一切正常。 @Zamboni用代码示例解释了它。

答案 3 :(得分:0)

罗伯特·罗斯尼的答案很棒,但我仍然认为单选按钮应该像单选按钮一样,让虚拟机处理更重要的逻辑。

这是我的解决方案:一个附加属性,用于切换同一组中所有按钮的IsChecked属性。适用于我的机器: - )

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Elca.MvvmHelpers {

    public class RadioButtonHelper : DependencyObject {

        private static readonly Dictionary<string, List<RadioButton>> s_group2ButtonsMap = new Dictionary<string, List<RadioButton>>();
        private static readonly List<RadioButton> s_knownButtons = new List<RadioButton>(); 

        private static void OnRadioButtonChecked(object sender, RoutedEventArgs e) {
            RadioButton rb = (RadioButton)sender;
            UncheckOtherButtonsInGroup(rb);
        }

        public static bool? GetIsChecked(RadioButton d) {
            return (bool?) d.GetValue(IsCheckedProperty);
        }

        public static void SetIsChecked(RadioButton d, bool? value) {
            d.SetValue(IsCheckedProperty, value);
        }

        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.RegisterAttached("IsChecked", 
                                                typeof(bool?), 
                                                typeof(RadioButtonHelper),
                                                new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Journal |
                                                                                     FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                                                              IsCheckedChanged));

        public static void IsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {

            var rb = d as RadioButton;
            if (rb == null) {
                throw new Exception("IsChecked attached property only works on a FrameworkElement type");
            }

            RememberRadioButton(rb);

            if ((bool) e.NewValue) {

                rb.IsChecked = true; // this triggers OnRadioButtonChecked => other buttons in the same group will be unchecked
            }
        }

        private static void RememberRadioButton(RadioButton rb) {
            var groupName = GetGroupName(rb);

            // if this button is unknown, add it to the right list, based on its group name
            if (s_knownButtons.Contains(rb)) {
                return;
            }

            s_knownButtons.Add(rb);

            List<RadioButton> existingButtons;
            if (! s_group2ButtonsMap.TryGetValue(groupName, out existingButtons)) {
                // unknown group
                s_group2ButtonsMap[groupName] = new List<RadioButton> {rb};
                RegisterButtonEvents(rb);
            } else {
                if (! existingButtons.Contains(rb)) {
                    existingButtons.Add(rb);
                    RegisterButtonEvents(rb);
                }
            }
        }

        private static void RegisterButtonEvents(RadioButton rb) {
            rb.Unloaded += OnButtonUnloaded;
            rb.Checked += OnRadioButtonChecked;
        }

        private static void OnButtonUnloaded(object sender, RoutedEventArgs e) {
            RadioButton rb = (RadioButton) sender;

            ForgetRadioButton(rb);
        }

        private static void ForgetRadioButton(RadioButton rb) {

            List<RadioButton> existingButtons = s_group2ButtonsMap[GetGroupName(rb)];
            existingButtons.Remove(rb);
            s_knownButtons.Remove(rb);

            UnregisterButtonEvents(rb);
        }

        private static void UnregisterButtonEvents(RadioButton rb) {
            rb.Unloaded -= OnButtonUnloaded;
            rb.Checked -= OnRadioButtonChecked;
        }

        private static void UncheckOtherButtonsInGroup(RadioButton rb) {

            List<RadioButton> existingButtons = s_group2ButtonsMap[GetGroupName(rb)];

            foreach (RadioButton other in existingButtons) {

                if (other != rb) {
                    SetIsChecked(other, false);
                }
            }
            SetIsChecked(rb, true);

        }

        private static string GetGroupName(RadioButton elt) {
            string groupName = elt.GroupName;
            if (String.IsNullOrEmpty(groupName)) {
                groupName = "none"; // any value will do
            }
            return groupName;
        }
    }
}

在视图中,对于每个按钮:

<RadioButton MvvmHelpers:RadioButtonHelper.IsChecked="{Binding IsExplicitFileSelected, Mode=TwoWay}">
...
</RadioButton>

VM为每个单选按钮都有一个布尔属性。必须为每个这样的属性赋值,以启动附加属性的监听过程。

没有组名的所有按钮都被视为属于同一组。