gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:53:26 24 4
gpt4 key购买 nike

我正在尝试在 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' 的有效值。”。

我究竟做错了什么?

最佳答案

您可以使用 x:Array 在 XAML 中创建数组,您仍然需要将其用作Liz的答案之类的资源。

<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}" />

关于wpf - 在 XAML 中填充自定义数组属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5967078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com