WPF binding to a value from my DataContextHow do I use WPF bindings with RelativeSource?Binding a WPF ComboBox to a custom listWPF - Hovering over one Element shows content in another ElementWPF change Button Content on ViewModel.PropertyChanged eventChange GridView cell background with CellTemplateSelectorDefine a custom property to bind toHow does data binding work in AngularJS?dynamic datatemplate binding in WPF datagridcolumnWPF MVVM binding errorWPF Style DataTrigger with binding to DataContext not working

How do I determine whether a permit is required for a new gas line?

Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?

What would be the ideal melee weapon made of "Phase Metal"?

Shortest distance around a pyramid

What explains 9 speed cassettes price differences?

Who Can Help Retag This?

How might the United Kingdom become a republic?

What's the minimum number of sensors for a hobby GPS waypoint-following UAV?

Is it rude to tell recruiters I would only change jobs for a better salary?

Robbers: The Hidden OEIS Substring

Were there any new Pokémon introduced in the movie Pokémon: Detective Pikachu?

What is temperature on a quantum level

Is Trump personally blocking people on Twitter?

Does Google Maps take into account hills/inclines for route times?

Why does the autopilot disengage even when it does not receive pilot input?

Is it possible for thermophilic viruses to infect humans?

I have a ruthless DM and I'm considering leaving the party. What are my options to minimize the negative impact to the rest of the group?

Why do players in the past play much longer tournaments than today's top players?

How do I take a fraction to a negative power?

Correlation of independent random processes

Filtering fine silt/mud from water (not necessarily bacteria etc.)

<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?

Bronze Age Underwater Civilization

Where is the USB2 OTG port on the RPi 4 Model B located?



WPF binding to a value from my DataContext


How do I use WPF bindings with RelativeSource?Binding a WPF ComboBox to a custom listWPF - Hovering over one Element shows content in another ElementWPF change Button Content on ViewModel.PropertyChanged eventChange GridView cell background with CellTemplateSelectorDefine a custom property to bind toHow does data binding work in AngularJS?dynamic datatemplate binding in WPF datagridcolumnWPF MVVM binding errorWPF Style DataTrigger with binding to DataContext not working






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I have a plain "Location" object with a list of string properties (City, Country, Zip, etc.) I want to display, underneath headers of the name of the property. What I've got now works, and looks like this:



<TextBlock Text="City" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding City" />
<TextBlock Text="Country" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding Country" />


… and so on. The header text happens to be the same as the property path, and naturally I want to avoid duplicating writing all these pairs of TextBlocks and instead use an ItemsControl and DataTemplates.



I've defined a list of strings in my Window.Resources, and have an ItemsControl displaying at least the headers correctly, but I can't use a dynamic value for a binding path:



<Window.Resources>
<x:Array x:Key="LocationKeys" Type="sys:String">
<sys:String>City</sys:String>
<sys:String>Country</sys:String>
<sys:String>Zip</sys:String>
</x:Array>
</Window.Resources>


...



<ItemsControl ItemsSource="StaticResource LocationKeys">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Binding" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding Path=Obviously I can't do this" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl>


So close! I can't shake the feeling that I'm missing something here, and I'm having enough trouble finding the terms to search for to find someone with the same problem - all the hits I'm getting so far for 'Dynamic binding path' etc. are usually trying to solve another problem that's quite different.



Am I missing something easy? Is there a better way?










share|improve this question

















  • 1





    You may bind the ItemsSource to a Dictionary<string, string> and use Key and Value in the ItemTemplate.

    – Clemens
    Mar 26 at 6:26











  • I got so tied up trying to do things the WPF way with Shared Resources I couldn't see the wood for the trees - thanks! This looks like the easiest way.

    – p10ben
    Mar 26 at 8:21

















-1















I have a plain "Location" object with a list of string properties (City, Country, Zip, etc.) I want to display, underneath headers of the name of the property. What I've got now works, and looks like this:



<TextBlock Text="City" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding City" />
<TextBlock Text="Country" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding Country" />


… and so on. The header text happens to be the same as the property path, and naturally I want to avoid duplicating writing all these pairs of TextBlocks and instead use an ItemsControl and DataTemplates.



I've defined a list of strings in my Window.Resources, and have an ItemsControl displaying at least the headers correctly, but I can't use a dynamic value for a binding path:



<Window.Resources>
<x:Array x:Key="LocationKeys" Type="sys:String">
<sys:String>City</sys:String>
<sys:String>Country</sys:String>
<sys:String>Zip</sys:String>
</x:Array>
</Window.Resources>


...



<ItemsControl ItemsSource="StaticResource LocationKeys">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Binding" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding Path=Obviously I can't do this" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl>


So close! I can't shake the feeling that I'm missing something here, and I'm having enough trouble finding the terms to search for to find someone with the same problem - all the hits I'm getting so far for 'Dynamic binding path' etc. are usually trying to solve another problem that's quite different.



Am I missing something easy? Is there a better way?










share|improve this question

















  • 1





    You may bind the ItemsSource to a Dictionary<string, string> and use Key and Value in the ItemTemplate.

    – Clemens
    Mar 26 at 6:26











  • I got so tied up trying to do things the WPF way with Shared Resources I couldn't see the wood for the trees - thanks! This looks like the easiest way.

    – p10ben
    Mar 26 at 8:21













-1












-1








-1








I have a plain "Location" object with a list of string properties (City, Country, Zip, etc.) I want to display, underneath headers of the name of the property. What I've got now works, and looks like this:



<TextBlock Text="City" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding City" />
<TextBlock Text="Country" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding Country" />


… and so on. The header text happens to be the same as the property path, and naturally I want to avoid duplicating writing all these pairs of TextBlocks and instead use an ItemsControl and DataTemplates.



I've defined a list of strings in my Window.Resources, and have an ItemsControl displaying at least the headers correctly, but I can't use a dynamic value for a binding path:



<Window.Resources>
<x:Array x:Key="LocationKeys" Type="sys:String">
<sys:String>City</sys:String>
<sys:String>Country</sys:String>
<sys:String>Zip</sys:String>
</x:Array>
</Window.Resources>


...



<ItemsControl ItemsSource="StaticResource LocationKeys">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Binding" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding Path=Obviously I can't do this" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl>


So close! I can't shake the feeling that I'm missing something here, and I'm having enough trouble finding the terms to search for to find someone with the same problem - all the hits I'm getting so far for 'Dynamic binding path' etc. are usually trying to solve another problem that's quite different.



Am I missing something easy? Is there a better way?










share|improve this question














I have a plain "Location" object with a list of string properties (City, Country, Zip, etc.) I want to display, underneath headers of the name of the property. What I've got now works, and looks like this:



<TextBlock Text="City" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding City" />
<TextBlock Text="Country" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding Country" />


… and so on. The header text happens to be the same as the property path, and naturally I want to avoid duplicating writing all these pairs of TextBlocks and instead use an ItemsControl and DataTemplates.



I've defined a list of strings in my Window.Resources, and have an ItemsControl displaying at least the headers correctly, but I can't use a dynamic value for a binding path:



<Window.Resources>
<x:Array x:Key="LocationKeys" Type="sys:String">
<sys:String>City</sys:String>
<sys:String>Country</sys:String>
<sys:String>Zip</sys:String>
</x:Array>
</Window.Resources>


...



<ItemsControl ItemsSource="StaticResource LocationKeys">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Binding" Style="StaticResource HeaderStyle" />
<TextBlock Text="Binding Path=Obviously I can't do this" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl>


So close! I can't shake the feeling that I'm missing something here, and I'm having enough trouble finding the terms to search for to find someone with the same problem - all the hits I'm getting so far for 'Dynamic binding path' etc. are usually trying to solve another problem that's quite different.



Am I missing something easy? Is there a better way?







wpf data-binding






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 4:30









p10benp10ben

3041 gold badge5 silver badges16 bronze badges




3041 gold badge5 silver badges16 bronze badges







  • 1





    You may bind the ItemsSource to a Dictionary<string, string> and use Key and Value in the ItemTemplate.

    – Clemens
    Mar 26 at 6:26











  • I got so tied up trying to do things the WPF way with Shared Resources I couldn't see the wood for the trees - thanks! This looks like the easiest way.

    – p10ben
    Mar 26 at 8:21












  • 1





    You may bind the ItemsSource to a Dictionary<string, string> and use Key and Value in the ItemTemplate.

    – Clemens
    Mar 26 at 6:26











  • I got so tied up trying to do things the WPF way with Shared Resources I couldn't see the wood for the trees - thanks! This looks like the easiest way.

    – p10ben
    Mar 26 at 8:21







1




1





You may bind the ItemsSource to a Dictionary<string, string> and use Key and Value in the ItemTemplate.

– Clemens
Mar 26 at 6:26





You may bind the ItemsSource to a Dictionary<string, string> and use Key and Value in the ItemTemplate.

– Clemens
Mar 26 at 6:26













I got so tied up trying to do things the WPF way with Shared Resources I couldn't see the wood for the trees - thanks! This looks like the easiest way.

– p10ben
Mar 26 at 8:21





I got so tied up trying to do things the WPF way with Shared Resources I couldn't see the wood for the trees - thanks! This looks like the easiest way.

– p10ben
Mar 26 at 8:21












1 Answer
1






active

oldest

votes


















0














Your view model may contain a dictionary like



public class ViewModel

public Dictionary<string, object> Locations get;
= new Dictionary<string, object>();



to which you would bind like this:



<ItemsControl ItemsSource="Binding Locations">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Binding Key" Width="100"/>
<ContentControl Content="Binding Value"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349881%2fwpf-binding-to-a-value-from-my-datacontext%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Your view model may contain a dictionary like



    public class ViewModel

    public Dictionary<string, object> Locations get;
    = new Dictionary<string, object>();



    to which you would bind like this:



    <ItemsControl ItemsSource="Binding Locations">
    <ItemsControl.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="Binding Key" Width="100"/>
    <ContentControl Content="Binding Value"/>
    </StackPanel>
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>





    share|improve this answer



























      0














      Your view model may contain a dictionary like



      public class ViewModel

      public Dictionary<string, object> Locations get;
      = new Dictionary<string, object>();



      to which you would bind like this:



      <ItemsControl ItemsSource="Binding Locations">
      <ItemsControl.ItemTemplate>
      <DataTemplate>
      <StackPanel Orientation="Horizontal">
      <TextBlock Text="Binding Key" Width="100"/>
      <ContentControl Content="Binding Value"/>
      </StackPanel>
      </DataTemplate>
      </ItemsControl.ItemTemplate>
      </ItemsControl>





      share|improve this answer

























        0












        0








        0







        Your view model may contain a dictionary like



        public class ViewModel

        public Dictionary<string, object> Locations get;
        = new Dictionary<string, object>();



        to which you would bind like this:



        <ItemsControl ItemsSource="Binding Locations">
        <ItemsControl.ItemTemplate>
        <DataTemplate>
        <StackPanel Orientation="Horizontal">
        <TextBlock Text="Binding Key" Width="100"/>
        <ContentControl Content="Binding Value"/>
        </StackPanel>
        </DataTemplate>
        </ItemsControl.ItemTemplate>
        </ItemsControl>





        share|improve this answer













        Your view model may contain a dictionary like



        public class ViewModel

        public Dictionary<string, object> Locations get;
        = new Dictionary<string, object>();



        to which you would bind like this:



        <ItemsControl ItemsSource="Binding Locations">
        <ItemsControl.ItemTemplate>
        <DataTemplate>
        <StackPanel Orientation="Horizontal">
        <TextBlock Text="Binding Key" Width="100"/>
        <ContentControl Content="Binding Value"/>
        </StackPanel>
        </DataTemplate>
        </ItemsControl.ItemTemplate>
        </ItemsControl>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 9:24









        ClemensClemens

        92.8k8 gold badges97 silver badges189 bronze badges




        92.8k8 gold badges97 silver badges189 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349881%2fwpf-binding-to-a-value-from-my-datacontext%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현