使用x:Static在xaml中为数组添加值

时间:2016-06-02 11:54:10

标签: c# wpf xaml binding contentproperty

例如,我必须在xaml

中使用字符串数组
<x:Array Type="sys:String" x:Key="Statuses">
    <sys:String>Ok</sys:String>
    <sys:String>Warning</sys:String>
    <sys:String>Error</sys:String>
</x:Array>

但是如果我必须通过x:Static分配物品怎么办?可能吗 ?以下代码不起作用。

<x:Array Type="system:String" x:Key="Statuses">
    <sys:String>
        <Binding="{x:Static model:Status.Ok}" />
    </sys:String>
    <sys:String>
        <Binding="{x:Static model:Status.Warning}" />
    </sys:String>
</x:Array>

public enum Status
{
    Ok,
    Warning,
    Error
}

所以我的问题是如何使用xaml

x:Static内初始化数组

我知道这种方法

[ContentProperty("Values")]
[ContentWrapper(typeof(Collection<State>))]
public class StateCollection
{
    private const string DefaultKeySeparator = ",";
    private string _keySeparator;
    public string KeySeparator
    {
        get { return _keySeparator ?? DefaultKeySeparator; }
        set { _keySeparator = value; }
    }
    private Collection<State> _values = new Collection<AlternationConverterItem>();
    public Collection<State> Values
    {
        get { return _values; }
        set { _values = value; }
    }
}

public sealed class State
{
    public Status Status { get; set; }
    public String Description { get; set; }
}

<myNamespace:StateCollection x:Key="MyCollection" KeySeparator="-">
    <myNamespace:State Status="{x:Static model:RestoreObjectStatus.Ok}" Description="{StaticResource Description1}" />
    <myNamespace:State Status="{x:Static model:RestoreObjectStatus.Warning}" Description="{StaticResource Description2}" />
    <myNamespace:State Status="{x:Static model:RestoreObjectStatus.Error}" Description="{StaticResource Description3}" />
</myNamespace:AlternationConverter2>

但是我并不热衷于对我上面写的字符串数组采用相同的方法。

1 个答案:

答案 0 :(得分:3)

我已成功使用x:Static Member=

<x:Array x:Key="SiteOkKey" Type="system:Object">
    <x:Static Member="model:RestoreObjectStatus.Ok" />
    <x:Static Member="core:RestoreObjectTypes.Site" />
</x:Array>