WPF绑定My.Settings集合,组合框项目 [英] WPF Binding My.Settings collection to Combobox items

查看:187
本文介绍了WPF绑定My.Settings集合,组合框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的WPF,仍然试图环绕在XAML约束力我的脑袋。

I'm VERY new to WPF, and still trying to wrap my head around binding in XAML.

我想填充一个字符串集合在my.settings的值的组合框。我能做到这一点在code是这样的:

I'd like to populate a combobox with the values of a string collection in my.settings. I can do it in code like this:

Me.ComboBox1.ItemsSource = My.Settings.MyCollectionOfStrings

Me.ComboBox1.ItemsSource = My.Settings.MyCollectionOfStrings

...和它的作品。

我怎样才能做到这在我的XAML?这可能吗?

How can I do this in my XAML? is it possible?

感谢

推荐答案

,你可以(也应该在大多数情况下)宣布在XAML绑定,因为这是最强大的功能于一体WPF。

Yes, you can (and should for the most part) declare bindings in XAML, since that's one of the most powerful features in WPF.

在你的情况,到ComboBox绑定到您的自定义设置,你可以使用下面的XAML之一:

In your case, to bind the ComboBox to one of your custom settings you would use the following XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:p="clr-namespace:WpfApplication1.Properties"
    Title="Window1">
    <StackPanel>
        <ComboBox
            ItemsSource="{Binding Source={x:Static p:Settings.Default}, Path=MyCollectionOfStrings}" />
    </StackPanel>
</Window>

注意以下几个方面:

Notice the following aspects:


  • 我们宣布与preFIX'P'指向哪里'设置'类生命,以指它在XAML的.NET命名空间XML命名空间

  • 我们使用标记扩展'{结合},以宣布在XAML绑定

  • 我们使用,以标记扩展'静',以表明我们要引用(VB中的共享)一类的静态成员在XAML

这篇关于WPF绑定My.Settings集合,组合框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆