将集合或数组添加到 wpf 资源字典 [英] Add collection or array to wpf resource dictionary

查看:63
本文介绍了将集合或数组添加到 wpf 资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了高低,找不到答案.我有两个问题

I've search high and low and can't find an answer to this. I have two questions

  1. 如何在 XAML 中创建数组或集合.我有一个数组,我想放在那里并绑定到一个组合框.我的第一个想法是将 ItemsControl 放入资源字典中,但组合框的 ItemsSource 需要 IEnumerable,因此不起作用.

这是我在我的资源字典中尝试过的,但都不起作用

Here's what I've tried in my resource dictionary and neither works

<ItemsControl x:Key="stateList">
    <sys:String>AL</sys:String>
    <sys:String>CA</sys:String>
    <sys:String>CN</sys:String>
</ItemsControl>
<ItemsControl x:Key="stateList2">
    <ComboBoxItem>AL</ComboBoxItem>
    <ComboBoxItem>CA</ComboBoxItem>
    <ComboBoxItem>CN</ComboBoxItem>
</ItemsControl>

这是我如何绑定它

<ComboBox SelectedValue="{Binding Path=State}" ItemsSource="{Binding Source={StaticResource stateList2}}"  >

                    </ComboBox>

更新

我让第一部分以这种方式工作

I got this first part to work this way

 <col:ArrayList x:Key="stateList3">
    <sys:String>AL</sys:String>
    <sys:String>CA</sys:String>
    <sys:String>CN</sys:String>
</col:ArrayList>

但是,我不想使用数组列表,我想使用通用列表,所以如果有人知道如何请告诉我.

However, I'd rather not use an array list, I'd like to use a generic list so if anyone knows how please let me know.

编辑更新:我猜 XAML 对泛型的支持非常有限,所以也许数组列表是我现在能做的最好的,但如果有人有答案,我仍然希望在第二个问题上得到帮助

EDIT UPDATE: I guess XAML has very limited support for generics so maybe an array list is the best I can do for now, but I would still like help on the second question if anyone has an anser

第二.我曾尝试在我的 XAML 中引用合并的资源字典,但遇到了问题,因为在 Window.resources 下,我拥有的不仅仅是字典,所以它需要我添加 x:Key.添加密钥后,系统将无法再在我的资源字典中找到这些项目.我不得不将合并的字典移到 Grid.Resources 中.理想情况下,我想在 app.xaml 中引用合并的字典,但我有同样的问题

2nd. I've tried referencing a merged resource dictionary in my XAML and had problems because under Window.resources I had more than just the dictionary so it required me to add x:Key. Once I add the key, the system can no longer find the items in my resource dictionary. I had to move the merged dictionary to Grid.Resources instead. Ideally I'd like to reference the merged dictionary in the app.xaml but I have the same problem

这是一些示例代码.第一部分需要一个 x:key 来编译,因为我有转换器,它抱怨每个项目必须有一个键,如果有多个

Here's some sample code. This first part required an x:key to compile because I have converter and it complained that every item must have a key if there is more than one

<UserControl.Resources>
    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ResourcesD.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

我不得不把它改成这个

<UI:BaseStep.Resources>
    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />             
</UI:BaseStep.Resources>
<Grid>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ResourcesD.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
</Grid>

谢谢

推荐答案

据我了解您的问题,您想将一个 ComboBox(或一个 ListBox)与一组项目绑定,对吗?如果您想要来自某个外部数据源的项目,您可以使用方便的 DataContext 属性.搜索 MSDN 了解更多信息.但是,如果您确实想要手动收集,请这样做:

As far as I understand your problem you want to bind a ComboBox (or a ListBox) with an array of items, right? If you want items from some external data source, you can just make use of handy DataContext property. Search MSDN for more on this. However, if you do want a manual collection, do it this way:

创建自己的类:

public class StringCollection : ObservableCollection<string> { }

现在像这样使用它:

<Window.Resources>
    <local:StringCollection x:Key="stringCollection">
        <sys:String>Hello</sys:String>
        <sys:String>World</sys:String>
    </local:stringCollection>
</Window.Resources>

...

    <ListBox
        Margin="15"
        ItemsSource="{StaticResource stringCollection}" />

或者,如果你想要更通用的集合,创建一个这样的类:

Or, if you want more generic collection create a class like this:

public class ObjectCollection : ObservableCollection<object> { }

并像这样使用它:

    <local:ObjectCollection x:Key="objectCollection">
        <sys:String>Hello</sys:String>
        <TextBlock>World</TextBlock>
        <sys:Int32>12345</sys:Int32>
    </local:ObjectCollection>

    ...

    <ComboBox
        Margin="15"
        ItemsSource="{StaticResource objectCollection}" />



您可能还想使用一些内置类,例如实现 IEnumerableInt32Collection.您可以直接将它们用作资源.



You may also want to make use of some in-built classes like Int32Collection that implements IEnumerable. You can use them directly as a resource.

Resources 属性(any FrameworkElementWindowUserControlApplication) 的类型是 ResourceDictionary 不是资源词典的集合!所以你不能这样做:

The Resources property (of any FrameworkElement like Window, UserControl, or Application) is of type ResourceDictionary not collection of resource dictionaries! so you can't do like this:

<UserControl.Resources>

    <!-- The first RD -->
    <!--<ResourceDictionary>
        <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
    </ResourceDictionary>-->

    <!-- Second RD!!! -->
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ResourcesD.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

而是这样做:

<UserControl.Resources>

    <!-- 
        There should be only 1 main RD, 
        Merge other RDs, if any
     -->
    <ResourceDictionary>

        <!-- First Resource -->
        <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />

        <!-- Second Resource -->
        <ResourceDictionary.MergedDictionaries>
            <!-- Now, there can be multiple RDs -->
            <ResourceDictionary Source="/ResourcesA.xaml" />
            <ResourceDictionary Source="/ResourcesB.xaml" />
            <ResourceDictionary Source="/ResourcesC.xaml" />
            <ResourceDictionary Source="/ResourcesD.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</UserControl.Resources>

希望这会有所帮助.
问候,
米希尔·戈卡尼

Hope this helps.
Regards,
Mihir Gokani

这篇关于将集合或数组添加到 wpf 资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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