【问题标题】:Find List of binded property in a Depedency Property in WPF C#在 WPF C# 的依赖属性中查找绑定属性列表
【发布时间】:2016-09-13 05:31:16
【问题描述】:

我有一个 WPF 自定义控件

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

ViewModel 属性

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

考虑属性的价值

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

自定义控件

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

考虑自定义控件中的一段代码

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

这里我需要从依赖属性SBItem获取绑定属性的值NameIDString Format。请帮助我。

【问题讨论】:

  • 我没完全理解你,你是不是想以某种方式炸毁SBItem...?
  • @AnnaSB,在运行时我们不知道它是什么类型的字符串...所以,我需要知道绑定信息以及字符串格式...
  • 老实说,这个问题毫无意义。您不能拥有通过 MultiBinding(产生字符串)绑定到视图模型对象的字符串类型的依赖属性,并希望能够访问视图模型类的单个属性。
  • @B.Balamanigandan 请您解释一下您的实际要求。你想用这个实现什么?
  • @B.Balamanigandan - 你的问题是有效的。

标签: c# wpf xaml custom-controls dependency-properties


【解决方案1】:

您无法在ApplyTemplate 方法中获取绑定值。在绑定之前调用。

因此,在您的 DP 定义中使用 new PropertyMetadata(null,new PropertyChangedCallback(OnPropertyChanged)) 为属性更改提供回调。

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

【讨论】:

  • BindingOperations.GetMultiBinding 是这里的关键。获取返回的 MultiBinding 并遍历其 Bindings 以检查其 Path
  • 他问的问题很笼统,但你的回答是针对给定字符串的。
【解决方案2】:

正如 AnjumSKhan 已经说过的,您必须为DependencyProperty 实现一个Changed 方法。接下来,您将找到一种更通用的方法来获取绑定值。

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");
        }
      }
    }

注意

当然你必须用你自己的逻辑替换我的Console.WriteLine。此示例将演示如何从绑定源中获取值。如果绑定是普通绑定而不是多重绑定,您可能可以忽略我作为后备实现的部分。

希望这能让你朝着正确的方向前进

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-08
    • 2010-12-26
    • 2014-08-31
    • 2023-03-26
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多