Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
My application needs to cause the appearance of various UI elements to change in response to real-time updates to the value of a property of a corresponding custom object. Each UI element needs to be associated with its own custom object. Sounds like a classic WPF binding problem, right?

I know that I can bind to an object that is a member of a Dictionary using something like:
Content="{Binding [key1]}"
where the "{Binding [key1]..." syntax causes 'key1' to be used to select the desired Dictionary member. What I cannot seem to make happen is binding to a specific property of that desired object. The following does not work:

Content="{Binding [key1], Path=ValueProperty}"

It causes the following to be written to the Output window at runtime:

System.Windows.Data Error: 39 : BindingExpression path error: '
MIDL
OPCTagValue' property not found on 'object' ''Dictionary`2' (HashCode=30880833)'. BindingExpression:Path=ValueProperty; DataItem='Dictionary`2' (HashCode=30880833); target element is 'Button' (Name='button1'); target property is 'Content' (type 'Object')


BTW, One-Way binding is all that is required here and I will be using a ValueConverter to translate the object's property value to the corresponding UI element property value.

Can someone tell me what I am missing here?

TIA.
Tom
Posted

1 solution

If you are trying to bind to a particular dictionary element, then you can use a fairly simple trick. First of all, here's a bit of code-behind that we are going to use to bind to.
C#
using System.Collections.Generic;
using System.Windows;
namespace BindingSampleTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Dictionary<string, OPC> Tag { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            Tag = new Dictionary<string, OPC>();
            Tag.Add("Test", new OPC { OPCTag = "Hello" });
            Tag.Add("Test2", new OPC { OPCTag = "Hello there" });
            DataContext = Tag;
        }
        public class OPC
        {
            public string OPCTag { get; set; }
        }
    }
}
Now, let's add the XAML bindy-goodness and rejoice at the gooeyness that is WPF data binding.
XML
<Window x:Class="BindingSampleTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="OPC Window" Height="350" Width="525">
    <Grid>
        <TextBlock Text="{Binding [Test].OPCTag}" />
    </Grid>
</Window>
If you look at the Binding, you can see that the trick is just to use the dot property to access it. I hope this helps.
 
Share this answer
 
Comments
tpwright4423 8-May-11 17:49pm    
Pete, thank you for the (naturally) oh-so-simple solution. Perfect! Is my WPF newbie-ness showing?

Regards,
Tom
Pete O'Hanlon 9-May-11 0:33am    
Tim, your question was not WPF-newbie. It's not the most obvious syntax.
tpwright4423 9-May-11 8:28am    
...and just how did you know that my app was OPC-related?
Pete O'Hanlon 9-May-11 8:41am    
2 reasons.

1. I used to work in the Chemical Engineering software industry, developing C++ applications to run in plants.
2. I read your error message.;)
tpwright4423 9-May-11 9:48am    
Oops! Forgot to edit that one occurrence, didn't I?

Thanks for the help. As usual, the solution, once seen, seems so easy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900