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

标签 c# silverlight xaml nullpointerexception

我制作了一个名为 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 均为空

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

最佳答案

this thread 的帮助下我已经解决了这个问题.我需要做的就是让 Section 派生 ContentControl 而不是 User Control

关于c# - xaml 中定义的 Silverlight 控件在运行时为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15904463/

相关文章:

c# - 如何在不转到磁盘的情况下将字符串加载到 FileStream 中?

c# - 在 XAML 中显示矩阵

c# - 有没有办法减少使用 String.Format(...., p1, p2, p3) 的冗长?

c# - 将 Entity Framework 从 MVC 移动到另一个项目会导致重新迁移

silverlight - 在 Silverlight 中进行拼写检查的免费 en-GB 词典

wpf - 带有 "UniformToFill"的 WPF Viewbox 内的可滚动内容

c# - WPF TextBlock 绑定(bind)不起作用

c# - MVVM 中 Silverlight 的热键命令?

wpf datagridcheckboxcolumn 样式

c# - 在XAML中获得子控件的更聪明的方法是什么?