WPF中继器控制

时间:2013-07-09 13:27:18

标签: wpf wpf-controls wpf-4.0

我有以下工作代码:

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

有没有办法在没有复制/粘贴的情况下执行相同的功能:)

1 个答案:

答案 0 :(得分:5)

好的,不知道你的逻辑我无法验证你是否真的需要两个值进入转换器,其中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