views:

687

answers:

3

I have an ItemsControl which is bound and set to an observablecollection in my viewmodel:

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

Now, how to find out which one is clicked? I would like to bind the IsChecked value of each Radiobutton to a single variable in the viewmodel that returns an index to the collection. This would make it very easy for me to directly reference the selected item. Any ideas?

A: 

This is how I solved this problem. I wrote an EnumToBool converter for this, like

  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 

    }

And I've the following enumeration

 public enum CompanyTypes
    {
        Type1Comp,
        Type2Comp,
        Type3Comp
    }

Now, in my Xaml, I'm passing the types as the converter parameter.

<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>

Now, in your view model, you should have a property (in this case Type), which is of that Enum type.

Like,

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

            }
        }

In this example, you might have noticed that Radiobuttons are static. In your case, as you are listing the radio buttons inside an Item control, you need to bind your ConverterParameter of your RadioButton as well, to the correct type.

amazedsaint
A: 

In the end, I put the radio buttons into a listview, and bind the isselected property of the listview to the radiobutton one.

link Forum post describing this technique

bluebit
A: 

When use MVVM with radiobutton control exits a problem on method onToggle(), but you can create a radiobutton for that.

 public class DataBounRadioButton: RadioButton
    {
        protected override void OnChecked(System.Windows.RoutedEventArgs e) {

        }

        protected override void OnToggle()
        {
            this.IsChecked = true;
        }
    }

Then add reference to control and Binding a property, in my case IsActive.

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