I want to bind (TwoWay) a string value ("True" or "False") to two radio buttons with Content "True" and "False".
Conditions
- When use select "True" RadioButton , string value should be "True" and vice versa for "False" radio button.
- At a time only one RadioButton is selectable either True or False.
- There is list of string and Radio Buttons are part of DataTemplate.
XAML
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding Values}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton x:Name="rbTrue" Content="True" IsChecked="{Binding Value,Mode=TwoWay}" />
<RadioButton x:Name="rbFalse" Content="False" IsChecked="{Binding Value,Mode=TwoWay}"/>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Value,Mode=TwoWay}" Value="True">
<Setter Property="IsChecked" TargetName="rbTrue" Value="True"/>
<Setter Property="IsChecked" TargetName="rbFalse" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Value,Mode=TwoWay}" Value="False">
<Setter Property="IsChecked" TargetName="rbTrue" Value="False"/>
<Setter Property="IsChecked" TargetName="rbFalse" Value="True"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
XAML.CS
public partial class MainWindow : Window
{
MainViewModel viewModel;
public MainWindow()
{
InitializeComponent();
viewModel = new MainViewModel();
this.DataContext = viewModel;
}
}
VIEWMODEL
public class MainViewModel : ModelBase
{
private List<MainModel> values;
public List<MainModel> Values
{
get
{
return values;
}
set
{
if (value != values)
{
values = value;
OnPropertyChanged("Values");
}
}
}
public MainViewModel()
{
Values = new List<MainModel>();
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
Values.Add(new MainModel() { Value = true });
else
Values.Add(new MainModel() { Value = false });
}
}
}
MODEL
public class MainModel : ModelBase
{
private bool strValue;
public bool Value
{
get
{
return strValue;
}
set
{
if (value != strValue)
{
strValue = value;
OnPropertyChanged("Value");
}
}
}
}
MODELBASE
public abstract class ModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
UI
I almost tried everything I know and also checked most of the relative links online on different forum and other material.
All I want is value either "True" or "False" based in user selection in list.
Thanks in advance.