【问题标题】:Setting template binding in code behind在后面的代码中设置模板绑定
【发布时间】:2011-11-24 15:13:41
【问题描述】:

我们期待的输出是这样的,

<Canvas Width="800" Height="600">
   <Ellipse Stroke="#FF000000" StrokeThickness="2" Width="284" Height="288" 
            ToolTip="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Min}" 
            Canvas.Left="312" Canvas.Top="122" />
</Canvas>

使用此代码,

//This will ultimately hold object of type UIElement, which is Ellipse in this case.
private DependencyObject selectedObject; 

public void AddBinding(DependencyProperty dependencyProperty, DependencyProperty ipartProperty)
{
    Binding binding = new Binding(ipartProperty.Name); //Here Name is Min, an attached property
    binding.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
    BindingOperations.SetBinding(selectedObject, dependencyProperty, binding);
}

但实际输出是

<Canvas Width="800" Height="600">
   <Ellipse Stroke="#FF000000" StrokeThickness="2" Width="284" Height="288" 
            ToolTip="{x:Null}" Canvas.Left="312" Canvas.Top="122"/>
</Canvas>

我不知道怎么回事,请有人帮忙

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:

    找到了答案。使用下面的类

    public class BindingConverter : ExpressionConverter
    {
        public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, Type destinationType)
        {
            return true;
        }
    
        public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(MarkupExtension))
            {
                BindingExpression bindingExpression = value as BindingExpression;
                if (bindingExpression == null)
                {
                    throw new FormatException("Expected binding, but didn't get one");
                }
                return bindingExpression.ParentBinding;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
    

    将此方法添加到您调用 XamlWriter.Save(obj) 的类中

    private void Register()
        {
            Attribute[] attr = new Attribute[1];
            TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(BindingConverter));
            attr[0] = vConv;
            TypeDescriptor.AddAttributes(typeof(BindingExpression), attr);
        }
    

    得到了我想要的答案!!! 归功于 Alex Dov http://www.codeproject.com/script/Membership/View.aspx?mid=106815 。非常感谢这个人

    【讨论】:

      【解决方案2】:

      什么的输出?

      如果您使用某种 XamlWriter,您应该注意它的限制,它们不会保留绑定,如果您的意思是这也是有意义的属性值,因为您无法绑定到这样的附加属性,您需要这个路径:(OwndingClass.AttachedProperty) (注意括号和拥有类前缀)

      【讨论】:

      • 路径将是 PropertyList.Min 这是我的错误。 1) 我正在使用 Object obj = XamlReader.Load(指向预期输出的流)。 2)玩一点obj。 3) string output = XamlWriter.Save(obj) 在这个输出字符串中而不是绑定 x:Null 来了。
      • @vikram.ma:你可以忘记这个,XamlWriter does not write bindings
      • @vikram.ma:我不知道这样的方法,你为什么需要这样做?通常,如果您不能做某事,那并不是您一开始就应该做的事情。
      • @H.B 还有其他方法可以将对象转换为 xaml 文件吗?我的意思是序列化它?
      猜你喜欢
      • 2016-05-16
      • 2016-07-25
      • 2013-02-23
      • 1970-01-01
      • 2013-10-29
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      相关资源
      最近更新 更多