简体   繁体   中英

Silverlight controls defined in xaml are null at runtime

I have made a user control called Section which has 2 custom DependencyProperty : Title and Elements (kinda like content). I have used Section on several pages it displays just fine on every page.

The following xaml code is a part of my AccountView class

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

When i try set the text of my TextBlocks in c# code all the TextBlock are 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;
    }

My Section c# class looks as follow:

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

I have solved this with a little help from this thread . All i needed to do was to make Section derive ContentControl instead of User Control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM