111,125
社区成员
发帖
与我相关
我的任务
分享
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF2">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Height" Value="250"/>
<Setter Property="Width" Value="250"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Button x:Name="button" Content="fgffgfg" Height="20" Background="Red"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
public class CustomControl1 : Control
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
button = this.GetTemplateChild("button") as Button;
aaa();
}
Button button;
public void aaa()
{
Button button2 = DeepCopy<Button>(this.button);
}
static T DeepCopy<T>(T obj)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
XamlWriter.Save(obj, stream);
stream.Seek(0, System.IO.SeekOrigin.Begin);
return (T)XamlReader.Load(stream);
}
}
<Window x:Class="WPF2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF2"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:CustomControl1/>
</Grid>
</Window>