简体   繁体   中英

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


File des.txt --> this file is deserialized to xaml tree

<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

<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

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

<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

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

      someNewLabel.LabelText = "new value";

        someNewLabel.SetLabelValue("asdfsadgsafgreg");

do not work. In other words, I am unable to set the textbox property to "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. That why you cant update the label nor by binding neither by setting the value directly because its not in the visual tree anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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