Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 运行时复制控制按钮_C#_Wpf - Fatal编程技术网

C# 运行时复制控制按钮

C# 运行时复制控制按钮,c#,wpf,C#,Wpf,大家好。我用一些(WPF)表单创建了一个应用程序。我在一张表格上有一个按钮。我想在运行时将此按钮复制到第二个窗体上,而无需再次创建它 例如:我在表格1上有“X”按钮。在运行时,我在表单2中创建了该按钮的副本,其属性值与表单1中的原始按钮相同 按钮: <Button Content="Open Window" Click="ButtonClicked" Height="25" HorizontalAlignment="Left" Margin="379,264,0,0" Name="butt

大家好。我用一些(WPF)表单创建了一个应用程序。我在一张表格上有一个按钮。我想在运行时将此按钮复制到第二个窗体上,而无需再次创建它

例如:我在表格1上有“X”按钮。在运行时,我在表单2中创建了该按钮的副本,其属性值与表单1中的原始按钮相同

按钮:

<Button Content="Open Window" Click="ButtonClicked" Height="25"
HorizontalAlignment="Left" Margin="379,264,0,0" Name="button1" 
VerticalAlignment="Top" Width="100" />


我是否有可能避免复制此代码?

您可以在
App.xaml
中为按钮定义自己的样式:

<Application x:Class="WpfButton.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style x:Key="myBtnStyle" TargetType="Button" >
            <Setter Property="Content" Value="Open Window" />
            <Setter Property="Height" Value="25" />
            <Setter Property="Width" Value="100" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="Margin" Value="379,264,0,0" />
            <Setter Property="VerticalAlignment" Value="Top" />
            <EventSetter Event="Click" Handler="myBtn_Click" />
        </Style>
    </Application.Resources>
</Application>
当您要将创建的早期样式指定给按钮时,应使用
StaticResource
和样式名称:

 <Button Style="{StaticResource myBtnStyle}" />


如果要创建精确的克隆,则需要序列化组件并将ExpressionConverter注入序列化过程。这将创建一个“深层”克隆。请参阅。

首先告诉我们您真正使用的是什么,wpf或Winforms我在我的应用程序中使用wpf我删除了Winforms,因为那时没有使用它。对于此错误,非常抱歉,谢谢。您为什么要复制此按钮?您能告诉我们原因吗
 <Button Style="{StaticResource myBtnStyle}" />