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;
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
add a comment |
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
1
You may bind the ItemsSource to aDictionary<string, string>
and useKey
andValue
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
add a comment |
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
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
wpf data-binding
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 aDictionary<string, string>
and useKey
andValue
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
add a comment |
1
You may bind the ItemsSource to aDictionary<string, string>
and useKey
andValue
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
add a comment |
1 Answer
1
active
oldest
votes
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>
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
add a comment |
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>
add a comment |
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>
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>
answered Mar 26 at 9:24
ClemensClemens
92.8k8 gold badges97 silver badges189 bronze badges
92.8k8 gold badges97 silver badges189 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
You may bind the ItemsSource to a
Dictionary<string, string>
and useKey
andValue
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