English 中文(简体)
为什么我的银灯用户控制财产无效?
原标题:Why is my Silverlight usercontrol property null?

我有一个用户控制器:

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Name="TextBlock1" Text="Text for"></TextBlock> 
    </Grid>

它有一个 cs 文件 :

public partial class MyUserControl: UserControl
{
    public string MyProperty { get; set; }

    public MyUserControl()
    {
        InitializeComponent();
        DoSomething();
    }

    private void DoSomething()
    {
        TextBlock1.Text = TextBlock1.Text + MyProperty;
    }
}

我想把这个用户控制程序 添加到另一个用户控制中 :

            UserControls.MyUserControl myUserControl = new UserControls.MyUserControl();
            myUserControl.MyProperty = "Something";
            MyStackPanel.Children.Add(myUserControl);

我以为我以前做过这样的事 没有任何问题 但在这个案子里 我的财产总是无效的 我做错了什么?

最佳回答

您的代码是好的, 除非您正在调用 < code> Do somethinging () () 。 您应该避免在控件的构建器中使用 < em> GUI 互动 , 原因很多( 其中并非最不重要的就是您的属性尚未按照 Mark Hall 的指示设定) 。 我建议不添加不同的构建符, 只取初始属性

您只是要推迟拨打 Loaded 控制事件。

Assuming you set the event in the Xaml like this:

<navigation:Page x:Class="MyUserControl" 
    ...
    Loaded="Page_Loaded">

Your code would look like:

public partial class MyUserControl: UserControl
{
    public string MyProperty { get; set; }

    public MyUserControl()
    {
        InitializeComponent();
    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
       DoSomething();
    }

    private void DoSomething()
    {
        TextBlock1.Text = TextBlock1.Text + MyProperty;
    }
}

If you want to set the Loaded event in code it would just look like:

public partial class MyUserControl: UserControl
{
    public string MyProperty { get; set; }

    public MyUserControl()
    {
        InitializeComponent();
        Loaded += Page_Loaded; 
    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
       DoSomething();
    }

    private void DoSomething()
    {
        TextBlock1.Text = TextBlock1.Text + MyProperty;
    }
}

它只是由您来设置您想要的 建筑后的所有属性, 但是在页面被加载之前, 您已经在这样做了 。

UserControls.MyUserControl myUserControl = new UserControls.MyUserControl();
myUserControl.MyProperty = "Something";
MyStackPanel.Children.Add(myUserControl); // This makes it visible when Loaded event is called
问题回答

正在发生的事情是,您在设定 < code> myProperty 之前,正在将 < code > Do somethinging 调用在您的 Contructor 中,所以 < code > MyProperty 等于空号。 如果您想要在创建 < code > User Control 时数据存在, 您可以将其设置在构造器中 。

或(或)

public partial class MyUserControl : UserControl
{
    public string MyProperty { get; set; }

    public MyUserControl()   //Default Constructor
    {
        InitializeComponent();
    }

    public MyUserControl(string MyData)
    {
        InitializeComponent();
        MyProperty = MyData;
        DoSomething();
    }
    private void DoSomething()
    {
        TextBlock1.Text = TextBlock1.Text + MyProperty;
    }

}

然后像这样创造它。

MyUserControl myUserControl = new MyUserControl(" Something");
MyStackPanel.Children.Add(myUserControl); 




相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...