【问题标题】:Bind a string in xaml to a property将 xaml 中的字符串绑定到属性
【发布时间】:2013-01-29 05:24:39
【问题描述】:

我的视图模型上有一个公共常量字符串,我想绑定到

系统:字符串

在下面的xaml sn-p里面:

<ComboBox.ItemsSource>
    <CompositeCollection>
       <sys:String></sys:String>
       <CollectionContainer Collection="{Binding VMCollection, Source={StaticResource proxy}}" />
    </CompositeCollection>
 </ComboBox.ItemsSource>

如果 String 有内容属性或其他东西会很容易,但只是想找出最好的方法。

【问题讨论】:

    标签: wpf string xaml mvvm


    【解决方案1】:

    就个人而言,我认为无论如何您都应该在 ViewModel 中组合这些选项(可用)。 (这些不是应该“呈现”给用户的选项吗?即使你为此创建了一个全新的 UI,选项也会一样吗?)

    但是要回答你的问题..

    <Window ...
    ...
    xmlns:local="clr-namespace:MyNamespace">
    
    <ComboBox>
            <ComboBox.ItemsSource>
                <CompositeCollection>
                    <x:StaticExtension Member="local:Constants.MyConst" />
                    <core:String>1</core:String>
                    <core:String>2</core:String>
                    <core:String>3</core:String>
                </CompositeCollection>
            </ComboBox.ItemsSource>
        </ComboBox>
    
    public static class Constants
    {
        public static string MyConst
        {
            get
            {
                return "asd";
            }
        }
    }
    

    这行得通

    【讨论】:

    • 我基本上有两个组合框。一个带有字符串列表,另一个带有相同的字符串列表,但也具有选择空项目的能力。基本上,我不想管理两个不同的列表,它们的内容基本相同,但不能选择任何内容。
    • 无论如何,很棒的答案。正是我想要的,谢谢。
    • @jsirr13 不客气,感谢您的更新,很高兴它有帮助
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多