Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have strange problem, which I don't know how to find - I looked for similair posts here, but failed.

Problem is that I have custom control in WPF and, obviously, I want to reuse it in multiple projects.

I have image background in that control with label over it (assured with Panel.ZIndex).

In one project it is showing correctly, but in another just Label is showing, image for some reason does no display.

What could problem be? I am loosing my mind over this...

Below code of a control:

<UserControl x:Class="SampleControls.LabelWithBoxBackground"
             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" 
             xmlns:local="clr-namespace:SampleControls"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="400" x:Name="labelWithBoxBackground">
    <Grid>
        <Image Source="pack://application:,,,/Images/boxImage.png" Stretch="Fill" Panel.ZIndex="1"/>
        <TextBlock Background="White" Text="{Binding ElementName=labelWithBoxBackground, Path=Text}" Margin="0,20,0,0" Panel.ZIndex="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" FontFamily="Calibri"/>
    </Grid>
</UserControl>

Code behind:

public partial class LabelWithBoxBackground : UserControl
{
  public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(LabelWithBoxBackground), new FrameworkPropertyMetadata(string.Empty));

  public string Text
  {
    get { return GetValue(TextProperty).ToString(); }
    set { SetValue(TextProperty, value); }
  }

  public LabelWithBoxBackground()
  {
    InitializeComponent();
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
277 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use a full Resource File Pack URI, including the assembly name (not the namespace) of your UserControl library, as shown below.

Otherwise WPF resolves the Pack URI with the name of the "local" assembly, which may be that of the main application.

Source="pack://application:,,,/SampleControls;component/Images/boxImage.png"

Also make sure that the Build Action of the image file is set to Resource.


As a note, setting Panel.ZIndex is pointless here. The elements are stacked by default in the order they are declared in XAML, so the TextBlock is always on top of the Image, even without setting ZIndex.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...