I have created a custom control button in Generic.xaml file and created a click routed event in CustomControl.cs [C# file for custom control button]
,When I am defining a click events in MainWidow.cs [C# file for implementing the custom library]
file so that its background property will change on button click.
Problem i am facing is that "On Button click" its background property should change but it is not.
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));
}
}
}
Ah, I found your problem... I'm sorry, but it's a real basic one too:
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));
}
Here, you create a new Button_Primary
object and set its Background
property and not the Background
of your UI element :
Button_Primary btn = new Button_Primary();
btn.Background = new SolidColorBrush(Color.fromRgb(0,0,0));
All you need to do is to access your actual Button_Primary
object from the UI... how do you do that? Well, you can find the full story from the How to: Find ControlTemplate-Generated Elements page on MSDN, but the gist of it is this: You need an instance of a control that has the relevant ControlTemplate
applied to it and then you can simply use the FrameworkTemplate.FindName
Method to access the Button
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.