2013-07-09 21 views
0

我有以下工作代码:WPF中继控制

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

有没有办法做同样的功能,而无需复制/粘贴:)

+0

用它创建一个'ItemsControl'的'Itemtemplate'作为'RadioButton'。让你的枚举的所有条目放入一个集合属性中,比如'ObservableCollection SomeProperty'并绑定你的'ItemsControl.ItemsSource =“{Binding SomeProperty}”'。 – Viv

+0

@Viv你能帮助我吗? –

+0

确定'CaseType'是什么,每个RadioButton都是一样的?我的猜测是你的转换器与它的参数相应地调整它? – Viv

回答

4

好了不知道你的逻辑,我不能,如果你真的验证需要两个值进入转换器,其中1对于每个项目都是相同的。

但是假设你确实确实需要他们:

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> 

从顶部开始:

Items被定义为:

public List<CaseTypeEnum> Items { 
    get { 
    return Enum.GetValues(typeof(CaseTypeEnum)).Cast<CaseTypeEnum>().ToList(); 
    } 
} 

private CaseTypeEnum _caseType; 
public CaseTypeEnum CaseType { 
    get { 
    return _caseType; 
    } 
    set { 
    if (value == _caseType) 
     return; 
    _caseType = value; 
    RaisePropertyChanged(() => CaseType); 
    } 
} 

枚举:

public enum CaseTypeEnum{ 
    TypeA, 
    TypeB, 
    TypeC, 
    TypeD, 
    TypeE, 
    TypeF, 
    TypeG, 
    TypeH, 
    TypeI, 
    TypeJ, 
} 

至于两MultiBinding的,我只是把一些虚拟的代码一样

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]); 
} 

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"); 
} 

这应该给你运行时:

enter image description here

您也可以下载样本Here

+0

对不起,但为什么我们使用MultiBinding? –

+1

@ m.samy cos'ConverterParameter'不能使用Binding。 – Viv

+0

谢谢@Viv。 :) –