Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XamlWriter失去绑定-好!但如何保持价值呢?(项目控制)_C#_Binding_Dependency Properties_Itemscontrol_Xamlreader - Fatal编程技术网

C# XamlWriter失去绑定-好!但如何保持价值呢?(项目控制)

C# XamlWriter失去绑定-好!但如何保持价值呢?(项目控制),c#,binding,dependency-properties,itemscontrol,xamlreader,C#,Binding,Dependency Properties,Itemscontrol,Xamlreader,我知道标准的XamlWriter不会持久化绑定。但真正令人讨厌的是绑定持有的当前值也没有被序列化 我目前的解决方法是创建dependencProperty fooProperty和属性foo。同时也是一个属性foo2。然后,foo的ChangeEventHandler将其值写入foo2 你看:愚蠢 有谁有更好的解决方案吗 干杯 --编辑回应托马斯-- 基本上你是对的。但是当使用ItemsControl时,它看起来有点不同: <Window x:Class="TestApp.MainWind

我知道标准的XamlWriter不会持久化绑定。但真正令人讨厌的是绑定持有的当前值也没有被序列化

我目前的解决方法是创建dependencProperty fooProperty和属性foo。同时也是一个属性foo2。然后,foo的ChangeEventHandler将其值写入foo2

你看:愚蠢

有谁有更好的解决方案吗

干杯

--编辑回应托马斯--

基本上你是对的。但是当使用ItemsControl时,它看起来有点不同:

<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>
如果运行此测试应用程序并将光标移动到不同的文本框上,您将看到序列化硬编码值和直接绑定值没有问题。 相反,数据模板化绑定不会序列化

顺便说一下,使用由代码生成的ItemsSource而不是StaticReference没有什么区别,只是测试了..

该值由
XamlWriter
保存。见本页:

序列化过程将取消对由各种标记扩展格式(如StaticResource或Binding)生成的对象的公共引用。在应用程序运行时创建内存中对象时,这些对象已经被取消引用,并且保存逻辑不会重新访问原始XAML以将这些引用恢复到序列化输出这可能会冻结任何数据绑定或资源获取的值,使其成为运行时表示上次使用的值,只有有限或间接的能力将此类值与本地设置的任何其他值区分开来。图像还被序列化为项目中存在的图像的对象引用,而不是原始源引用,从而丢失最初引用的文件名或URI。即使是在同一页中声明的资源也会被序列化到引用它们的位置,而不是作为资源集合的键保留

我只是做了一个简单的测试,它对我来说很好:

public class Test : DependencyObject
{
    public static DependencyProperty FooProperty = DependencyProperty.Register("Foo", typeof(string), typeof(Test));

    public string Foo
    {
        get { return (string)GetValue(FooProperty); }
        set { SetValue(FooProperty, value); }
    }

    public static DependencyProperty BarProperty = DependencyProperty.Register("Bar", typeof(int), typeof(Test));

    public int Bar
    {
        get { return (int)GetValue(BarProperty); }
        set { SetValue(BarProperty, value); }
    }
}

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        Message = "Hello";
        Answer = 42;

        var t = new Test();

        Binding fooBinding = new Binding("Message") { Source = App.Current };
        BindingOperations.SetBinding(t, Test.FooProperty, fooBinding);

        Binding barBinding = new Binding("Answer") { Source = App.Current };
        BindingOperations.SetBinding(t, Test.BarProperty, barBinding);

        var s = XamlWriter.Save(t);
        Debug.Print(s);

    }

    public string Message { get; set; }
    public int Answer { get; set; }
}
此程序在调试输出中打印以下XAML:

<Test Foo="Hello" Bar="42" xmlns="clr-namespace:WpfApplication1;assembly=WpfApplication1" />

所以在你的情况下它不起作用肯定还有另一个原因。。。您确定没有任何绑定错误吗?序列化对象之前,对象是否具有预期值?

该值由
XamlWriter
保存。见本页:

序列化过程将取消对由各种标记扩展格式(如StaticResource或Binding)生成的对象的公共引用。在应用程序运行时创建内存中对象时,这些对象已经被取消引用,并且保存逻辑不会重新访问原始XAML以将这些引用恢复到序列化输出这可能会冻结任何数据绑定或资源获取的值,使其成为运行时表示上次使用的值,只有有限或间接的能力将此类值与本地设置的任何其他值区分开来。图像还被序列化为项目中存在的图像的对象引用,而不是原始源引用,从而丢失最初引用的文件名或URI。即使是在同一页中声明的资源也会被序列化到引用它们的位置,而不是作为资源集合的键保留

我只是做了一个简单的测试,它对我来说很好:

public class Test : DependencyObject
{
    public static DependencyProperty FooProperty = DependencyProperty.Register("Foo", typeof(string), typeof(Test));

    public string Foo
    {
        get { return (string)GetValue(FooProperty); }
        set { SetValue(FooProperty, value); }
    }

    public static DependencyProperty BarProperty = DependencyProperty.Register("Bar", typeof(int), typeof(Test));

    public int Bar
    {
        get { return (int)GetValue(BarProperty); }
        set { SetValue(BarProperty, value); }
    }
}

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        Message = "Hello";
        Answer = 42;

        var t = new Test();

        Binding fooBinding = new Binding("Message") { Source = App.Current };
        BindingOperations.SetBinding(t, Test.FooProperty, fooBinding);

        Binding barBinding = new Binding("Answer") { Source = App.Current };
        BindingOperations.SetBinding(t, Test.BarProperty, barBinding);

        var s = XamlWriter.Save(t);
        Debug.Print(s);

    }

    public string Message { get; set; }
    public int Answer { get; set; }
}
此程序在调试输出中打印以下XAML:

<Test Foo="Hello" Bar="42" xmlns="clr-namespace:WpfApplication1;assembly=WpfApplication1" />


所以在你的情况下它不起作用肯定还有另一个原因。。。您确定没有任何绑定错误吗?在序列化对象之前,对象是否具有预期值?

在ItemTemplate内部绑定的相同情况下,我看到了相同的情况:

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

虽然不漂亮,但很管用。(无论您丢失了什么属性,都可以很容易地对其进行修改。)

在ItemTemplate内部绑定的相同情况下,我看到了同样的情况:

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

虽然不漂亮,但很管用。(无论您失去什么财产,都可以轻松修改。)

感谢您的回复。看来你是对的。。在我的测试床上,我还不能复制这个bug。在我的实际应用程序中,有一堆ContentControls。TabControl,ItemControl,ItemControl。那里的绑定通过RelativeSource FindAncestor工作。。我再试试。谢谢你的回复。看来你是对的。。在我的测试床上,我还不能复制这个bug。在我的实际应用程序中,有一堆ContentControls。TabControl,ItemControl,ItemControl。那里的绑定通过RelativeSource FindAncestor工作。。我再试试看。