wpf WPF中继器控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17549436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:10:55  来源:igfitidea点击:

WPF repeater control

wpfwpf-controlswpf-4.0

提问by Mahmoud Samy

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

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

回答by Viv

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.

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

However assuming you indeed do need them:

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

xaml:

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>

Starting from the Top:

从顶部开始:

Itemsis defined as:

Items定义为:

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

至于两者MultiBinding,我只是放了一些虚拟代码,例如

MyEnumDescriptionConverter-

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

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:

运行时应该给你:

enter image description here

在此处输入图片说明

You can download the sample Here

您可以在此处下载示例