【问题标题】:How can I use Blend or VS Designer to Edit Default Style Template for WPF PropertyGrid如何使用 Blend 或 VS Designer 编辑 WPF PropertyGrid 的默认样式模板
【发布时间】:2016-07-05 17:34:50
【问题描述】:

我在我的项目中使用来自 NuGet 的 Extended WPF Toolkit Community Edition v2.6,但我不知道我是否应该做更多的事情来允许我设置主题或自定义控件模板。

在要求 Designer/Blend 为 PropertyGrid 控件创建现有默认模板的副本后,UI 已损坏。模板看起来正确,但在设计时或运行时不再起作用。

选择将默认模板复制到一个新的样式后:

是否有一种简单的方法可以编辑此控件的内置样式?我真的只是想覆盖 PropertyGrid 的编辑器/标签的前景色/背景色。

我在 XAML 中尝试了一些手动戳,但成功有限:

<Style TargetType="{x:Type xctk:DropDownButton}">
  <Setter Property="Background" Value="Black"/>
  <Setter Property="Foreground" Value="White"/>
</Style>
<Style TargetType="{x:Type xctk:CustomPropertyItem}">
  <Setter Property="Background" Value="Black"/>
  <Setter Property="Foreground" Value="White"/>
</Style>
<Style TargetType="{x:Type xctk:PropertyGridEditorCollectionControl}">
  <Setter Property="Background" Value="Black"/>
  <Setter Property="Foreground" Value="White"/>
</Style>

当尝试创建属性容器样式时,通过复制默认值,我得到“复制样式失败”。 VS Designer 或 Blend 中的错误。

结果如下:

我已尝试手动包含 Xceed Toolkit 程序集中的 generic.xaml,但仍未解决问题。

我尝试了两种引用资源的方式:

<ResourceDictionary Source="/Xceed.Wpf.Toolkit;component/themes/generic.xaml" />

<ResourceDictionary Source="pack://application:,,,/Xceed.Wpf.Toolkit;component/Themes/generic.xaml">

这是我尝试设置 PropertyContainerStyle 时来自 Designer 的堆栈跟踪:

【问题讨论】:

  • 您尝试的样式有什么问题(“我已经尝试在 XAML 中进行一些手动戳,但成功有限”)。它没有工作吗?它是否覆盖了其他内容?
  • 上面的截图显示了我尝试创建样式后发生的情况。整个控件折叠成一个条形,就好像 Items 集合是空的。从那以后,我发现我应该手动包含 generic.xaml,但这对我的情况没有帮助。我仍然无法设置此 PropertyGrid 的样式。
  • 你用的是什么版本的VS?在具有更新 1 的 VS Professional 2015 中,创建默认模板的副本适用于您描述的两种方式(使用“编辑模板”和“编辑附加模板”),而不会破坏 UI。我使用的是相同的工具包 v2.6,但没有 nuget(只是下载和引用的库)。但请注意 - 对于这个测试,我刚刚创建了空的 PropertyGrid。如果您需要,我可以分享生成的模板。

标签: wpf propertygrid expression-blend-4 wpf-extended-toolkit


【解决方案1】:

Blend 中的“编辑副本”选项并不总是准确的。使用复制的模板时,您在 PropertyGrid 中看不到 PropertyItems,因为没有复制 ItemsSource。

查看针对“PropertyGrid”的样式中的“PART_PropertyItemsControl”:没有 ItemsSource。将其设置为: ItemsSource="{绑定属性,RelativeSource={RelativeSource TemplatedParent}}" 你会看到PropertyItems。

【讨论】:

    【解决方案2】:

    要直接从 Xceed.Wpf.Toolkit.dll 获取完整的 PropertyGrid 的控制模板,您可以尝试在任何 WPF 应用程序中使用此代码(注意您需要有 @987654322 @ 在你的 xaml 中的任何地方,这个例子都可以工作):

            var type = Assembly.LoadFile(@"C:\path\to\file\Xceed.Wpf.Toolkit.dll")
                .GetType("Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid");
    
            // Instantiate the type.
            var info = type.GetConstructor(Type.EmptyTypes);
            var control = (Control)info.Invoke(null);
    
            // Add it to the grid (but keep it hidden).
            control.Visibility = Visibility.Collapsed;
            this.RootGrid.Children.Add(control);
    
            // Get the template.
            var template = control.Template;
    
            // Get the XAML for the template.
            var settings = new XmlWriterSettings();
            settings.Indent = true;
            var sb = new StringBuilder();
            var writer = XmlWriter.Create(sb, settings);
            XamlWriter.Save(template, writer);
    
            // Display the template any appropriate way.
            Trace.Write(sb.ToString());
    

    使用sb.ToString() 获得控制模板xaml 后,您可以复制/粘贴它以在您的PropertyGrid 中使用并编辑您需要的任何颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      相关资源
      最近更新 更多