Reputation: 20242
I have
class Student
{
public List<Degree> Degrees {get;set;}
}
class Degree
{
public Subject Subject{get;set;}
public int Value {get;set;}
}
class Subject
{
English =0,
Germany = 1,
Biology=2
}
And I have a Treeview
<HierarchicalDataTemplate DataType="{x:Type MyService:Student}" >
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="English: "/>
<CheckBox IsChecked="{Binding Path=Degrees.Where(d=>d.Subject==English).First()}, Converter={StaticResource degreeToBoolIsPassed}"> </CheckBox>
<TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="Germany: "/>
<CheckBox IsChecked="{Binding Path=Degrees.Where(d=>d.Subject==Germany).First()}, Converter={StaticResource degreeToBoolIsPassed}"> </CheckBox>
etc
Architecture of classed couldn't be change ,degreeToBoolIsPassed is easy to do, so I'm only want to xaml from you, or .cs of this control. I know that:
<CheckBox IsChecked="{Binding Path=Degrees.Where(d=>d.Subject==Germany).First()}, Converter={StaticResource degreeToBoolIsPassed}"> </CheckBox>
doesn't work. It is only example what I want.
Important thing:
Every student must have 4 checkboxes
Upvotes: 0
Views: 2403
Reputation: 27055
You will need a ValueConverter:
Bind to Degrees:
IsChecked="{Binding Path=Degrees, Converter={StaticResource DegreesConverter}, ConverterParameter={x:Static namespace:Subject.Germany}}
Implement a nice converter somewhere:
public class DegreesConverter: IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
List<Degree> degrees = (List<Degree>) value;
return degrees.Any(d => Equals(d.Subject, (Subject)parameter));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Register your converter in the resources somewhere under the key DegreesConverter like so:
<namespace:DegreesConverter x:Key="DegreesConverter" />
Note that the namespace prefix should also be registered in the Control you are using this stuff in, but I think you will know how to do this!
Good luck!
Upvotes: 1