【问题标题】:WPF Reference control in resourcedictionary资源字典中的 WPF 引用控件
【发布时间】:2014-06-05 19:28:02
【问题描述】:

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

这是资源字典:

<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文件中的窗口。

【问题讨论】:

    标签: wpf resourcedictionary


    【解决方案1】:

    您当然可以在另一个控件中托管类似UserControl 的内容。

    例如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 中可能/可能没有感兴趣的内容。

    【讨论】:

    • 啊,很高兴它有帮助,如果您尝试将其与另一个控件嵌套,Window 将会崩溃,但如果您需要的内容可以放入 UserControl 中,则一切正常。顺便说一句,这不是在项目的其他地方使用Control 的最直接方法。通常你会添加一个新的命名空间,并直接在 XAML 中引用有问题的 Control,例如:msdn.microsoft.com/en-gb/library/bb514546%28v=vs.90%29.aspx 我建议使用这种方法,因为它似乎最接近你想要的。祝你好运=D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多