在 XAML 中填充自定义数组属性
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 x:Array 在 XAML 中创建数组,但您仍然需要像莉兹的答案一样将其用作资源。
You can create arrays in XAML using x:Array, you still need to use it as a resource like Liz's answer.
除了您在这里所做的之外,还有其他原因将 ComboBox 子类化吗?
因为如果想法只是向组合框提供字符串列表,那么请查看 “将集合或数组添加到 wpf 资源字典”
尽可能让组合框自行处理 - 通过使用 ItemsSource 属性。因此,我们在链接示例中所做的就是提供一个包含字符串列表的“资源”,然后将此资源传递给 ItemsSource,如下所示:
定义这样的类型:
然后创建一个静态资源
我希望有所帮助。
编辑:您还可以更改 DependencyProperty 以使用 StringList 而不是 String[],那么您的依赖属性也将起作用。
然后是 dependencyProperty:
那么这将有效地执行与直接绑定到 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:
Define a type like this:
And then create a static resource
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.
And then the dependencyProperty:
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.
还有一个带有
enum
的额外示例:And, an extra example with
enum
: