tags:

views:

41

answers:

1

Hi all,

I have a problem with bindings in wpf. I've discovered a strange problem. I have class ConfiugrationStringBuilder which inherits from DbConnectionStringBuilder. When I do smth like this

public class ConfiugrationStringBuilder : DbConnectionStringBuilder
{
  public string Directory { get;set; }
}

ConfiugrationStringBuilder builder = new ConfiugrationStringBuilder(connStr);

builder.Directory = "d:\dir"; //my property

txtBox.DataContext = builder;

then add binding to xaml for txtBox

<TextBox Name="txtBox" Text="{Binding Path=Directory}"></TextBox>

The problem is that above code doesn't work. I can't bind to my class property, but i can bind to public properies of base DbConnectionStringBuilder.

And the most funniest thing is that when i remove inheritance from DbConnectionStringBuilder

so my code looks like this

public class ConfiugrationStringBuilder
{
  public string Directory { get;set; }
}

In this case the above binding works well. I can't find any info about such cases and why in this case binding doesn't work? can somebody explain such behavior?

While I was writing the qusetion I tried to investigate what is a problem. And discovered that DbConnectionStringBuilder has indexer, so when try to bind to Path=Directory on backstage it does builder["Directory"] not builder.Directory. Anyway, can somebody tell me why it is doing so?

+3  A: 

It's a part of the Binding-'Magic' to support Master-Detail scenarios (see here for a complete description). Basically, as your object implements IEnumerable, a TextBox will try to bind to as it would a collection - meaning it will be looking for the Current item.

To work around this issue - I'd probably look to wrapping the DBConnectionStringBuilder instead of inheriting from it (also, you'll better adhere to composition over inheritance).

Goblin