0

So, it's been a long day figuring out the best way to implement a property binding. By the end of it, I threw a hail mary and won!... but I don't know why. Being a fundamentalist, I'd like to know why this line worked:

FormatString="{Binding ElementName=comboRingBuffer, Path=Tag.Format}"

in the following code-behind:

// Argument is a struct with a Limit "property"
private Argument ringBuffer;

public Limit RingBufferPrefix
{
    get
    {
        return this.ringBuffer.Limit;
    }

    set
    {
        this.ringBuffer.Limit = value;
        this.OnPropertyChanged(nameof(this.RingBufferPrefix));
    }
}

public class Limit
{
    public Limit(string prefix, string format)
    {
        this.Prefix = prefix;
        this.Format = format;
    }

    public string Prefix { get; set; }
    public string Format { get; set; }
    public override string ToString()
    {
        return this.Prefix;
    }
}

and XAML:

<ComboBox x:Name="comboRingBuffer"
          SelectionChanged="SelectionChanged_Event"
          Tag="{Binding Path=RingBufferPrefix}" />
<xctk:IntegerUpDown x:Name="integerRingBuffer"
                    DockPanel.Dock="Right"
                    Style="{StaticResource DigitalInteger}"
                    ValueChanged="ValueChanged_Event"
                    FormatString="{Binding ElementName=comboRingBuffer, Path=Tag.Format}"
                    Value="{Binding Path=RingBuffer}" />

I thought for sure the it would just say that the Tag is an object and display nothing.

UndeadBob
  • 1,110
  • 1
  • 15
  • 34

2 Answers2

0

Its simple to understand if you know.

 "{Binding ElementName=comboRingBuffer, Path=Tag.Format}"

Above line means that you are binding to a property of a different control named comboRingBuffer and the name of the property to look is Tag.Format

You have binded Tag Property of comboRingBuffer to a property in ViewModel/DataContext so FormatString is automatically checking for the property which is blinded.

Abin
  • 2,868
  • 1
  • 23
  • 59
  • But all it knows is that the `ComboBox` has a `Tag` which should be an `object`. How did it know the `Limit` type that was being bound to the property? – UndeadBob May 24 '16 at 18:09
  • what do you meant by Limit ? i suggest you go thru [this](http://stackoverflow.com/a/7000922/2470362) and understand how the converter is used to match the type of dependency property. In your case you are passing the same type to binding otherwise it may throw error at run-time. – Abin May 24 '16 at 18:16
0

There is some sort of reflection happening inside the PropertyPath (Path property of Binding class)implementations.

The value you set from the XAML is treated as string, parsed and reflection.

You can use a tool to check the implementation of this class like ILSpy or others..

tgpdyk
  • 1,203
  • 9
  • 13
  • Well, that's true, the XAML value is treated like a string. Noticed that when I started using `this.OnPropertyChanged(nameof(this.RingBufferPrefix));` et. al. It just opens a whole can of worms if I can use structs with nested properties in the *View*. – UndeadBob May 26 '16 at 17:25