简体   繁体   English

xaml:如何通过路由事件使用自定义控件来更改基本元素属性

[英]xaml: How to change basic element properties using custom control through routed events

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. 我在Generic.xaml文件中创建了自定义控件按钮,并在CustomControl.cs [C# file for custom control button]创建了单击路由事件 ,当我在MainWidow.cs [C# file for implementing the custom library]文件,以便其背景属性将在单击按钮时更改。

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对象并设置 Background属性, 而不是UI元素的Background

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? 您需要做的就是从UI访问实际的 Button_Primary对象。 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 . 好的,您可以从MSDN上的“ 如何:查找ControlTemplate生成的元素”页面中找到完整的故事,但是要点是:您需要一个具有相关ControlTemplate实例,然后您可以简单地使用FrameworkTemplate.FindName方法访问Button

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在XAML模板中使用WPF自定义控件属性? - How to use WPF custom control properties in XAML Template? 如何进行事件以更改定制属性(定制控件) - How to make events for changing custom made properties (custom control) 从样式XAML设置自定义控件属性 - Setting custom control properties from Style XAML 如何从用户控件中引发自定义路由事件? - How can I raise a custom Routed Event from user control? 从处理路由事件停止控制 - Stopping a Control from handling routed events WPF未从用户控件接收路由事件 - WPF Not receiving routed events from user control 如何正确扩展具有新属性的XAML控件? - How to extend a XAML control with new properties properly? 如何从 Generic.xaml 中定义的资源字典绑定到自定义控件的依赖属性? - How can I bind to dependency properties of a custom control from a resource dictionary defined in Generic.xaml? 如何仅使用 XAML 更改资源模板元素的属性? - How to change resource template element's property using only XAML? 自定义控件的属性只能由 StaticResource 填充,而不能由 XAML 中定义的值填充 - Properties of custom control can only be filled by StaticResource but not with values defined in XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM