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

33,128

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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 ?

Share:
33,128

Related videos on Youtube

Morgan Herlocker
Author by

Morgan Herlocker

Creator of turf, a Javascript library for performing geospatial operations on geojson data. developer at Mapbox

Updated on July 09, 2022

Comments

  • Morgan Herlocker
    Morgan Herlocker almost 2 years

    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 static arrays. It is super easy to add items manually, as many have pointed out.

    • Grant Thomas
      Grant Thomas over 13 years
      WPF gets its praise for the fact it nicely allows to segment the logic away from design and so on - not because it can do everything and anything.
    • Morgan Herlocker
      Morgan Herlocker over 13 years
      @Disappointment- This is not anything too crazy...certainly not "anything and everything". Its simply and issue of conveinence of one of the most common tasks in building apps.
    • Grant Thomas
      Grant Thomas over 13 years
      I know it's not crazy; but once you start to get complacent and work along the lines of 'I'll just embed this piece of data in the UI...' who knows where it'll end? And if we're talking about "data" which can essentially be deemed part of the UI, like a list of drop-down options which are common and constant, then it is generally the utilising container which is abstracted and reused. I'm not getting at you, the question, or the method, just stating some things, BTW.
  • Morgan Herlocker
    Morgan Herlocker over 13 years
    edited to show what I was really getting at: when the List items are unknown.
  • Morgan Herlocker
    Morgan Herlocker over 13 years
    Sorry, I was not clear, but I'm looking for a way to add dynamic content (for example from a List<string> where the values are not known).
  • Philip Rieck
    Philip Rieck over 13 years
    @Prof Plum I suppose I don't understand - you want the xaml to hold a static resource with no code behind, but you want the values to be dynamic? Where/when will the values be set?
  • Morgan Herlocker
    Morgan Herlocker over 13 years
    Thanks, what does the collectionviesource method involve? If you know of any links that demonstrate a concise usage besides MSDN that would be quite helpful.
  • Henk Holterman
    Henk Holterman over 13 years
    Now you seem to want to mix imperative C# into the declarative XAML. If you just turn the function into a property you're done.
  • cod3monk3y
    cod3monk3y over 10 years
    I'd prefer to have the xmlns defined at the window level e.g. <Window ... xmlns:sys="clr-namespace..." xmlns:local="clr-namespace:..."> instead of on the array resources. This is very clean solid standard XAML.
  • edhedges
    edhedges almost 9 years
    Just a heads up, in your xaml you've got <TexBlock>, but it should be <TextBlock>. I can't make the edit since it's less than 6 chars.
  • Nawaz
    Nawaz almost 9 years
    @edhedges: Fixed. Thanks.