从ResourceDictionary获取DataTemplate时的空/空绑定 [英] Null/Empty binding when getting DataTemplate from ResourceDictionary

查看:205
本文介绍了从ResourceDictionary获取DataTemplate时的空/空绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好







我正试图从代码中的ResourceDictionary获取DatatTemplate 。问题是当我试图将它保存到字符串时我得到所有Binding位置为空或null。



这是我的代码



ResourceDictionary dictionary = new ResourceDictionary();

dictionary.Source = new Uri(WpfApplication1; component / Dict.xaml,UriKind.RelativeOrAbsolute );

DataTemplate模板=(DataTemplate)字典[helloTextBox];

字符串保存= XamlWriter.Save(template.LoadContent());







我很乐意为您提供任何见解。



谢谢

Hi all



I'm trying to get a DatatTemplate from a ResourceDictionary in code. The problem is when I'm trying to save it to string I get all the Binding location to be either empty or null.

Here is my piece of code

ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri("WpfApplication1;component/Dict.xaml", UriKind.RelativeOrAbsolute);
DataTemplate template = (DataTemplate) dictionary["helloTextBox"];
string save = XamlWriter.Save(template.LoadContent());



I'd be happy for any insight.

Thanks

推荐答案

事实证明我需要在TypeConverter中为BindingExpression注册一个绑定转换器,以便序列化处理绑定,因为默认情况下它们不是序列化的。



需要将此课程添加为ExpressionConverter





public班斌dingConverter:ExpressionConverter

{

public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context,Type destinationType)

{

返回true;

}



公共覆盖对象ConvertTo(System.ComponentModel.ITypeDescriptorContext context,System.Globalization.CultureInfo culture,object value,类型destinationType)

{

if(destinationType == typeof(MarkupExtension))

{

BindingExpression bindingExpression = value as BindingExpression;

if(bindingExpression == null)

{

抛出新的FormatException(预期绑定,但没有得到一个);

}

返回bindingExpression.ParentBinding;

}

返回base.ConvertTo(context ,文化,val ue,destinationType);

}

}





然后使用这个mehtod注册我在哪里做XamlWriter.Save部分





private void注册()

{

属性[] attr = new属性[1];

TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(BindingConverter));

attr [0 ] = vConv;

TypeDescriptor.AddAttributes(typeof(BindingExpression),attr);

}
It turns out I need to register a binding converter for BindingExpression in TypeConverter in order for the serialize to work on bindings as by default they are not serialized.

Needs to add this class as the ExpressionConverter


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


and then use this mehtod to register where I do the XamlWriter.Save part


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


这篇关于从ResourceDictionary获取DataTemplate时的空/空绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆