将文本块绑定到XAML中键的字典值?

mbm*_*voy 6 c# wpf xaml

我有一个带有枚举属性的模型(在这种情况下,与出口管制条例有关).当向用户显示值时,我想显示相应的字符串.有时这是在ComboBox中(用户可以选择一个值),有时它在TextBlock中(它是只读的).

示例:for ExportRegulationType.EAR,我想显示"EAR",而for ExportRegulationType.DoNotExport,我想要显示"Do Not Export".请注意,我没有任何语言本地化需求,但我认识到这个问题......

目前,在我的ViewModel中,我有一个属性,它返回一个基于当前枚举值的字符串,以及另一个返回一个的属性Dictionary<ExportRegulationType, string>.对于ComboBoxes,我可以绑定ItemsSource到dictionary属性,对于TextBlocks,我可以绑定到string属性.这有效,但有点笨拙.

两个问题:

1)在我看来,我应该能够申报词典(与键和值),如XAML(可能在App.xaml中)的静态资源,并使用了的ItemsSource的组合框的版本.但是,我无法弄清楚如何声明和引用这样的东西.我怎样才能做到这一点?

2)假设上面已经到位,我认为我也可以设置与textblock的绑定,因此基于enum属性,它将在字典中查找字符串.

我已经看到以下与静态或动态枚举值有关的问题.第一个是不够的,第二个没有回答......

这些应该只是一个XAML,并且可以让我从我的ViewModel中删除方法(只有一个暴露的ExportRegulationType枚举属性.这些可能吗?

编辑:附加信息:

在应用程序中,我将有许多不同的视图,模型和ViewModel.但是,由于出口管制法规是一个共同且一致的要求,我使用组合来保持干燥.即,模型A和B都具有ExportControl模型.ViewModels A1,A2,B1和B2将具有ExportControlViewModel.视图将具有绑定到其ViewModel的ExportControlViewModel的控件.视图将具有ComboBox或TextBlock,但不能同时具有两者(取决于用户是否可以更改值).

Dyl*_*dor 3

我不知道这是否适合您的情况,但这是一个可能的解决方案。在视图模型中,公开 ExportRegulationType 属性,然后创建一个值转换器来显示所需的字符串。

首先创建您的值转换器:

class ExportRegulationTypeToStringConverter: IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ExportRegulationType regType = (ExportRegulationType)value;

        switch(regType)
        {
            case ExportRegulationType.EAR:
                return "EAR";
            case ExportRegulationType.DoNotExport:
                return "Do Not Export";

            //handle other cases
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter,    System.Globalization.CultureInfo culture)
    {
        return Binding.DoNothing;
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

然后在 xaml 中添加对转换器的引用。local是您的类所在的命名空间。

<local:ExportRegulationTypeToStringConverter x:Key="exportRegConverter" />
Run Code Online (Sandbox Code Playgroud)

最后,设置文本框的值以使用转换器。pathToEnum是在 ExportRegulationType 类型的 ViewModel 上公开的属性。

<TextBlock Text="{Binding pathToEnum, Converter={StaticResource exportRegConverter}}" />
Run Code Online (Sandbox Code Playgroud)

使用ObjectDataProvider用枚举值填充 ComboBox。

<Window.Resources>
 <ObjectDataProvider x:Key="dataFromEnum"
   MethodName="GetValues" ObjectType="{x:Type System:Enum}">
      <ObjectDataProvider.MethodParameters>
           <x:Type TypeName="local:ExportRegulationType"/>
      </ObjectDataProvider.MethodParameters>
 </ObjectDataProvider>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

现在我们创建 ComboBox 并使用容器样式和值转换器来显示枚举所需的字符串。

<ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="Content" Value="{Binding Converter={StaticResource exportRegConverter}}" />
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)