Question

I have made an UserControl in WPF & C#, which is added later to an ItemsControl.

My problem is that I need a button and an image inside my UserControl, but when I drop a Button in UserControl and compile, the button seems to be disabled. And, if I add any image control into UserControl and I try to set ImageSource, the Image does not display. I have tried to add my control directly to the main window (outside of ItemsControl), but the result is the same.

What am I doing wrong?

XALM Code:

<UserControl x:Class="MyProject.MyCustomClass"
         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" 
         MinHeight="100" MinWidth="400"
         HorizontalAlignment="Stretch" IsHitTestVisible="False">
<Grid x:Name="MainGrid" Background="White">


    <Border BorderThickness="0,0,0,1" BorderBrush="Black">
    </Border>
    <TextBlock x:Name="Header1" HorizontalAlignment="Left" Height="33" Margin="105,10,88,0" TextWrapping="NoWrap" Text="Header1" VerticalAlignment="Top" FontSize="22" TextTrimming="CharacterEllipsis"/>
    <TextBlock x:Name="Header2" HorizontalAlignment="Left" Height="20" Margin="105,43,0,0" TextWrapping="NoWrap" Text="Header2" VerticalAlignment="Top" FontSize="14" TextTrimming="CharacterEllipsis"/>
    <Button Height="80" Margin="316,10,5,0" VerticalAlignment="Top" Width="74" VerticalContentAlignment="Bottom" HorizontalAlignment="Right" Click="Button_Click">
        <TextBlock TextAlignment="Center">Add to<LineBreak/>the list</TextBlock>
    </Button>
    <Image x:Name="Image1" HorizontalAlignment="Left" Height="80" Margin="10,10,0,0" VerticalAlignment="Top" Width="80" Stretch="UniformToFill" Source="pack://application:,,,/MyImage.png" />
<!--I've already tried to set Source form app resources but it does not work-->




</Grid>

And in my C# code I don't modify any special thing.

Was it helpful?

Solution

You need to set the IsHitTestVisible property of your UserControl to True. If this property is set to false, it means that the UserControl won't detect any clicking event.

For the image make sure that you added the png file to your solution in the Solution Explorer.

OTHER TIPS

For the image problem try right clicking on the image in Solution Explorer, then click Properties and set the Build Action to Resource.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top