I'm new to Avalonia and I need to generate a List of Questions and answers for one of my projects. Up to now I have generated the Questions and and Answers as I needed. Code for the XAML
<ItemsControl Items="{Binding Questions}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock
Classes="header"
Text="{Binding QuestionDescription}"
TextWrapping="Wrap" />
<ItemsControl Items="{Binding Answers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="{Binding AId}" Content="{Binding Answer}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
What I want now is to create different type of answering options (Radiobutton or checkbox) depending on the AnswerType value that I get from the Questions List. Here is my Question model
public class Question
{
public string QId { get; set; }
public string QuestionDescription { get; set; }
public List<Answers> Answers { get; set; }
public string AnswerType { get; set; }
}
public class Answers
{
public string AId { get; set; }
public string Answer { get; set; }
}
Sample data
{
"QId": "Q1",
"QuectionDescription": "Quection01",
"Answers": [
{
"AId": "Q1A1",
"Answer": "Yes"
},
{
"AId": "Q1A2",
"Answer": "No"
}
],
"AnswerType": "RadioButton"
},
{
"QId": "Q2",
"QuectionDescription": "Quection02",
"Answers": [
{
"AId": "Q2A1",
"Answer": "Football"
},
{
"AId": "Q2A2",
"Answer": "Baseball"
}
],
"AnswerType": "CheckBox"
}
public class TemplateDictionaryConverter : Dictionary<string, IDataTemplate>, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string s)
return this[s];
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotSupportedException();
}
<ItemsControl Items="{Binding Answers}">
<ItemsControl.ItemTemplate>
<Binding
Path="AnswerType">
<Binding.Converter>
<example:TemplateDictionaryConverter>
<DataTemplate x:Key="CheckBox">
<CheckBox Content="{Binding Answer}" />
</DataTemplate>
<DataTemplate x:Key="RadioButton">
<RadioButton Content="{Binding Answer}" />
</DataTemplate>
</example:TemplateDictionaryConverter>
</Binding.Converter>
</Binding>
</ItemsControl.ItemTemplate>
</ItemsControl>
Related
Consider in my wpf application, I have a checkbox and 2 textedits, as below:
<CheckBox x:Uid="Checkbox_1" FlowDirection="RightToLeft" IsChecked="{Binding TickCheckBox, Mode=TwoWay}" Style="{StaticResource StandardCheckBoxStyle}">My Checkbox</CheckBox>
<dxe:TextEdit x:Uid="dxe:TextEdit_1" Grid.Row="1" Grid.Column="1" Width="100" Style="{StaticResource FleetScheduledHoursStyle}" EditValue="{Binding RealValue, Mode=OneWay}" EditMode="InplaceInactive" ToolTipService.ShowDuration="20000" />
<dxe:TextEdit x:Uid="dxe:TextEdit_2" Grid.Row="1" Grid.Column="1" Width="100" Style="{StaticResource FleetScheduledHoursStyle}" EditValue="{Binding RealValue, Mode=OneWay}" EditMode="InplaceInactive" ToolTipService.ShowDuration="20000" />
The TickCheckBox is bound to a property in my viewmodel as below:
private bool tickCheckBox;
public bool TickCheckBox
{
get
{
return this.tickCheckBox;
}
set
{
if (this.TickCheckBox.Equals(value))
{
return;
}
this.tiketCheckBox = value;
this.NotifyPropertyChange(() => this.TickCheckBox);
}
}
How do I change the property "EditMode" of one of the textedit (say Text_Edit1) to "InplaceActive" when I ticked the checkbox?
Thanks for your help!
You can use an IValueConverter:
BoolToEditModeConverte.cs
public class BoolToEditModeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is bool isChecked))
{
throw new ArgumentException("Converter value must be of type 'bool'");
}
return isChecked
? EditMode.InplaceInactive
: EditMode.None;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
Usage
<Window>
<Window.Resources>
<BoolToEditModeConverte x:Key="BoolToEditModeConverte" />
</Window.Resources>
<CheckBox x:Name="MyCheckbox" />
<TextEdit EditMode="{Binding ElementName=MyCheckBox,
Path=IsChecked,
Converter={StaticResource BoolToEditModeConverte}}" />
</Window>
Since you're using POCOViewModel, you just define a property for the TextEdit.EditMode and binding in the xaml, and define a method for the TickCheckBox changed event in the poco view model, like this:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
this.DataContext = Vm.Create();
}
}
[POCOViewModel]
public class Vm {
public virtual bool TickCheckBox { get; set; } = false;
public virtual EditMode EditMode { get; set; } = EditMode.InplaceInactive;
public static Vm Create() => ViewModelSource.Create(() => new Vm());
protected void OnTickCheckBoxChanged() {
if (this.TickCheckBox) {
// or this.EditMode = EditMode.InplaceActive;
this.EditMode = EditMode.Standalone;
} else {
this.EditMode = EditMode.InplaceInactive;
}
}
}
and the xaml:
<StackPanel>
<CheckBox x:Uid="Checkbox_1" FlowDirection="RightToLeft" IsChecked="{Binding TickCheckBox, Mode=TwoWay}">
My Checkbox
</CheckBox>
<dxe:TextEdit
x:Uid="dxe:TextEdit_1"
EditMode="{Binding EditMode}"
EditValue="{Binding RealValue, Mode=OneWay}"
ToolTipService.ShowDuration="20000" />
<dxe:TextEdit
x:Uid="dxe:TextEdit_2"
EditMode="Standalone"
EditValue="{Binding RealValue, Mode=OneWay}"
ToolTipService.ShowDuration="20000" />
</StackPanel>
I have two models in my application:
class Line
{
string line_id { get; set; }
string color { get; set; }
}
class Point
{
string point_id { get; set; }
string line_id { get; set; }
int weight { get; set; }
}
And I have two ObservableCollection:
lines - ObservableCollection<Line>
points - ObservableCollection<Point>
I want to display two ListBox'es : first (outer) to display lines, and second (inner) to display the points which belongs to this line.
<ListView x:Name="lvPoint" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding color, Mode=TwoWay}" />
<ListBox ItemsSource="{Binding SOMETHING, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding weight, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I set DataContext for outer ListBox in the code:
lvPoint.DataContext = lines;
How can I set DataContext for inner ListBox to display Points for each Line ?
Your Line model is not good for this scenario. It should have a property such as called Points which contains the interested points belonging to the Line. Then the Binding is just simple:
class Line {
public string line_id { get; set; }
public string color { get; set; }
ObservableCollection<Point> _points;
public ObservableCollection<Point> Points {
get {
if (_points == null) _points = new ObservableCollection<Point>();
return _points;
}
}
}
Then in XAML code you can just set the Path to Points like this:
<ListBox ItemsSource="{Binding Points, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding weight, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The model above is just an example showing the main idea. The full implementation should of course be different and more advanced depending on the current project.
Update: Without using the model above, you can try using Converter for the Binding. The Binding is set directly to the current item (Line). The Converter will convert Line to points (maybe based on line_id and your query method):
public class LineToPointsConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture){
var line = value as Line;
//convert to points by querying the points based on line.line_id here
return ...
}
public object ConvertBack(object value, Type targetType,
object parameter,
System.Globalization.CultureInfo culture){
return Binding.DoNothing;
}
}
Define a static property of LineToPointsConverter or create an instance of that converter in Resources:
<Window.Resources>
<local:LineToPointsConverter x:Key="lineToPointsConverter"/>
</Window.Resources>
Then in XAML code set that converter:
<ListBox ItemsSource="{Binding Mode=TwoWay, Path=.,
Converter={StaticResource lineToPointsConverter}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding weight, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have a List< Versions> where Version, amongst others has the properties VersionUUID, Label, SKU and IsSelected. I would like to bind this to a Combobox and have the selected item just select the IsSelected flag (unselected any previous set flag).
Note:The combobox is in a template, used inside a datagrid cell, so I can not just bind a SelectedItem to the model!
What I have so far is working, the datagrid updates the DB as expected, however the initial value is not set onLoad. If one version already has a IsSelected=true, I would like to have that showing int the Combobox, but it is always empty unless i Select one from the list.
<DataTemplate x:Key="dtDatagridVersionSelector">
<ComboBox Margin="0" Width="90" Style="{StaticResource DatagridComboBox}"
ItemsSource="{Binding Path=Versions, Mode=OneTime}">
<ComboBox.ItemTemplate >
<DataTemplate >
<RadioButton Focusable="false" IsEnabled="true"
GroupName="{Binding VersionUUID}"
IsChecked="{Binding IsSelected, Mode=TwoWay}">
<StackPanel Orientation="Horizontal" >
<TextBlock Margin="3,0,0,0" Text="{Binding Label}"/>
<TextBlock Foreground="Red" Margin="3,0,0,0"
Text="{Binding SKU}"/>
</StackPanel>
</RadioButton>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="IsSelected"
Value="{Binding IsSelected, Mode=OneWay}" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</DataTemplate>
Also, the use of the Radiobox is not written in stone, if there is a better solution to achieve this so only one item isSelected, I'm all open for it
Thanx for any pointers
Andreas
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:SelectedItemConverter x:Key="selectedItemConverter"/>
</Window.Resources>
<Grid>
<ComboBox ItemsSource="{Binding Students}" SelectedItem="{Binding Students, Converter={StaticResource selectedItemConverter}}" DisplayMemberPath="Name"/>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Students = new ObservableCollection<Student>();
Students.Add(new Student() { Name = "HArish", RollNo = 1, IsSelected = false });
Students.Add(new Student() { Name = "Arev", RollNo = 2, IsSelected = false });
Students.Add(new Student() { Name = "Pankaj", RollNo = 3, IsSelected = true });
Students.Add(new Student() { Name = "Deepak", RollNo = 4, IsSelected = false });
DataContext = this;
}
public ObservableCollection<Student> Students { get; set; }
}
public class Student
{
public string Name { get; set; }
public int RollNo { get; set; }
public bool IsSelected { get; set; }
}
public class SelectedItemConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is IEnumerable<Student>)
return ((IEnumerable<Student>)value).Where(s => s.IsSelected).FirstOrDefault();
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
I hope this will help.
I have a list of items
public List<Item> MyItems { get; set; }
displayed on the DataGrid. One column shows status "icon" which is defined by template.
Code looks something like that:
Column template [...]
<DataGridTemplateColumn Header="Status">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid Height="18" Width="35">
<Rectangle Fill="{Binding Status.Background}" />
<TextBlock Text="{Binding Status.Text}" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Data model [...]
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
public int StatusId { get; set; }
public Status Status { get; set; }
}
public class Status
{
public int StatusId { get; set; }
public int Text { get; set; }
public Brush Background
{
get
{
//Colour logic goes here
}
}
}
I'd like to remove colour logic from the data model and put it to the resource file instead.
<DataGridTemplateColumn Header="Status" CellTemplate="{StaticResource MyCustomTemplate}" </DataGridTemplateColumn>
I hope so far I am going to right direction but at this point I am lost as I don't know how to bind Status property (or StatusId) to MyCustomTemplate.
If anyone could help my with this it would be great.
EDIT
This works fine.
<DataGridTemplateColumn Header="V" Width="25" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ic:CloseIcon Visibility="{Binding DockStatus, Converter={StaticResource CloseIconDisplayVisibilityConverter}}" />
<ic:DockIcon Visibility="{Binding DockStatus, Converter={StaticResource DockIconDisplayVisibilityConverter}}" />
<ic:UndockIcon Visibility="{Binding DockStatus, Converter={StaticResource UndockIconDisplayVisibilityConverter}}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
This doesn't refresh UI when model changes (DockStatus changes)
<DataGridTemplateColumn Header="V" Width="25" IsReadOnly="True" CellEditingTemplateSelector="{StaticResource DockIconCellTemplateSelector}}">
If it's only about selecting the proper background colour for a specific item, or items with a specific StatusId, you could write a binding converter. The converter would simply convert an int to a Brush:
[ValueConversion(typeof(int), typeof(Brush))]
public class StatusColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int)
{
int statusId = (int)value;
// create Brush from id here and return it
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
You would modify your Binding like this:
<Rectangle Fill="{Binding Status.StatusId, Converter={StaticResource StatusColorConverter}}" />
I've bound enums to radio buttons before, and I generally understand how it works. I used the alternate implementation from this question: How to bind RadioButtons to an enum?
Instead of enumerations, I'd like to generate a runtime-enumerated set of a custom type and present those as a set of radio buttons. I have gotten a view working against a runtime-enumerated set with a ListView, binding to the ItemsSource and SelectedItem properties, so my ViewModel is hooked up correctly. Now I am trying to switch from a ListView to a ItemsControl with radio buttons.
Here's as far as I've gotten:
<Window.Resources>
<vm:InstanceToBooleanConverter x:Key="InstanceToBooleanConverter" />
</Window.Resources>
<!-- ... -->
<ItemsControl ItemsSource="{Binding ItemSelections}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type vm:ISomeType}">
<RadioButton Content="{Binding Name}"
IsChecked="{Binding Path=SelectedItem, Converter={StaticResource InstanceToBooleanConverter}, ConverterParameter={Binding}}"
Grid.Column="0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
InstanceToBooleanConverter has the same implementation as EnumToBooleanConverter from that other question. This seems right, since it seems like it just invokes the Equals method:
public class InstanceToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? parameter : Binding.DoNothing;
}
}
The problem I am getting now is that I can't figure out how to send a runtime value as the ConverterParameter. When I try (with the code above), I get this error:
A 'Binding' cannot be set on the 'ConverterParameter' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
Is there a way to bind to the item instance, and pass it to the IValueConverter?
It turns out that it is much simpler to abandon using ItemsControl and instead go with ListBox.
It may be more heavy-weight, but that's mostly because it is doing the heavy lifting for you. It is really easy to do a two-way binding between RadioButton.IsChecked and ListBoxItem.IsSelected. With the proper control template for the ListBoxItem, you can easily get rid of all the selection visual.
<ListBox ItemsSource="{Binding Properties}" SelectedItem="{Binding SelectedItem}">
<ListBox.ItemContainerStyle>
<!-- Style to get rid of the selection visual -->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:SomeClass}">
<RadioButton Content="{Binding Name}" GroupName="Properties">
<!-- Binding IsChecked to IsSelected requires no support code -->
<RadioButton.IsChecked>
<Binding Path="IsSelected"
RelativeSource="{RelativeSource AncestorType=ListBoxItem}"
Mode="TwoWay" />
</RadioButton.IsChecked>
</RadioButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
As far as I know, there's no good way to do this with a MultiBinding, although you initially think there would be. Since you can't bind the ConverterParameter, your ConvertBack implementation doesn't have the information it needs.
What I have done is created a separate EnumModel class solely for the purpose of binding an enum to radio buttons. Use a converter on the ItemsSource property and then you're binding to an EnumModel. The EnumModel is just a forwarder object to make binding possible. It holds one possible value of the enum and a reference to the viewmodel so it can translate a property on the viewmodel to and from a boolean.
Here's an untested but generic version:
<ItemsControl ItemsSource="{Binding Converter={StaticResource theConverter} ConverterParameter="SomeEnumProperty"}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton IsChecked="{Binding IsChecked}">
<TextBlock Text="{Binding Name}" />
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The converter:
public class ToEnumModelsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var viewmodel = value;
var prop = viewmodel.GetType().GetProperty(parameter as string);
List<EnumModel> enumModels = new List<EnumModel>();
foreach(var enumValue in Enum.GetValues(prop.PropertyType))
{
var enumModel = new EnumModel(enumValue, viewmodel, prop);
enumModels.Add(enumModel);
}
return enumModels;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
The EnumModel:
public class EnumModel : INPC
{
object enumValue;
INotifyPropertyChanged viewmodel;
PropertyInfo property;
public EnumModel(object enumValue, object viewmodel, PropertyInfo property)
{
this.enumValue = enumValue;
this.viewmodel = viewmodel as INotifyPropertyChanged;
this.property = property;
this.viewmodel.PropertyChanged += new PropertyChangedEventHandler(viewmodel_PropertyChanged);
}
void viewmodel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == property.Name)
{
OnPropertyChanged("IsChecked");
}
}
public bool IsChecked
{
get
{
return property.GetValue(viewmodel, null).Equals(enumValue);
}
set
{
if (value)
{
property.SetValue(viewmodel, enumValue, null);
}
}
}
}
For a code sample that I know works (but it's still quite unpolished - WIP!), you can see http://code.google.com/p/pdx/source/browse/trunk/PDX/PDX/Toolkit/EnumControl.xaml.cs. This only works within the context of my library, but it demonstrates setting the Name of the EnumModel based on the DescriptionAttribute, which might be useful to you.
You are so close. When you are need two bindings for one converter you need a MultiBinding and a IMultiValueConverter! The syntax is a little more verbose but no more difficult.
MultiBinding Class
IMultiValueConverter Interface
Edit:
Here's a little code to get you started.
The binding:
<RadioButton Content="{Binding Name}"
Grid.Column="0">
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource EqualsConverter}">
<Binding Path="SelectedItem"/>
<Binding Path="Name"/>
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
and the converter:
public class EqualsConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values[0].Equals(values[1]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Second Edit:
The above approach is not useful to implement two-way binding using the technique linked in the question because the necessary information is not available when converting back.
The correct solution I believe is straight-up MVVM: code the view-model to match the needs of the view. The amount of code is quite small and obviates the need for any converters or funny bindings or tricks.
Here is the XAML;
<Grid>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton
GroupName="Value"
Content="{Binding Description}"
IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
and code-behind to simulate the view-model:
DataContext = new CheckBoxValueCollection(new[] { "Foo", "Bar", "Baz" });
and some view-model infrastructure:
public class CheckBoxValue : INotifyPropertyChanged
{
private string description;
private bool isChecked;
public string Description
{
get { return description; }
set { description = value; OnPropertyChanged("Description"); }
}
public bool IsChecked
{
get { return isChecked; }
set { isChecked = value; OnPropertyChanged("IsChecked"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class CheckBoxValueCollection : ObservableCollection<CheckBoxValue>
{
public CheckBoxValueCollection(IEnumerable<string> values)
{
foreach (var value in values)
this.Add(new CheckBoxValue { Description = value });
this[0].IsChecked = true;
}
public string SelectedItem
{
get { return this.First(item => item.IsChecked).Description; }
}
}
Now that I know about x:Shared (thanks to your other question), I renounce my previous answer and say that a MultiBinding is the way to go after all.
The XAML:
<StackPanel>
<TextBlock Text="{Binding SelectedChoice}" />
<ItemsControl ItemsSource="{Binding Choices}">
<ItemsControl.Resources>
<local:MyConverter x:Key="myConverter" x:Shared="false" />
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton>
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource myConverter}" >
<Binding Path="DataContext.SelectedChoice" RelativeSource="{RelativeSource AncestorType=UserControl}" />
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=Self}" />
</MultiBinding>
</RadioButton.IsChecked>
<TextBlock Text="{Binding}" />
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
The viewmodel:
class Viewmodel : INPC
{
public Viewmodel()
{
Choices = new List<string>() { "one", "two", "three" };
SelectedChoice = Choices[0];
}
public List<string> Choices { get; set; }
string selectedChoice;
public string SelectedChoice
{
get { return selectedChoice; }
set
{
if (selectedChoice != value)
{
selectedChoice = value;
OnPropertyChanged("SelectedChoice");
}
}
}
}
The converter:
public class MyConverter : IMultiValueConverter
{
object selectedValue;
object myValue;
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
selectedValue = values[0];
myValue = values[1];
return selectedValue == myValue;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
{
return new object[] { myValue, Binding.DoNothing };
}
else
{
return new object[] { Binding.DoNothing, Binding.DoNothing };
}
}
}