Silverlight UserControl无法显示

时间:2014-03-31 04:55:07

标签: c# .net silverlight user-controls

我想要以编程方式在stackpanel中显示一个简单的UserControl

当我这样做时,UC不会显示在屏幕上。如果我从工具箱中拖动一个实例,它可以正常工作。

用户控件是XAML

<UserControl x:Class="MYProj.Controls.SpecialNumberOption"
    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="200">
    <Viewbox>
    <Grid x:Name="LayoutRoot" Background="White" Width="200" Height="300">
        <Button x:Name="buttonMe" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="180" Height="150" Style="{StaticResource NumberButtonStyle}" Click="buttonMe_Click"/>
        <TextBlock x:Name="subText" HorizontalAlignment="Left" Margin="10,165,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="125" Width="180" FontStyle="Italic" TextAlignment="Center"/>

    </Grid>
    </Viewbox>
</UserControl>

Codebehind

public partial class SpecialNumberOption : UserControl
{


    public event RoutedEventHandler Click;



    public SpecialNumberOption()
    {
        InitializeComponent();


        this.applyStyle();
    }

    public SpecialNumberOption(SurveyQuestionOption option)
    {
        this.buttonMe.Content = option.Text;
        this.subText.Text = option.SubText;

        this.applyStyle();
    }


    private void applyStyle()
    {

        this.buttonMe.FontSize = 26;
        this.buttonMe.Background = standardBackground;
        this.buttonMe.Foreground = standardForecolor;

    }

    ///Raise the event to the outside

    private void buttonMe_Click(object sender, RoutedEventArgs e)
    {
        Click(this, e);
    }



}

实施

这是我添加控件的方式

    foreach (var y in x.Options)
    {

            //Create new instance from An object
            var r = new SpecialNumberOption(y);

            // Set visibility
            r.Visibility = System.Windows.Visibility.Visible;


            r.IsEnabled = false;

            //Assign the event handler
            r.Click += r_Click;

            //This is my stackpanel
            listOptions.Children.Add(r);


            ....
    }




    //Handle the click event
    void r_Click(object sender, RoutedEventArgs e)
    {

        SpecialNumberOption o = (SpecialNumberOption)e.OriginalSource;
        ....

    }

更新

我发现当我使用备用构造函数时,这就是它停止工作的时候。 我必须使用默认构造函数。这是正常的吗?

1 个答案:

答案 0 :(得分:0)

我没有检查你发布的所有代码的正确性,但这是你的构造函数的问题:你必须调用InitializeComponent(它必须在你访问任何命名元素之前发生)

这是一个带有修复的版本:

public SpecialNumberOption()
{
    InitializeComponent();
    this.applyStyle();
}

public SpecialNumberOption(SurveyQuestionOption option) : this () //will call the empty default constructor
{
    this.buttonMe.Content = option.Text;
    this.subText.Text = option.SubText;
}

备注:我认为控件的风格不仅仅是空的默认构造函数。 应通过属性设置器设置参数。它使您和任何将重用您的控件的人能够在xaml中使用和参数化。