1

我有以下工作代码:

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

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

4

1 回答 1

5

好的,在不知道您的逻辑的情况下,我无法验证您是否真的需要将两个值输入转换器,其中 1 对于每个项目都是相同的。

但是,假设您确实需要它们:

xml:

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

运行时应该给你:

在此处输入图像描述

您可以在此处下载示例

于 2013-07-09T14:52:33.457 回答