将集合或数组添加到 wpf 资源字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2478738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:35:35  来源:igfitidea点击:

Add collection or array to wpf resource dictionary

wpfresourcedictionary

提问by Chris Cap

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

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

  1. How do you create an array or collection in XAML. I've got an array I want to stick in there and bind to a combo box. My first idea was to put an ItemsControl in a resource dictionary, but the ItemsSource of a combo box expects IEnumerable so that didn't work.
  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>

and here's how I bind to it

这就是我如何绑定它

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

                    </ComboBox>

EDIT: UPDATED

编辑:更新

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.

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

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 对泛型的支持非常有限,所以也许数组列表是我现在能做的最好的,但如果有人有 anser,我仍然希望帮助解决第二个问题

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

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

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

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

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

I had to change it to this

我不得不把它改成这个

<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>

Thank you

谢谢

回答by mg007

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:

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

Create your own class:

创建自己的类:

public class StringCollection : ObservableCollection<string> { }

and now use it like this:

现在像这样使用它:

<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> { }

and use it like this:

并像这样使用它:

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



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



您可能还想使用一些内置类,比如Int32Collectionimplements IEnumerable。您可以直接将它们用作资源。



The Resourcesproperty (of anyFrameworkElementlike Window, UserControl, or Application) is of type ResourceDictionarynot collection of resource dictionaries! so you can'tdo like this:

Resources财产(的任何FrameworkElement一样WindowUserControlApplication)是类型的ResourceDictionary不是资源字典集合!所以你不能这样做:

<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>

Instead do like this:

而是这样做:

<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

希望这可以帮助。
问候,
米希尔·戈卡尼

回答by Brian R. Bondy

Please correct me if I'm wrong but I think you want something like this?

如果我错了,请纠正我,但我认为你想要这样的东西?

<Grid.Resources>
  <local:MyCustomCollection x:Key="local:MyCustomCollection"/>
</Brid.Resources>

At the top of your window you'd have a property:

在您的窗口顶部,您将拥有一个属性:

xmlns:local="clr-namespace:MyNamespace"

And inside your code you'd have:

在您的代码中,您将拥有:

MyCustomCollection  custCol = MyGrid.Resources["MyCustomCollection"] as MyCustomCollection;

Binding would happen on a control with a property something like this:

绑定将发生在具有如下属性的控件上:

ItemsSource="{StaticResource MyCustomCollection}"

回答by Abe Heidebrecht

For the resources, you just need to mover your additional converter into the newly declared ResourceDictionary:

对于资源,您只需将额外的转换器移动到新声明的 ResourceDictionary 中:

<App.Resources> 
    <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/ResourcesD.xaml" /> 
        </ResourceDictionary.MergedDictionaries> 

    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" /> 
    </ResourceDictionary> 
</App.Resources> 

回答by Akash Kava

If you add resource dictionary inside Window's resources, you will not be able to access it everywhere, instead you should add in "App"'s resources (check out App.xaml file).

如果你在 Window 的资源中添加资源字典,你将无法在任何地方访问它,而是应该添加“App”的资源(查看 App.xaml 文件)。

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

This will be available in every WPF object regardless of creating any inheritance hierarchy.

无论创建任何继承层次结构,这都将在每个 WPF 对象中可用。