【问题标题】:WPF's application wide styles in Windows Forms contextWindows 窗体上下文中 WPF 的应用程序范围样式
【发布时间】:2012-06-09 18:41:11
【问题描述】:

我们从 WinForms 迁移到 WPF...慢慢地 =)

不,我们将 WPF 用户控件与 ElementHost 一起使用。

是否可以在这种情况下定义应用程序范围的资源?在纯 WPF Application.Resources 中代表它。但是与WinForms集成时没有WPF App。

【问题讨论】:

    标签: c# wpf winforms


    【解决方案1】:

    即使您的项目是带有几个单独 WPF 表单或控件的 WinForms 项目,您也可以使用 WPF 应用程序对象。该对象不会为您预先创建,但如果您手动创建它,只需通过new App()(甚至没有派生类,new System.Windows.Application()),您项目中的所有内容都会看到它。

    【讨论】:

      【解决方案2】:

      您可以创建一个通用的ResourceDictionary 并将其添加到您的用户控件的资源中。这样,您只需在一个位置更改样式。

      Dictionary1.xaml

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

      并使用 MergedDictionarys 将其添加到您的 UserControl 中

      <UserControl.Resources>
          <ResourceDictionary>
              <ResourceDictionary.MergedDictionaries>
                  <ResourceDictionary Source="Dictionary1.xaml" />
              </ResourceDictionary.MergedDictionaries>
          </ResourceDictionary>
      </UserControl.Resources>
      

      或者像这样将它添加到控件的资源中

      <UserControl.Resources>
          <ResourceDictionary Source="Dictionary1.xaml"/>
      </UserControl.Resources>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-04
        • 1970-01-01
        • 2011-02-19
        • 2012-06-06
        • 1970-01-01
        • 2018-02-24
        相关资源
        最近更新 更多