【问题标题】:MahApps and Property GridMahApps 和属性网格
【发布时间】:2017-09-03 11:37:13
【问题描述】:

首先,非常感谢 MahApps。多么酷的项目!

我有一个用 WPF 编写的现有应用程序,我已经应用了 MahApps 库。我使用了这个教程:

http://mahapps.com/guides/quick-start.html

但是对属性网格 (Xceed) 的影响很小。

我的其他窗口中的组合框如下所示:

属性网格组合框仍然看起来像这样(丑陋!):

但是,单击组合框会显示项目的正确 MahApps 样式。只有 Combo Box 本身(关闭)不平整。

我对 WPF 的了解是基本的。我从哪里开始尝试解决这个问题?是否需要手动覆盖属性网格中的组合框模板?

【问题讨论】:

  • 您可以尝试其他属性网格:github.com/SoftFluent/SoftFluent.Windows(免责声明:我写的,它是免费和开源的)也许它们与 mahapps 集成而无需太多努力。
  • 所以丑陋的东西有点主观,但一个快速的建议是,mahapps 可以与 Material design 相结合,创造一些非常棒的开箱即用的用户体验(包括网格):materialdesigninxaml.net

标签: c# wpf mahapps.metro


【解决方案1】:

在 MainWindow.xaml 中使用 Controls:MetroWindow

<Controls:MetroWindow x:Name="MainApp" x:Class="AppWin.MainWindow"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                      MinHeight="700"
                      MinWidth="1024"
                      >

在MainWindow.xaml.cs继承MetroWindow

namespace AppWin
{
    public partial class MainWindow : MetroWindow
    {
  ...

添加App.xaml以下设置

    <Application x:Class="AppWin.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:AppWin"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />

/*--change template color for example green.xaml--*/
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/red.xaml" />

/*--change template style for example BaseDark.xaml--*/
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />

                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

【讨论】:

  • 这是使 MahApps 工作的基本代码。我确实在我的代码中实现了这一点。我的问题不是 MahApps 不工作。 MahApps 没有对属性网格内的组合框进行样式化
  • 如果您已正确定义 MahApps.Metro ,您应该在此属性的组合框中看到它。 ibb.co/mZFZ1v
  • OP 描述的问题是一个合法的故障。即使按照HERE 所述的使用DataTemplate 创建自定义编辑器的方法,仍然会出现同样的问题。
  • jstreet,谢谢。如果这是一个小故障,那么我想我无能为力
【解决方案2】:

也许您的其他组合框看起来很难看,因为找不到 MahApps 资源?

将您正在使用的 mahapp 资源放在 App.xaml 文件的资源字典中,以便所有窗口都可以访问它。 (而不是将它们放在仅一个窗口的资源字典中,即 mainwindow.xaml)

App.xaml:

<Application... >
<Application.Resources>
    <ResourceDictionary>
         <!-- My other resources -->
         <!-- ... -->

        <!-- MahApps resources -->
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

【讨论】:

  • 我已经更新了这个问题。这不是我的问题。当组合框打开时,您可以在新图片中看到正确的样式。问题是当组合框关闭时。组合框需要平整
  • 哦,我明白了。您是否定义了自己的
  • 我尝试过覆盖 Xceed 枚举组合框,但没有成功。我对 WPF 还很陌生,所以不确定我是否做得对
  • 重点是你不应该覆盖它,以便应用组合框控件的 MahApp 的 ControlTemplate(样式)。
  • 或许您可以提供一个小项目来突出您的问题?
【解决方案3】:

属性网格会覆盖 MahApp 的样式。您必须为您的属性创建一个自己的 PropertyEditor。 PropertyEditor 会覆盖属性网格的样式。

我知道工作量很大,但这是获得 MahApp 外观的唯一方法。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2011-01-22
    • 1970-01-01
    • 2013-04-06
    • 2010-12-12
    • 2011-05-21
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多