Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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_Xaml_Resources_Window - Fatal编程技术网

C# 将定义为资源的对象放置在可视化树中

C# 将定义为资源的对象放置在可视化树中,c#,wpf,xaml,resources,window,C#,Wpf,Xaml,Resources,Window,我定义了一个资源,其中包含一个按钮,如下面的代码 <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="313" Width=

我定义了一个资源,其中包含一个按钮,如下面的代码

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="313" Width="481">

    <Window.Resources>
        <Button x:Key="btnMy">my button</Button>
    </Window.Resources>

    <!--And now, how can I place 'btnMy' into here?-->
</Window>

我的纽扣
我喜欢通过XAML编码将控件放入Window1

请帮帮我。


<StaticResource ResourceKey="btnMy"/>
如果你在不止一个地方使用它,你会得到一些很好的例外

编辑:有些人可能感兴趣的是,可以通过在相关资源上设置为false来避免这些异常,这将导致在引用该资源的任何位置创建新的控件。


如果你在不止一个地方使用它,你会得到一些很好的例外


编辑:有些人可能感兴趣的是,可以通过在相关资源上设置为false来避免这些异常,这将导致在引用控件的任何位置创建新控件。

为什么要在资源中放置按钮?使用控件的方式很糟糕…我只想知道如何将资源放置到窗口。为什么要在资源中放置按钮?使用控件的方式很糟糕…我只想知道如何将资源放置到窗口。