표준을 알고 XamlWriter 바인딩을 유지하지 않습니다. 하지만 실제로 악취가 나는 것은 바인딩이 보유하고있는 현재 값이 직렬화되지 않는다는 것입니다.XamlWriter 바인딩이 끊어졌습니다 - 확인! 그러나 가치를 지키는 방법? (ItemsControl)
내 현재 - 정말 바보 같은 - 해결 방법은 DependencyProperty fooProperty와 foo 속성을 만드는 것입니다. 또한 속성 foo2. foo의 Change-eventhandler는 그 값을 foo2에 기록합니다.
다음을 참조하십시오.
누구에게 더 좋은 해결책이 있습니까?
환호
- 편집 응답 토마스 -은 기본적으로 당신 말이 맞아. 이 테스트 응용 프로그램을 실행하고 다른 텍스트 상자 위로 커서를 이동하면
<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); }
}
}
는, 당신이이 직렬화 문제를 프로그래머 없습니다 볼 수 있습니다하십시오 ItemsControl에를 사용하는 경우 그러나 그것은 다른 조금 보인다 하드 코드 된 및 바로 바인딩 된 값. 반대로 템플릿 템플릿 데이터가 직렬화되지 않습니다. 대신 StaticReference의 코드에 의해 생성 된 ItemsSource 사용에 아무런 차이가 없을 그런데
은 단지 테스트 ..
답장을 보내 주셔서 감사합니다. 당신이 옳은 것 같아요 .. 내 테스트 베드에서 아직이 버그를 재현 할 수 없습니다. 내 실제 응용 프로그램에는 ContentControls가 많이 있습니다. TabControl, ItemControl, ItemControl. 거기에 바인딩은 RelativeSource FindAncestor를 통해 작동합니다 .. 좀 더 살펴 보겠습니다. – kris
보세요. :) – kris