简体   繁体   English

WPF - 无法通过反序列化设置动态添加的用户控件的属性

[英]WPF - Unable to set properties of dynamically added User Control via deserialization


File des.txt --> this file is deserialized to xaml tree 文件des.txt - >此文件被反序列化为xaml树

<NewLabel NewText="some new text" LabelText="yes" xmlns="clr-namespace:WpfPractice;assembly=WpfPractice" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><av:Grid><av:Canvas><av:TextBox Width="100" Height="30">yes</av:TextBox></av:Canvas></av:Grid></NewLabel>

Mainwindow.xaml Mainwindow.xaml

<Window x:Class="WpfPractice.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfPractice"
    Title="MainWindow"
    Width="525"
    Height="350">
<Grid Name="theGrid">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Button Grid.Row="1"
            Width="50"
            Height="25"
            Click="Button_Click_2"
            Content="Click" />
</Grid>

MainWindow.xaml.cs MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WpfPractice
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    NewLabel someNewLabel;
    public MainWindow()
    {
        InitializeComponent();

        someNewLabel = (NewLabel)DeserializeXaml(File.ReadAllText(@"D:\des.txt"));

        theGrid.Children.Add(someNewLabel);
    }

    void Serialize()
    {
        NewLabel theNewLabel = new NewLabel();

        theNewLabel.NewText = "some new text";

        string xamlString = XamlWriter.Save(theNewLabel);

        XmlReader reader = XmlReader.Create(new StringReader(xamlString));

        UIElement copy = XamlReader.Load(reader) as UIElement;
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        someNewLabel.LabelText = "new value";

        someNewLabel.SetLabelValue("asdfsadgsafgreg");

        //Serialize();
    }


    public object DeserializeXaml(string xaml)
    {
        StringReader stringReader = new StringReader(xaml);

        XmlReader xmlReader = XmlReader.Create(stringReader);

        return System.Windows.Markup.XamlReader.Load(xmlReader);
    }
    }

} }

NewLabel.xaml NewLabel.xaml

<UserControl x:Class="WpfPractice.NewLabel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         d:DesignHeight="300"
         d:DesignWidth="300"
         mc:Ignorable="d">
    <Grid Name="theGrid">


    </Grid>

NewLabel.xaml.cs NewLabel.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfPractice
{
/// <summary>
/// Interaction logic for NewLabel.xaml
/// </summary>
public partial class NewLabel : UserControl, INotifyPropertyChanged
{

    public static DependencyProperty NameProperty =
   DependencyProperty.Register("NewText",
                               typeof(string),
                               typeof(NewLabel),
                               new PropertyMetadata(String.Empty));

    public string NewText
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }


    private string _labelText = "yes";
    public string LabelText
    {
        get
        {
            return _labelText;
        }
        set
        {
            _labelText = value;
            OnPropertyChanged("LabelText");
        }
    }

    Label theLabel;
    public NewLabel()
    {
        InitializeComponent();

        theLabel = new Label();

        theLabel.Height = 30;

        theLabel.Width = 100;

        theLabel.Content = "old value";

        Canvas theCanvas = new Canvas();

        theCanvas.Children.Add(theLabel);

        theGrid.Children.Add(theCanvas);

        Binding b = new Binding("LabelText");

        b.Source = this;

        theLabel.SetBinding(TextBox.TextProperty, b);

        theLabel.MouseDoubleClick += theLabel_MouseDoubleClick;

        this.DataContext = this;
    }

    void theLabel_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("clicked!");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }


    public void SetLabelValue(string val)
    {
        theLabel.Content = val;
    }
}

} }

Question: 题:

Both these statements in MainWindow.xaml.cs 这两个语句都在MainWindow.xaml.cs中

      someNewLabel.LabelText = "new value";

        someNewLabel.SetLabelValue("asdfsadgsafgreg");

do not work. 不工作。 In other words, I am unable to set the textbox property to "new value" 换句话说,我无法将textbox属性设置为“new value”

You are creating the binding in NewLable constructor, but then you load a new visual tree from a file which overrides the original one. 您正在NewLable构造函数中创建绑定,但随后从一个覆盖原始文件的文件中加载一个新的可视树。 That why you cant update the label nor by binding neither by setting the value directly because its not in the visual tree anymore. 这就是为什么你不能更新标签,也不能通过直接设置值来绑定,因为它不再在可视树中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM