XamlWriter.Save从ListBox中丢失ItemsSource绑定

时间:2012-01-25 15:14:27

标签: c# .net wpf

我有自定义ContentControl

public class DataControl : ContentControl
{
    public List<DataItem> Options
    {
        get { return (List<DataItem>)GetValue(OptionsProperty); }
        set { SetValue(OptionsProperty, value); }
    }

    public static readonly DependencyProperty OptionsProperty =
        DependencyProperty.Register("Options", typeof(List<DataItem>), typeof(DataControl));

    public DataControl()
    {
        Options = new List<DataItem>();
    }

    public string Label
    {
        get { return (string)GetValue(LabelProperty); }
        set { SetValue(LabelProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Label.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LabelProperty =
        DependencyProperty.Register("Label", typeof(string), typeof(DataControl));
}

public class DataItem
{
    public DataItem(string key, string value)
    {
        Key = key;
        Value = value;
    }

    public string Key { get; set; }

    public string Value { get; set; }
}

其模板由以下样式应用:

<Style TargetType="{x:Type local:DataControl}" x:Key="DefaultStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:DataControl}">
                <StackPanel>
                <ListBox ItemsSource="{TemplateBinding Options}" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                        <Label Content="{Binding Key}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <Label Content="{TemplateBinding Label}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果我使用XamlWriter保存此样式然后再次将其读回,则ItemsSource绑定将丢失,但Label上的Content绑定不会丢失。

Style style = Application.Current.TryFindResource("DefaultStyle") as Style;

string s = XamlWriter.Save(style);
Style secondStyle = XamlReader.Parse(s) as Style;

有没有办法确保ItemsSource绑定被正确序列化或者将其重新添加回来?

当尝试从另一个项目的ResourceDictionary获取Style时也会发生这种情况,例如

ResourceDictionary styles = new ResourceDictionary();
styles.Source = new Uri(String.Format("pack://application:,,,/StyleCopyTest;component/Styles/{0}Styles.xaml", type));
return styles;

2 个答案:

答案 0 :(得分:2)

在WPF源代码中,ItemsSource定义为

[Bindable(true), CustomCategory("Content"),     DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IEnumerable ItemsSource { get; set; }

所以这不能被XamlWriter序列化。

因此,您必须编写自己的序列化程序或使用提及here

的方法

答案 1 :(得分:0)

我发现这个类here in code project可以帮助您序列化ItemsControl属性绑定:

using System;
using System.Linq;
using System.ComponentModel;

namespace GUIKonfigurator
{
    using System.Windows.Controls;

    public class ItemsControlTypeDescriptionProvider:TypeDescriptionProvider
    {
        private static readonly TypeDescriptionProvider defaultTypeProvider = TypeDescriptor.GetProvider(typeof(ItemsControl));

        public ItemsControlTypeDescriptionProvider(): base(defaultTypeProvider)
        {
        }

        public static void Register()
        {
            TypeDescriptor.AddProvider(new ItemsControlTypeDescriptionProvider(), typeof(ItemsControl));
        }

        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType,object instance)
        {
            ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);
            return instance == null ? defaultDescriptor: new ItemsControlCustomTypeDescriptor(defaultDescriptor);
        }
    }

    internal class ItemsControlCustomTypeDescriptor: CustomTypeDescriptor
    {
        public ItemsControlCustomTypeDescriptor(ICustomTypeDescriptor parent): base(parent)
        {
        }

        public override PropertyDescriptorCollection GetProperties()
        {
            PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(base.GetProperties().Cast<PropertyDescriptor>().ToArray());
            return ConvertPropertys(pdc);
        }

        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(base.GetProperties(attributes).Cast<PropertyDescriptor>().ToArray());
            return ConvertPropertys(pdc);
        }

        private PropertyDescriptorCollection ConvertPropertys(PropertyDescriptorCollection pdc)
        {
            PropertyDescriptor pd = pdc.Find("ItemsSource", false);
            if (pd != null)
            {
                PropertyDescriptor pdNew = TypeDescriptor.CreateProperty(typeof(ItemsControl), pd, new Attribute[]
                                                                                                       {
                                                                                                           new DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible),
                                                                                                           new DefaultValueAttribute("")
                                                                                                       });
                pdc.Add(pdNew);
                pdc.Remove(pd);
            }
            return pdc;
        }
    }
}

注册BindingConvertor后,您只需要这样注册:

EditorHelper.Register<BindingExpression, BindingConvertor>();
ItemsControlTypeDescriptionProvider.Register();

这是我做的快速测试,创建了一个ComboBox并将其序列化:

ComboBox cb = new ComboBox();
cb.Width = 100;
cb.Height = 20;
Binding b = new Binding("Model.Activity");
b.Source = this.DataContext;
cb.SetBinding(ComboBox.ItemsSourceProperty, b);
string xaml = _Serializer.SerializeControlToXaml(cb);

这里产生的Xaml包括ItemsSource绑定:

<ComboBox Width="100" Height="20" ItemsSource="{Binding Path=Model.Activity}" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />

希望这有帮助,我仍然需要花一些时间来理解它,但到目前为止似乎正在起作用......