2012-03-20 77 views
0

大家好大家如何給我的按鈕一個eventhanlder b2.Click + = new RoutedEventHandler(BtnChiefAns); 我試過了,它沒有工作給事件處理程序生成按鈕後面的自定義代碼

我的按鈕是一個自定義的按鈕 這是代碼來調用它

ButtonLeft B2 =新ButtonLeft();

當我這樣做 b2.Click + = new RoutedEventHandler(BtnChiefAns); 這將突出單擊Word和說未知成員「點擊」「‘定製用戶控制’」

這是我定製的按鈕後面的代碼

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:edc="clr-namespace:Microsoft.Expression.Controls;assembly=Microsoft.Expression.Drawing" 
mc:Ignorable="d" 
x:Class="Volunteer.LayoutRootControl" Height="127" Width="200"> 
<UserControl.Resources> 
    <Style x:Key="ButtonStyle8" TargetType="Button"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Grid> 
         <edc:Callout AnchorPoint="0.85,1.19" CalloutStyle="Rectangle" Fill="#FFE054EF" FontSize="14.666999816894531" Stroke="Black"/> 
         <ContentPresenter Height="96" Width="196"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

<Button Style="{StaticResource ButtonStyle8}" HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="200"> 
    <Button.Content> 
     <StackPanel Orientation="Horizontal" Width="197" Margin="-40,-34,-41,-32"> 
      <TextBlock Width="196" x:Name="BtnIN3" Text="" FontSize="22" TextWrapping="Wrap" Margin="0,0,0,-12" Height="95" /> 
     </StackPanel> 
    </Button.Content> 
</Button> 

我需要能夠點擊這個按鈕:(提前致謝!

回答

0

由於這是一個UserControl,它沒有Click事件,您需要實現它。在爲用戶控件的代碼,添加以下內容:

public event RoutedEventHandler Click; 

,然後勾你在XAML和實施有按鈕的Click事件,寫類似如下:

if (Click != null) 
{ 
    Click(this, e); // Where "e" is the parameter you got from the button. 
} 
+0

??????????????? – TransformBinary 2012-03-20 10:16:25

+0

TransformBoy,如果我不明白你的問題,你能解釋一下問題是什麼?如果問號,我幾乎無法幫助或理解問題。 – 2012-03-20 14:53:37

+0

我不知道該怎麼處理它sry – TransformBinary 2012-03-20 16:11:23

0

如果您的UserControl只有一個Button,您可以創建一個帶有自定義樣式的Button,而不需要UserControl。然後,你可以在XAML或以這種方式創建按鈕:

Button b = new Button() 
{ 
    Style = (Style)Application.Current.Resources["ButtonStyle8"] 
}; 
b.Click += ... 

這將是更容易,更有效地

+0

嗯,它不工作:( – TransformBinary 2012-03-20 09:59:01

相關問題