hi,
i know the standard XamlWriter does not persist bindings. but what really stinks is that the current value the binding holds does not get serialized too.
my current - really stupid - workaround is to create a DependencyProperty fooProperty and the property foo. and also a property foo2. the Change-eventhandler of foo then writes its value into foo2.
you see: stupid.
does anyone have a better solution?
cheers
-- edit in response to thomas --
basically you're right. but when using a ItemsControl it looks a bit different:
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp"
Title="MainWindow" Height="350" Width="525"
x:Name="window"
>
<StackPanel>
<StackPanel.Resources>
<x:Array x:Key="arr1" Type="{x:Type local:TestData}">
<local:TestData Message="itemcontrol binding 1"/>
<local:TestData Message="itemcontrol binding 2"/>
</x:Array>
</StackPanel.Resources>
<local:Test Foo="hard coded"/>
<local:Test Foo="{Binding Message, ElementName=window}"/>
<ItemsControl ItemsSource="{StaticResource arr1}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:Test Foo="{Binding Path=Message }"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Message = "direct binding";
}
public static DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(MainWindow));
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
}
public class Test : TextBox
{
public static DependencyProperty FooProperty = DependencyProperty.Register("Foo", typeof(string), typeof(Test), new PropertyMetadata(OnFooChanged));
private static void OnFooChanged(DependencyObject d, DependencyPropertyChangedEventArgs a)
{
(d as Test).Text = a.NewValue as String;
}
public string Foo
{
get { return (string)GetValue(FooProperty); }
set { SetValue(FooProperty, value); }
}
protected override void OnMouseEnter(MouseEventArgs e)
{
Debug.Print("foo is really: " + Foo);
Debug.Print(XamlWriter.Save(this));
}
}
public class TestData : DependencyObject
{
public static DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(TestData));
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
}
if you run this test application and move the cursor over the different TextBoxes, you'll see that theres no problem serializing the hard coded and the directly bound values. the data templated binding on the contrary doesn't get serialized.
by the way there is no difference in using a ItemsSource generated by code instead of a StaticReference, just tested that..