开发者

Is it possible to make an List<string> a static resource in xaml?

If I want to bind something like a combobox in the code-behind I have no problem at all. Something like :

List<string> strings = new List<string>();
AddStringsFromDataSourceToList(strings);

comboBox1.ItemSource = strings;

As far as I can tell, there is no quick and dirty way to do this in XAML. For all of the praise wpf is receiving for its super simple databinding, something this simple seems far easier to just do in C#. Is there an easier way to do this than creating DependencyProperty wrappers and adding them as resources without much help from intellisense or all that goes into ObservableCollections? I understand that its not impossible, but I must be missing something if such a simple task seems so in depth...

EDIT: To clarify, adding dynamic Lists is the issue here, not st开发者_C百科atic arrays. It is super easy to add items manually, as many have pointed out.


<Window.Resources>
    <x:Array x:Key="strings" Type="sys:String" 
            xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <sys:String>One</sys:String>
            <sys:String>Two</sys:String>
    </x:Array>
    <!-- likewise -->
    <x:Array x:Key="persons" Type="{x:Type local:Person}" 
            xmlns:local="clr-namespace:namespace-where-person-is-defined">
            <local:Person FirstName="Sarfaraz" LastName="Nawaz"/>
            <local:Person FirstName="Prof" LastName="Plum"/>
    </x:Array>
<Window.Resources>


<ComboBox ItemsSource="{StaticResource strings}" />

<ListBox ItemsSource="{StaticResource persons}">
     <ListBox.ItemTemplate>
           <DataTemplate>
               <TextBlock>
                     <Run Text="{Binding FirstName}"/>
                     <Run Text="  "/>
                     <Run Text="{Binding LastName}"/>
               </TextBlock>
           </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>


Are you looking for something like this:

<ComboBox>
    <ComboBox.ItemsSource>
        <x:Array Type="sys:String" 
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <sys:String>One</sys:String>
            <sys:String>Two</sys:String>
        </x:Array>
    </ComboBox.ItemsSource>                
</ComboBox>

If you only want to define some items for a list, look at the solution from Henk Holterman. By the way, you can declare the array also as a resource and for other types.

Update

It seems that you have changed your question. However I don't understand now what your desire is. If you want to bind a collection you have in your code-behind, then make a public property that returns this collection, set the DataContext to the object that exposes this property and define a Binding in XAML:

<ComboBox ItemsSource="{Binding NameOfYourCollectionProperty}"...

Hope I understood your question right...


Sure there is:

<ComboBox>
    <ComboBoxItem>One</ComboBoxItem>
    <ComboBoxItem>Two</ComboBoxItem>
</ComboBox>

There are other syntaxes depending on your goal that are nearly as simple - using resources or inline itemssources, or grouped data.. even xml data. Don't throw up your hands in frustration because the first thing you tried wasn't easy - wpf is worth the learning curve, in my opinion.

WPF gets praise because it makes separating the visuals from the behavior much easier than windows forms, and because it makes creating nice visual effects much much easier, not because it makes it easier to do trivial examples. However, in this case - it is easier to do the trivial example.

With your edit Where do you want to pull them from? You don't have to create dependency properties or observable collections by any means. A simple list property will do (I prefer to use a collectionviewsource in the xaml in that case). Also, don't forget that you don't need to use all XAML if you hate it. But if you design for WPF instead of in spite of it you'll find a lot of tasks (like this one) easy.


You can do:

    <ComboBox>
        <ComboBoxItem>one</ComboBoxItem>
        <ComboBoxItem>two</ComboBoxItem>
    </ComboBox>

And there also exists syntax for declaring (and implicitly creating) data in for example a Resource section.

Maybe you can point out a more full scenario, with the requirements and constraints ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜