在 XAML 中填充自定义数组属性

发布于 2024-11-07 02:16:01 字数 1310 浏览 0 评论 0原文

我试图在 XAML 中填充自定义字符串数组,但收到错误。我对 ComboBox 进行了子类化,并添加了一个要用自定义值填充的字符串 []:

public class MyComboBox : ComboBox
{
    public string[] MyProperty { get { return (string[])GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } }
    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string[]), typeof(MyComboBox));
}

我的 XAML 如下:

<Window x:Class="Samples.CustomArrayProperty"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Samples"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="CustomArrayProperty" Height="300" Width="300">
    <Grid>
        <local:MyComboBox Height="20">
            <local:MyComboBox.MyProperty>
                <sys:String>Monday</sys:String>
                <sys:String>Wednesday</sys:String>
                <sys:String>Friday</sys:String>
            </local:MyComboBox.MyProperty>
        </local:MyComboBox>
    </Grid>
</Window>

当我运行此命令时,我收到错误:“'Monday' 不是属性 'MyProperty' 的有效值。 ”。

我做错了什么?

I am trying to populate a custom string array in XAML, but am receiving an error. I subclassed a ComboBox and added a string [] that I want to fill with custom values:

public class MyComboBox : ComboBox
{
    public string[] MyProperty { get { return (string[])GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } }
    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string[]), typeof(MyComboBox));
}

My XAML is as follows:

<Window x:Class="Samples.CustomArrayProperty"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Samples"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="CustomArrayProperty" Height="300" Width="300">
    <Grid>
        <local:MyComboBox Height="20">
            <local:MyComboBox.MyProperty>
                <sys:String>Monday</sys:String>
                <sys:String>Wednesday</sys:String>
                <sys:String>Friday</sys:String>
            </local:MyComboBox.MyProperty>
        </local:MyComboBox>
    </Grid>
</Window>

When I run this, I get the error: "'Monday' is not a valid value for property 'MyProperty'.".

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-11-14 02:16:01

您可以使用 x:Array 在 XAML 中创建数组,但您仍然需要像莉兹的答案一样将其用作资源。

<Window.Resources>
    <x:Array Type="sys:String" x:Key="days">
        <sys:String>Monday</sys:String>
        <sys:String>Wednesday</sys:String>
        <sys:String>Friday</sys:String>
    </x:Array>
</Window.Resources>

<local:MyComboBox Height="23" MyProperty="{StaticResource days}" />

You can create arrays in XAML using x:Array, you still need to use it as a resource like Liz's answer.

<Window.Resources>
    <x:Array Type="sys:String" x:Key="days">
        <sys:String>Monday</sys:String>
        <sys:String>Wednesday</sys:String>
        <sys:String>Friday</sys:String>
    </x:Array>
</Window.Resources>

<local:MyComboBox Height="23" MyProperty="{StaticResource days}" />
谁的年少不轻狂 2024-11-14 02:16:01

除了您在这里所做的之外,还有其他原因将 ComboBox 子类化吗?

因为如果想法只是向组合框提供字符串列表,那么请查看 “将集合或数组添加到 wpf 资源字典”

尽可能让组合框自行处理 - 通过使用 ItemsSource 属性。因此,我们在链接示例中所做的就是提供一个包含字符串列表的“资源”,然后将此资源传递给 ItemsSource,如下所示:

<ComboBox ItemsSource="{StaticResource stringList}" /> 

定义这样的类型:

public class StringList : List<string> { }

然后创建一个静态资源

<Window.Resources>
    <local:StringList x:Key="stringList">
        <sys:String>Monday</sys:String>
        <sys:String>Wednesday</sys:String>
        <sys:String>Friday</sys:String>
    </local:StringList >
</Window.Resources>

我希望有所帮助。

编辑:您还可以更改 DependencyProperty 以使用 StringList 而不是 String[],那么您的依赖属性也将起作用。

<local:MyComboBox MyProperty="{StaticResource stringList}" Height="23" />

然后是 dependencyProperty:

   public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty",typeof(StringList),typeof(MyComboBox),new FrameworkPropertyMetadata(null, listChangedCallBack));

   static void listChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
       ComboBox combo = (ComboBox)property;
       combo.ItemsSource= (IEnumerable)args.NewValue;
    }

那么这将有效地执行与直接绑定到 ItemsSource 相同的操作。但如果我理解正确的话,最重要的是让 Dependency 属性发挥作用。

Is there any other reason you are subclassing the ComboBox beyond what you are doing here?

Because if the idea is to just provide a list of strings to the combobox then take a look at "Add collection or array to wpf resource dictionary"

As much as possible it would be better to let the combobox take care of itself - by using the ItemsSource property. So what we are doing in the linked example is providing a 'resource' containing your list of strings, and then passing this resource to the ItemsSource as follows:

<ComboBox ItemsSource="{StaticResource stringList}" /> 

Define a type like this:

public class StringList : List<string> { }

And then create a static resource

<Window.Resources>
    <local:StringList x:Key="stringList">
        <sys:String>Monday</sys:String>
        <sys:String>Wednesday</sys:String>
        <sys:String>Friday</sys:String>
    </local:StringList >
</Window.Resources>

I hope that helps.

Edit: You could also change your DependencyProperty to use StringList instead of String[], then your dependency property will work as well.

<local:MyComboBox MyProperty="{StaticResource stringList}" Height="23" />

And then the dependencyProperty:

   public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty",typeof(StringList),typeof(MyComboBox),new FrameworkPropertyMetadata(null, listChangedCallBack));

   static void listChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
       ComboBox combo = (ComboBox)property;
       combo.ItemsSource= (IEnumerable)args.NewValue;
    }

Then this would effectively do the same thing as binding directly to the ItemsSource. But the main thing if I understand you correctly, is just to get the Dependency property to work.

终陌 2024-11-14 02:16:01

还有一个带有 enum 的额外示例:

namespace MyNamespace
{
    public enum OrganizationType
    {
        Office365,
        OnPremisses
    }
}

xmlns:local="clr-namespace:MyNamespace"

<x:Array Type="local:OrganizationType" x:Key="OrganizationTypeArray">
    <local:OrganizationType>Office365</local:OrganizationType>
    <local:OrganizationType>OnPremisses</local:OrganizationType>
</x:Array>

And, an extra example with enum:

namespace MyNamespace
{
    public enum OrganizationType
    {
        Office365,
        OnPremisses
    }
}

xmlns:local="clr-namespace:MyNamespace"

<x:Array Type="local:OrganizationType" x:Key="OrganizationTypeArray">
    <local:OrganizationType>Office365</local:OrganizationType>
    <local:OrganizationType>OnPremisses</local:OrganizationType>
</x:Array>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文