2014-02-07 31 views
0

我在Generic.xaml文件中創建一個自定義的控制按鈕和CustomControl.cs[C# file for custom control button]創建點擊路由事件,當我改變使用自定義控制的基本元素屬性在MainWidow.cs[C# file for implementing the custom library]文件中定義點擊事件,以便其按鈕點擊時其背景屬性會發生變化。XAML:如何通過路由事件

我面對的問題是「按鈕單擊」其背景屬性應該改變,但它不是。

MainWindow.xaml

<Window x:Class="my_ToolBox.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ravi="clr-namespace:my_ToolBox" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <ravi:Button_Primary Text="Primary" x:Name="Primary_button"></ravi:Button_Primary> 
</Grid></Window> 

MainWindow.cs

namespace my_ToolBox 
{ 

public partial class MainWindow : Window 
{ 

    public MainWindow() 
    { 
     InitializeComponent(); 
     Primary_button.onclick+=Primary_button_onclick; 
    } 



    private static void Primary_button_onclick(object sender, RoutedEventArgs e) 
    { 
     MessageBox.Show("Hello robins"); 
     Button_Primary btn = new Button_Primary(); 
     btn.Background = new SolidColorBrush(Color.fromRgb(0,0,0)); 

    } 
} 
} 

Generic.xaml

<Style TargetType="{x:Type local:Button_Primary}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:Button_Primary}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         > 
        <Button Height="38" Width="86" Background="#428bca" BorderBrush="#428bca" BorderThickness="0" Content="{TemplateBinding Text}" FontSize="14" Foreground="white" FontFamily="Segoe UI" x:Name="Primary_button"> 
        </Button> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

CustomControl.cs

namespace my_ToolBox 
{ 

public class Button_Primary : Control 
{ 
    private Button clickbutton; 

    public static readonly RoutedEvent onclickevent = EventManager.RegisterRoutedEvent("onclick", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(Button_Primary)); 

    static Button_Primary() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(Button_Primary), new FrameworkPropertyMetadata(typeof(Button_Primary))); 
    } 


    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     clickbutton = GetTemplateChild("Primary_button") as Button; 
     if(clickbutton!=null) clickbutton.Click+=clickbutton_Click; 

    } 

    public event RoutedEventHandler onclick 
    { 
     add 
     { 
      AddHandler(onclickevent, value); 
     } 
     remove 
     { 
      RemoveHandler(onclickevent, value); 
     } 
    } 



    private void clickbutton_Click(object sender, RoutedEventArgs e) 
    { 
     RaiseEvent(new RoutedEventArgs(onclickevent)); 
    } 

} 
} 
+0

'Primary_button_onclick'處理程序是否被調用? – Sheridan

+0

@Sheridan是消息框顯示,但背景屬性仍然保持不變。 –

回答

1

啊,我發現你的問題......我很抱歉,但它是一個真正的基本一個太:

private static void Primary_button_onclick(object sender, RoutedEventArgs e) 
{ 
    MessageBox.Show("Hello robins"); 
    Button_Primary btn = new Button_Primary(); 
    btn.Background = new SolidColorBrush(Color.fromRgb(0,0,0)); 
} 

在這裏,你創建一個新的Button_Primary對象,並設置其Background財產而不是你的UI元素Background

Button_Primary btn = new Button_Primary(); 
btn.Background = new SolidColorBrush(Color.fromRgb(0,0,0)); 

所有你需要做的是訪問您的實際上Button_Primary UI中的對象...你怎麼做到這一點?那麼,你可以從MSDN上的How to: Find ControlTemplate-Generated Elements頁面找到完整的故事,但其要點是:你需要一個控制實例,有相關的ControlTemplate適用於它,然後你可以簡單地使用FrameworkTemplate.FindName Method來訪問Button