簡體   English   中英

單擊Button控件事件中未觸發的圖像

[英]Click image within Button control event not firing

我有以下代碼,單擊圖像時不會觸發Click事件。 當我在圖像外部單擊時,按鈕單擊事件將觸發。

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

在x_Click事件中只是MessageBox.Show(“Button clicked”); 其中x是btn_Main等......

單擊圖像時,imgButtonImage_Click不會觸發。 我也試過改變ZIndex無濟於事。

你為什么在一個按鈕內的stackpanel里面使用<telerik:RadImageEditor x:Name="imgButtonImage" /> 在按鈕內部使用圖像編輯器對我來說沒有意義。 如果要在按鈕內顯示帶有文本的圖像,可以使用類似於:

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

要使用base64string作為圖像源,您可以執行以下操作:

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;

要封裝此代碼,並直接在xaml中使用,您可以創建一個IValueConverter並使用類似<Image Source="{Binding ...stringDataSource..., Converter={StaticResource MyBase64ImageConverter}}"/>

用JSimoes幫助弄清楚它。

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

謝謝。

我不知道Telerik RadImageEditor控件,但我猜它會處理鼠標冒泡事件。 您應該使用隧道事件(事件名稱以Preview開頭),例如PreviewMouseLeftButtonDown

嘗試將事件添加到xaml

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM