0

I'm having a WPF Custom Control

<local:SuperControl>
    <local:SuperControl.SBItem>
        <MultiBinding StringFormat="{}Name: {0} ({1})">
            <Binding Path="Name" />
            <Binding Path="ID" />
        </MultiBinding>
    </local:SuperControl.SBItem>
</local:SuperControl>

The ViewModel Property

public string Name { get; set; }
public string ID { get; set; }

Consider the Value for the Property

Name = "John";
ID = "STK001";

The Custom Control

public class SuperControl : ItemsControl
{
    public static readonly DependencyProperty SBItemProperty = DependencyProperty.Register("SBItem", typeof(string), typeof(BAutoComplete), new PropertyMetadata(null));

    public string SBItem
    {
        get { return (string)GetValue(SBItemProperty); }
        set { SetValue(SBItemProperty, value); }
    }

    public override void OnApplyTemplate()
    {
        string Name = SBItem;
        string ID = SBItem;
        string StringFormat = SBItem;
    }
}

Consider the Piece of Code in the Custom Control

public override void OnApplyTemplate()
{
    string Name = SBItem;
    string ID = SBItem;
    string StringFormat = SBItem;
}

Here I need to get the Value of the Binded Property Name, ID and String Format from the Dependency Property SBItem. Kindly assist me.

11
  • I didn't completely understand you, are you trying to explode SBItem somehow...? Commented Sep 13, 2016 at 5:39
  • @AnnaSB, in runtime we don't know what type of string it is... So, I need to know the Binded information along with String Format... Commented Sep 13, 2016 at 5:40
  • 2
    Honestly, the question makes no sense. You can't have a dependency property of type string that is bound to a view model object via a MultiBinding (that produces a string) and hope to be able to access the single properties of the view model class. Commented Sep 13, 2016 at 5:52
  • @B.Balamanigandan Will you pls explain whats your actual requirement. What you are trying to achieve with this? Commented Sep 13, 2016 at 5:54
  • 1
    @B.Balamanigandan - Your question is valid. Commented Sep 13, 2016 at 5:58

2 Answers 2

1

You cannot get Binded values in ApplyTemplate method. As it is called before binding.

So, provide a callback for property change using new PropertyMetadata(null,new PropertyChangedCallback(OnPropertyChanged)) in your DP definition.

private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            string value = (string)e.NewValue;
            string Name = value.Split(new char[] { ':' })[1].Split(new char[] { '(' })[0].Trim();
            string ID = value.Split(new char[] { ':' })[1].Split(new char[] { '(' })[1].Split(new char[] { ')' })[0].Trim();
            string formatting = BindingOperations.GetMultiBinding(d, MyButton.MyPropertyProperty).StringFormat;
        }
Sign up to request clarification or add additional context in comments.

2 Comments

BindingOperations.GetMultiBinding is the key here. Take the returned MultiBinding and iterate over its Bindings to check their Path.
He asked the question in general, but your answer is specific for the given string.
0

As AnjumSKhan already stated, you have to implement a Changed-Method for the DependencyProperty. Following you'll find a more generic approach on how you can get the bound Values.

public static readonly DependencyProperty SBItemProperty = DependencyProperty.Register("SBItem", typeof(string), typeof(BAutoComplete), new PropertyMetadata(Changed));

    private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) {
      MultiBindingExpression mbe = null;
      try {
        mbe = BindingOperations.GetMultiBindingExpression(d, e.Property);
      } catch { }
      if (mbe != null) {
        foreach (var beb in mbe.BindingExpressions) {
          var bindingExpressionBase = (BindingExpression)beb;
          var di = bindingExpressionBase.DataItem;
          var p = di.GetType().GetProperty(bindingExpressionBase.ResolvedSourcePropertyName);
          var val = p.GetValue(di);
          Console.WriteLine($"Property: {p.Name} Value: {val}");
        }
      } else {
        try {
          var binding = BindingOperations.GetBindingExpression(d, e.Property);
          var di = binding.DataItem;
          var p = di.GetType().GetProperty(binding.ResolvedSourcePropertyName);
          var val = p.GetValue(di);
          Console.WriteLine($"Property: {e.Property.Name} Value: {val}");
        } catch {
          Console.WriteLine("No binding found");
        }
      }
    }

Note

Of course you have to replace my Console.WriteLine with your own logic. This example shall demonstrate, how you can get the values from the bound source. Probably you can ignore the part i've implemented as fallback if the Binding is a normal one and not a Multibinding.

Hope this gets you in the right direction

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.