在xaml中定义的Silverlight控件在运行时为空

本文关键字:运行时 控件 xaml 定义 Silverlight | 更新日期: 2023-09-27 18:08:20

我做了一个名为Section的用户控件,它有2个自定义DependencyProperty:标题和元素(有点像内容)。我在几个页面上使用了Section,它在每个页面上都显示得很好。

下面的xaml代码是我的AccountView类的一部分

    <Controls:Section Title="Epic Movies" Grid.Column="1" Grid.Row="1" x:Name="DescriptionSection" Visibility="Collapsed">
        <Controls:Section.Elements>
            <StackPanel>
                <TextBlock Style="{StaticResource FrameLabel}" Text="Description:" Grid.Column="1"/>
                <TextBlock x:Name="DescriptionField" Style="{StaticResource FrameText}" Text="some text..." Grid.Column="1" Grid.Row="1"/>
                <TextBlock Text="Products:" Style="{StaticResource FrameLabel}"/>
                <ScrollViewer Margin="12,0" VerticalScrollBarVisibility="Auto" MaxHeight="180">
                    <StackPanel x:Name="ProductList"/>
                </ScrollViewer>
                <TextBlock Style="{StaticResource FrameLabel}" Text="Category"/>
                <TextBlock x:Name="CategoryField" Style="{StaticResource FrameText}" Text="some category"/>
                <TextBlock Style="{StaticResource FrameLabel}" Text="Views:"/>
                <TextBlock x:Name="ViewsField" Style="{StaticResource FrameText}" Text="12322 Views"/>
                <TextBlock Style="{StaticResource FrameLabel}" Text="Price:"/>
                <TextBlock x:Name="PriceField" Style="{StaticResource FrameText}" Text="455$"/>
                <Button Content="Go to Module" Grid.Column="1" FontSize="15" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="20,15"/>
            </StackPanel>
        </Controls:Section.Elements>
    </Controls:Section>

当我尝试在c#代码中设置我的TextBlock的文本时,所有的TextBlock都是null

    private void ShowModuleInformation(Module m)
    {
        DescriptionSection.Title = m.Name;
        //DescriptionField is null even though clearly defined in xaml
        DescriptionField.Text = m.Description;
        PriceField.Text = m.Price + "";
        ViewsField.Text = m.views+"";
        CategoryField.Text = m.Category.Name;
        foreach (Product p in m.Products)
        {
            TextBlock t = new TextBlock() { Text = p.Name };
            ProductList.Children.Add(t);
        }
        DescriptionSection.Visibility = Visibility.Visible;
    }

我的Section c#类如下所示:

    public partial class Section : UserControl
{
    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
        "Title", typeof(string), typeof(Section), new PropertyMetadata(new PropertyChangedCallback(TitleChanged)));
    public static readonly DependencyProperty ElementsProperty = DependencyProperty.Register(
        "Elements", typeof(UIElement), typeof(Section), new PropertyMetadata(new PropertyChangedCallback(ElementsChanged)));
    public Section()
    {
        InitializeComponent();
    }
    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    public UIElement Elements
    {
        get { return (UIElement)GetValue(ElementsProperty); }
        set { SetValue(ElementsProperty, value); }
    }
    private static void TitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var sec = (Section)d;
        if (sec != null)
        {
            sec.TitleField.Text = (string)e.NewValue;
        }
    }
    private static void ElementsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Section sec = (Section)d;
        if (sec != null)
        {
            sec.ElementPanel.Content = (UIElement)e.NewValue;
        }
    }

在xaml中定义的Silverlight控件在运行时为空

我用这个线程的一点帮助解决了这个问题。我所需要做的就是使Section派生ContentControl而不是User Control