【问题标题】:WPF XAML pass to ConverterParameter array of enumsWPF XAML 传递给 ConverterParameter 枚举数组
【发布时间】:2023-03-06 00:36:01
【问题描述】:

我有枚举

public enum DocumentTypes
{
    First, Second, Third, Fourth
}

如何将enum 的值传递给<sys:Enum></sys:Enum>

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1.Converters"
xmlns:enums="clr-namespace:WpfApplication1.Enums"
xmlns:sys="clr-namespace:System;assembly=mscorlib"

       <Label Content="Test2">
            <Label.Visibility>
                <MultiBinding Converter="{StaticResource Converter}">
                    <MultiBinding.ConverterParameter>
                        <x:Array Type="{x:Type sys:Enum}">
                            <sys:Enum></sys:Enum>
                        </x:Array>
                    </MultiBinding.ConverterParameter>
                    <Binding ElementName="First" Path="IsChecked" />
                    <Binding ElementName="Second" Path="IsChecked" />
                    <Binding ElementName="Third" Path="IsChecked" />
                    <Binding ElementName="Fourth" Path="IsChecked" />
                </MultiBinding>
            </Label.Visibility>
        </Label>

【问题讨论】:

  • IsChecked 属性是 bool。您确定这就是您想要传递给转换器的内容吗?
  • @MikeEason,我知道 isChecked 是一个布尔值。这只是一个例子。我不知道如何通过 x:array 枚举。

标签: c# wpf xaml enums


【解决方案1】:

这样做:

    <x:Array Type="{x:Type sys:Enum}">
        <local:DocumentTypes>First</local:DocumentTypes>
        <local:DocumentTypes>Second</local:DocumentTypes>
        <local:DocumentTypes>Third</local:DocumentTypes>
    </x:Array>

【讨论】:

  • 找不到本地命名空间
  • 使用xmlns:sys="clr-namespace:System;assembly=mscorlib"
【解决方案2】:

您可以向数据上下文添加可绑定属性,例如:

public IEnumerable DocumentTypesList
{
   get
   {
       return Enum.GetVaues(typeof(DocumentTypes));
   }
}

并通过以下方式绑定到它:

<MultiBinding.ConverterParameter>
                    <Binding Path="DocumentTypesList">
</MultiBinding.ConverterParameter>

这样,如果您的枚举被更改,您不必更改它的任何 XAML 表示。

或者如果转换器总是使用这个显式枚举,你可以直接在其中引用它。

【讨论】:

  • 不能绑定 ConverterParameter。
  • 我的错。上次我处理这些是在不久前。您可以做的是,因为您已经在使用 MultiValueConverter,要么将枚举枚举作为附加值传递,要么使用 并在转换器中枚举枚举。
【解决方案3】:

试试这个:

<MultiBinding.ConverterParameter>
  <x:Array Type="{x:Type sys:Enum}">
    <x:Static Member="sys:Enum:YourEnumType.YourEnumValue1" />
    <x:Static Member="sys:Enum:YourEnumType.YourEnumValue2" />
  </x:Array>
</MultiBinding.ConverterParameter>

在我的项目中,我添加了枚举类型命名空间,例如:

xmlns:mod="clr-namespace:MyProject.Modal;assembly=MyProject.Modal"

其中 MyProject.Modal 是我的枚举定义的命名空间。这样,如果您有一个枚举名称“MyEnum”,那么您可以这样做:

<MultiBinding.ConverterParameter>
  <x:Array Type="{x:Type mod:MyEnum}">
    <x:Static Member="mod:MyEnum.Value1" />
    <x:Static Member="mod:MyEnum.Value2" />
  </x:Array>
</MultiBinding.ConverterParameter>

参考:https://social.msdn.microsoft.com/Forums/vstudio/en-US/9db5f1f5-f3a9-402c-9869-08d3624eea59/xarray-markup-extension-as-itemssource-for-combobox?forum=wpf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2012-03-21
    • 2010-09-26
    • 2012-01-14
    • 1970-01-01
    • 2011-08-03
    相关资源
    最近更新 更多