我創建了一個包含自定義控件的庫,以簡化我的同事的一些開發。 然後我創建了一個模板,我想讓他們有機會修改默認模板等等。 經過一番研究,我發現了關於Themes,Generic.xaml和ThemeInfo屬性的一些信息,但有些東西不起作用。在裝配體中定義的自定義控件,其中ControlTemplates在另一個裝置中定義
所以我的最後一次嘗試: - 與主題屬性的控制庫的AssemblyInfo.cs:
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //où se trouvent les dictionnaires de ressources spécifiques à un thème
//(utilisé si une ressource est introuvable dans la page,
// ou dictionnaires de ressources de l'application)
ResourceDictionaryLocation.ExternalAssembly //où se trouve le dictionnaire de ressources générique
//(utilisé si une ressource est introuvable dans la page,
// dans l'application ou dans l'un des dictionnaires de ressources spécifiques à un thème)
)
所有控件從System.Windows.Control繼承和具有靜態構造函數:
DefaultStyleKeyProperty.
OverrideMetadata(typeof(Tire), new FrameworkPropertyMetadata(typeof(Tire)));
然後,在應用程序中,在這個庫中引用,我有一個主題用這樣的定義/ Generic.xaml文件:
<Style TargetType="{x:Type g:Tire}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type g:Tire}">
<Viewbox Name="view">
<Ellipse Stroke="Black" StrokeThickness="10" Width="30"
Height="30"/>
</Viewbox>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Present}" Value="true">
<Setter TargetName="view" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding Present}" Value="false">
<Setter TargetName="view" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我也試着設置一個鍵與{x:Type g:Tire}
OU simplement g:Tire
但沒有成功:這些控件沒有VS2010控件模板呈現。
要成功顯示控件,我必須在app.xaml中將generic.xaml文件添加爲ResourceDictionary,而在此第二個程序集中添加ThemeInfo屬性或不添加ThemeInfo屬性。
的其他相關的問題:如果在一個window.xaml文件或其他控制的XAML文件,使用樣式的力量,我補充一下:
<Style TargetType="{x:Type g:Tire}">
<Setter Property="Width" Value="20"/>
</Style>
系統似乎覆蓋通用定義的樣式。 xaml和控件模板不再呈現。爲了檢查我在屬性網格中查看了Template屬性,並且在同一個程序集中定義了自定義控件時,該值的原點被引用爲「Style in Defined」而不是傳統的「Inheritance」。
所以我的問題,有人可以指出我應該看看克服這種情況或幫助我找到正確的方式來準備這樣的設置:在類庫中定義控件,並使用默認樣式機制,定義在wpf應用程序中的generic.xaml文件中控制模板?
在此先感謝!
更新:
我在幾件事情看,它是一個比較明確的:因爲我不打算在一般的方式來使用主題化,我必須以某種方式來設定樣式系統使用客戶庫中定義的資源作爲「通用」字典。
通過添加ThemeInfo(...無,...無),並將客戶xaml文件添加爲客戶程序集中的資源,我設法將DependencyPropertyHelper.GetValueSource(customControl1, Library.CustomControl.TemplateProperty).BaseValueSource
從Style
返回的值更改爲Default
,但如果應用程序中定義了樣式,則模板仍會被覆蓋:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:Library;assembly=Library">
<Grid>
<Grid.Resources>
<Style TargetType="my:CustomControl"></Style>
</Grid.Resources>
<my:CustomControl HorizontalAlignment="Left" Margin="128,95,0,0" Name="customControl1" VerticalAlignment="Top" Height="157" Width="253" />
</Grid>
此外,在屬性網格中,模板財產的來源,仍設置爲風格...
我想現在我在看的方式來設置樣式爲隱式或默認或者什麼......(在顧客組裝中定義的那個)或源組件中定義的控件)。
爲了清楚起見,我現在在WpfApp客戶應用程序中使用定義在庫類庫中的簡單CustomControl來簡化解釋並找到解決方案。 – Joseph