resourcedictionary中的WPF引用控件

时间:2014-06-05 19:28:02

标签: wpf resourcedictionary

我创建了我的资源字典,可以访问其中的样式,但是如何(如果可能)从中引用整个控件?

这是resourcedictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="MainWindowStyle" TargetType="Window">
        <Setter Property="Background" Value="Transparent"></Setter> 
        <!--Can access this-->
    </Style>

    <Window x:Key="CustomWindow" Background="Transparent">
        <!--Want to access this-->
    </Window>

</ResourceDictionary>

这是主窗口xaml文件:

<Window x:Class="MyNamespace.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="640" Width="960" >
        <!--setting Style={StaticResource MainWindowStyle} adds the style but I want this window to be replaced by the one in the resourcedictionary-->
    <Grid>

    </Grid>
</Window>

所以我基本上想用xaml文件中的窗口替换资源字典中创建的窗口。

1 个答案:

答案 0 :(得分:0)

你当然可以在另一个控件中托管像UserControl这样的东西。

E.g。一个ContentControl

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Window x:Key="CustomUserControl" Background="Transparent">
    <!--Want to access this-->
</Window>

</ResourceDictionary>

然后:

<ContentControl Content="{StaticResource ResourceKey=CustomUserControl}" />

我怀疑这不是你所追求的,但不清楚你感兴趣的Window中你可能/可能没有的东西。