k1f1
k1f1

Reputation: 433

Click image within Button control event not firing

I have the following code and the Click event is not firing when I click the image. When I click outside the image the button click event fires.

XAML:

<Grid x:Name="LayoutRoot" Background="White">
    <Button x:Name="btnMain" Background="Purple" >             
        <StackPanel x:Name="spButtonPanel" Background="Black">
            <telerik:RadImageEditor x:Name="imgButtonImage" />
            <TextBlock x:Name="tbButtonText" />
        </StackPanel>
    </Button>
</Grid>

CS:

this.btnMain.Click += new RoutedEventHandler(btnMain_Click);
this.imgButtonImage.MouseLeftButtonDown += new MouseButtonEventHandler(imgButtonImage_Click);
this.spButtonPanel.MouseLeftButtonDown += new MouseButtonEventHandler(spButtonPanel_Click);  

In the x_Click events are simply MessageBox.Show("Button clicked"); Where x is btn_Main etc...

The imgButtonImage_Click will not fire when I click the image. I have tried also changing ZIndex to no avail.

Upvotes: 0

Views: 1206

Answers (4)

k1f1
k1f1

Reputation: 433

Figured it out with JSimoes help.

        BitmapImage bi = new BitmapImage();
        bi.SetSource(new MemoryStream(imageBytes));
        imgButtonImage.Source = bi;

Thanks.

Upvotes: 1

Jo&#227;o Sim&#245;es
Jo&#227;o Sim&#245;es

Reputation: 1361

Why are you using <telerik:RadImageEditor x:Name="imgButtonImage" /> inside the stackpanel which is inside a button? Having an image editor inside a button doesn't make sense to me. If you want to show an image with text inside a button you can use something similar to:

<Button Style="{StaticResource MyButtonStyle}">  
    <StackPanel>  
        <Image Source="{Binding ...}  Margin="10" />  
        <TextBlock Text="Localizable Text" />  
    </StackPanel>  
</Button>

To use a base64string as the image source you could do the following:

string bgImage64 = //   image stored in string
byte[] binaryData = Convert.FromBase64String(bgImage64);

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(binaryData);
bi.EndInit();

imgButtonImage.Source = bi;

To encapsulate this code, and use directly in the xaml, you can create an IValueConverter and use something like <Image Source="{Binding ...stringDataSource..., Converter={StaticResource MyBase64ImageConverter}}"/>

Upvotes: 1

Lone Starr
Lone Starr

Reputation: 65

Try adding the event to the xaml

<Button x:Name="btnMain" Background="Purple" Click="btnMain_Click" >           

Upvotes: 0

Sisyphe
Sisyphe

Reputation: 4684

I don't know the Telerik RadImageEditor control but I guess it handles the mouse bubbling events. You should use tunneling events instead (events names starting with Preview) like PreviewMouseLeftButtonDown for example.

Upvotes: 0

Related Questions