4

我在我的項目中使用NuGet的Extended WPF Toolkit Community Edition v2.6,但我不知道是否還有更多我應該做的讓我主題或自定義控件模板。我如何使用混合或VS設計器來編輯WPF PropertyGrid的默認樣式模板

在要求Designer/Blend爲PropertyGrid控件創建現有默認模板的副本後,UI已損壞。該模板看起來正確,但不再在設計時或運行時起作用。

enter image description here

選擇爲默認模板複製到一個新的樣式後:

Image http://i63.tinypic.com/2pt59oi.png

有一種簡單的方法來編輯此控件的內置樣式?我真的只是試圖覆蓋PropertyGrid的Editors/Labels的前景/背景顏色。

我試着與有限的成功的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> 

當試圖創建一個屬性Container風格,通過複製默認情況下,我得到「複製樣式失敗。」來自VS Designer或Blend中的錯誤。

enter image description here

結果在此:

Error from Copy Style

我已經手動嘗試包括從Xceed工具包組件的generic.xaml但它並沒有解決了這一問題。

我想引用資源的方式有兩種:

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

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

這裏是從設計師的堆棧跟蹤,當我嘗試設置PropertyContainerStyle:

stack trace

+0

你嘗試過的樣式有什麼問題(「我嘗試過在XAML中進行一些有限的成功手動戳)」。它沒有工作嗎?它覆蓋了其他東西嗎? – FriendlyGuy

+0

上面的屏幕截圖顯示了我嘗試創建樣式後發生的情況。整個控件摺疊爲一個條,就好像Items集合是空的。我從那以後發現我應該手動包含generic.xaml,但它沒有幫助我的情況。我仍然無法設計這個PropertyGrid。 –

+0

你使用什麼版本的VS?在帶有Update 1的* VS Professional 2015中*創建默認模板的副本,可以在不中斷用戶界面的情況下爲您描述的兩種方式(無論是「編輯模板」還是「編輯其他模板」)都可用。我使用的是相同的工具包v2.6,但沒有nuget(只是下載和引用的庫)。但是請注意 - 對於這個測試,我只是創建了空的'PropertyGrid'。如果需要,我可以共享生成的模板。 – Sam

回答

0

,以獲得完整PropertyGrid的控制模板直接從Xceed.Wpf.Toolkit.dll中,您可以嘗試在任何WPF應用程序中使用此代碼(注意,您需要有<Grid x:Name="RootGrid" />在你的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()); 

得到控制模板XAML與sb.ToString()你可以複製/粘貼使用您的PropertyGrid和編輯任何您需要的顏色後。

0

Blend中的「編輯副本」選項並不總是準確的。由於未複製ItemsSource,因此在使用複製的模板時無法在PropertyGrid中看到PropertyItem。

查看「PropertyGrid」風格中的「PART_PropertyItemsControl」:沒有ItemsSource。將其設置爲: ItemsSource =「{Binding Properties,RelativeSource = {RelativeSource TemplatedParent}}」 您將看到PropertyItems。