如何在Silverlight中访问派生用户控件的命名元素? [英] How to access a named element of a derived user control in silverlight?

查看:55
本文介绍了如何在Silverlight中访问派生用户控件的命名元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Silverlight中有一个自定义基本用户控件。

I have a custom base 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>

后面带有以下代码

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;
        }
    }

我有一个派生的控件。

<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>

后面带有以下代码。

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

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

我正在尝试设置派生控件中的标头文本块。

I'm trying to set the text value of the header textblock in the derived control.

能够同时设置两种方式(例如,使用数据绑定或在后面的派生控制代码中进行设置)会很好,但都不起作用。进行数据绑定后,此操作将无效。如果我尝试使用后面的代码,则会得到对标题的空引用。这是Silverlight 4(不确定是否有区别)

It would be nice to be able to set both ways, i.e. with databinding or in the derived control code behind, but neither work. With the data binding, it doesn't work. If I try in the code behind I get a null reference to 'header'. This is silverlight 4 (not sure if that makes a difference)

关于如何使用数据绑定和代码的任何建议吗?

Any suggestions on how to do with with both databinding and in code ?

欢呼

推荐答案

首先,我将向您展示如何调整派生控件来处理此问题。您需要做两件事,首先需要实现 INotifyPropertyChanged ,其次要向用户控件添加绑定。

First of all I'll show you how to adjust your Derived control to handle this. You need to do two things, first you need to implement INotifyPropertyChanged and secondly you to add the binding to the user control.

MyControl Xaml:-

MyControl Xaml:-

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

MyControl代码:-

MyControl code:-

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的类,就应该退后一步,询问是否应该将基项和派生项作为模板控件。如果有时间,我会尝试使用模板控件添加代码版本。

This should get you working. However, as soon as you feel you need to inherit a UserControl based class you should take a step back and ask whether the base and derived items ought to be templated controls instead. If I get time I'll try to add a version of your code in terms of templated controls.

这篇关于如何在Silverlight中访问派生用户控件的命名元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆