user3160134
user3160134

Reputation: 199

Custom UserControl not showing up in ListBox

I have a problem displaying custom UserControls in my ListBox programmatically. I just can't seem to figure out what is wrong. The list-item shows up without image or text.

My project consists of:

Code of MainWindow.xaml.cs

private void cvMenuItem_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
{
    lstContacts.Items.Clear();

    cvMenuItem test = new cvMenuItem("test", 
        Environment.GetEnvironmentVariable("USERPROFILE") + @"\Downloads\images.jpg");

    lstContacts.Items.Add(test);
}

Code of cvMenuItem.xaml.cs

public partial class cvMenuItem : UserControl
{
    public cvMenuItem()
    {
        InitializeComponent();
    }

    public cvMenuItem(string text, string Logo)
    {
        this.Height = 50;
        this.Width = 186;
        txtService = new TextBlock() { Width = 100, Height = 50 };
        imgLogo = new Image() { Width = 50, Height = 50 };

        //Just found out, adding the objects as childeren partially works
        this.AddChild(imgLogo);
        //But I can't add txtService as Childeren
        //this.AddChild(txtService);

        this.Services = text;
        this.Logo = Logo;
    }

    public string Services
    {
        get{ return txtService.Text.ToString() }
        set
        {
            txtService.Text = value;
        }
    }

    public string Logo
    {
        get{ return imgLogo.Source.ToString(); }
        set
        {
            var uriSource = new Uri(value);
            imgLogo.Source = new BitmapImage(uriSource);
        }
    }

My cvMenuItem.xaml.cs

<UserControl x:Class="WpfApplication1.cvMenuItem"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"  Height="50" Width="186">
    <Grid Width="186" VerticalAlignment="Top">
        <Image Name="imgLogo" Height="50" Width="50" HorizontalAlignment="Left" VerticalAlignment="Top" OpacityMask="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}" />
        <TextBlock Name="txtService" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Bottom" Height="18" Width="121" Margin="70,0,0,18" RenderTransformOrigin="0.499,1.932"/>
    </Grid>
</UserControl>

Upvotes: 2

Views: 330

Answers (1)

Peter Hansen
Peter Hansen

Reputation: 8907

First of all you need to call InitializeComponent in the custom constructor you have added, as that is needed to process the XAML properly. Otherwise all the controls you add in the XAML will be null when running the application.

Additionally it makes no sense to create the TextBlock and Image again in the code-behind. You just have to use the ones created in the XAML.

So to get it working, change the code in the constructor to the following:

public cvMenuItem(string text, string Logo)
{
    InitializeComponent();

    this.Height = 50;
    this.Width = 186;

    this.Services = text;
    this.Logo = Logo;
}

Upvotes: 1

Related Questions