How to access a named element of a derived user control in silverlight?

I have a custom user control in silverlight.

<UserControl x:Class="Problemo.MyBaseControl"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Border Name="HeaderControl" Background="Red" />
    </Grid>
</UserControl>

With the following code

public partial class MyBaseControl : UserControl
    {
        public UIElement Header { get; set; }

        public MyBaseControl()
        {
            InitializeComponent();
            Loaded += MyBaseControl_Loaded;
        }

        void MyBaseControl_Loaded(object sender, RoutedEventArgs e)
        {
            HeaderControl.Child = Header;
        }
    }

I have a derived control.

<me:MyBaseControl x:Class="Problemo.MyControl"
    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"
    mc:Ignorable="d"
    xmlns:me="clr-namespace:Problemo" 
    d:DesignHeight="300" d:DesignWidth="400">
    <me:MyBaseControl.Header>
        <TextBlock Name="header" Text="{Binding Text}" />
    </me:MyBaseControl.Header> 
</me:MyBaseControl>

With the following code.

 public partial class MyControl : MyBaseControl
    {
        public string Text
        {
            get; set;
        }

        public MyControl(string text)
        {
            InitializeComponent();
            Text = text;
        }
    }

I am trying to set the text value of a header text block in a derived control.

It would be nice to be able to set both methods, i.e. with data binding or in derived control code behind, but none of them work. When data binding, this does not work. If I try in the code behind, I get a null reference to the 'header'. This is silverlight 4 (not sure if it matters)

Any suggestions on how to do this with both data binding and code?

Greetings

+1
1

, , Derived- , . : INotifyPropertyChanged, -, .

MyControl Xaml: -

<me:MyBaseControl.Header>     
    <TextBlock Name="headerItem" />     
</me:MyBaseControl.Header>

MyControl: -

public partial class MyControl : MyBaseControl, INotifyPropertyChanged
{
    public MyControl ()
    {
        InitializeComponent();
        headerItem.SetBinding(TextBlock.TextProperty, new Binding("Text") { Source = this });
    }

    string _text;
    public string Text
    {
        get { return _text; }
        set { _text = value; NotifyPropertyChanged("Text"); }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    #endregion
}

. , , UserControl, , . , .

0

Source: https://habr.com/ru/post/1740879/


All Articles