[英]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.