Find List of binded property in a Depedency Property in WPF C#

浪子不回头ぞ 提交于 2019-12-12 02:16:31

问题


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.


回答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;
        }



回答2:


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



来源:https://stackoverflow.com/questions/39462765/find-list-of-binded-property-in-a-depedency-property-in-wpf-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!