Reputation: 2852
I have the following working code:
<StackPanel>
<TextBlock FontSize="14" Foreground="White" Text="Case Type: " TextDecorations="Underline"/>
<RadioButton IsChecked="{Binding CaseType, Converter={StaticResource MyEnumToBooleanConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeA}}"
Style="{StaticResource ToggleButtonStyle}"
Content="{Binding CaseType, Converter={StaticResource MyEnumDescriptionConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeA}}" />
<RadioButton IsChecked="{Binding CaseType, Converter={StaticResource MyEnumToBooleanConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeB}}"
Style="{StaticResource ToggleButtonStyle}"
Content="{Binding CaseType, Converter={StaticResource MyEnumDescriptionConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeB}}" />
...
...
...
<RadioButton IsChecked="{Binding CaseType, Converter={StaticResource MyEnumToBooleanConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeJ}}"
Style="{StaticResource ToggleButtonStyle}"
Content="{Binding CaseType, Converter={StaticResource MyEnumDescriptionConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeJ}}" />
</StackPanel>
Is there any way to do the same functionality without copy/paste :)
Upvotes: 1
Views: 5573
Reputation: 17380
Ok without knowing your logic I cannot validate if you actually need two values going into the converter where 1 is the same for every item anyways.
However assuming you indeed do need them:
xaml:
<StackPanel>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.Resources>
<local:MyEnumDescriptionConverter x:Key="MyEnumDescriptionConverter" />
<local:MyEnumToBooleanConverter x:Key="MyEnumToBooleanConverter" />
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton>
<RadioButton.Content>
<MultiBinding Converter="{StaticResource MyEnumDescriptionConverter}">
<Binding Path="." />
<Binding Path="DataContext.CaseType"
RelativeSource="{RelativeSource FindAncestor,
AncestorType={x:Type ItemsControl}}" />
</MultiBinding>
</RadioButton.Content>
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource MyEnumToBooleanConverter}">
<Binding Path="." />
<Binding Path="DataContext.CaseType"
RelativeSource="{RelativeSource FindAncestor,
AncestorType={x:Type ItemsControl}}" />
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
Starting from the Top:
Items
is defined as:
public List<CaseTypeEnum> Items {
get {
return Enum.GetValues(typeof(CaseTypeEnum)).Cast<CaseTypeEnum>().ToList();
}
}
and
private CaseTypeEnum _caseType;
public CaseTypeEnum CaseType {
get {
return _caseType;
}
set {
if (value == _caseType)
return;
_caseType = value;
RaisePropertyChanged(() => CaseType);
}
}
enum:
public enum CaseTypeEnum{
TypeA,
TypeB,
TypeC,
TypeD,
TypeE,
TypeF,
TypeG,
TypeH,
TypeI,
TypeJ,
}
As for the two MultiBinding
's, I just put some dummy code like
MyEnumDescriptionConverter
-
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
if (values.Length < 2)
return string.Empty;
return string.Format("Formatted {0} with CaseType property: {1}", (CaseTypeEnum)values[0], (CaseTypeEnum)values[1]);
}
and MyEnumToBooleanConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
if (values.Length < 2)
return false;
return ((CaseTypeEnum)values[0]).ToString().EndsWith("D");
}
which should when run give you:
You can download the sample Here
Upvotes: 5