繁体   English   中英

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

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

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

我面临的问题是“单击按钮时”其背景属性应该更改,但事实并非如此。

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

}
}

啊,我发现了您的问题...很抱歉,但这也是一个真正的基本问题:

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM