【问题标题】:How to get the selected index of MVVM-bound radiobuttons?如何获取 MVVM 绑定单选按钮的选定索引?
【发布时间】:2009-08-19 14:37:41
【问题描述】:

我有一个 ItemsControl,它在我的视图模型中绑定并设置为 observablecollection:

<ItemsControl ItemsSource="{Binding AwaySelection}" >
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton Content="{Binding AwayText}" ></RadioButton>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

现在,如何找出点击的是哪一个?我想将每个 Radiobutton 的 IsChecked 值绑定到视图模型中的单个变量,该变量返回集合的索引。这将使我很容易直接引用所选项目。有什么想法吗?

【问题讨论】:

    标签: c# .net wpf data-binding mvvm


    【解决方案1】:

    这就是我解决这个问题的方法。我为此写了一个 EnumToBool 转换器,比如

      public class EnumToBoolConverter : IValueConverter
        {
            #region IValueConverter Members
    
            public object Convert(object value, 
                Type targetType, object parameter, 
                System.Globalization.CultureInfo culture) 
            { 
                if (parameter.Equals(value)) 
                    return true; 
                else 
                    return false; 
            } 
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
            { 
                return parameter; 
    
            } 
            #endregion 
    
        }  
    

    我有以下枚举

     public enum CompanyTypes
        {
            Type1Comp,
            Type2Comp,
            Type3Comp
        }
    

    现在,在我的 Xaml 中,我将类型作为转换器参数传递。

    <Window x:Class="WpfTestRadioButtons.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfTestRadioButtons"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <local:EnumToBoolConverter x:Key="EBConverter"/>
        </Window.Resources>
        <Grid>
            <StackPanel>
                <RadioButton IsChecked="{Binding Path=Type, 
                    Converter={StaticResource EBConverter}, 
                    ConverterParameter={x:Static local:CompanyTypes.Type1Comp}}" Content="Type1"/>
                <RadioButton IsChecked="{Binding Path=Type, 
                    Converter={StaticResource EBConverter}, 
                    ConverterParameter={x:Static local:CompanyTypes.Type2Comp}}" Content="Type2"/>
            </StackPanel>
    
        </Grid>
    </Window>
    

    现在,在您的视图模型中,您应该有一个属于该 Enum 类型的属性(在本例中为 Type)。

    喜欢,

    public CompanyTypes Type 
            {
                get
                {
                    return _type;
                }
                set
                {
                    _type = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("Type"));
    
                }
            }
    

    在此示例中,您可能已经注意到单选按钮是静态的。在您的情况下,当您在 Item 控件中列出单选按钮时,您还需要将 RadioButton 的 ConverterParameter 绑定到正确的类型。

    【讨论】:

      【解决方案2】:

      最后,我把单选按钮放到了一个listview中,并将listview的isselected属性绑定到单选按钮上。

      link Forum post describing this technique

      【讨论】:

        【解决方案3】:

        当使用带有单选按钮控件的 MVVM 时,方法 onToggle() 会出现问题,但您可以为此创建单选按钮。

         public class DataBounRadioButton: RadioButton
            {
                protected override void OnChecked(System.Windows.RoutedEventArgs e) {
        
                }
        
                protected override void OnToggle()
                {
                    this.IsChecked = true;
                }
            }
        

        然后添加对控件的引用并绑定一个属性,在我的例子中是 IsActive。

        <controls:DataBounRadioButton 
                              IsChecked="{Binding IsActive}"/>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-19
          • 2011-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-28
          相关资源
          最近更新 更多