2011-10-14 31 views
0

我創建了一個包含自定義控件的庫,以簡化我的同事的一些開發。 然後我創建了一個模板,我想讓他們有機會修改默認模板等等。 經過一番研究,我發現了關於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).BaseValueSourceStyle返回的值更改爲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> 

此外,在屬性網格中,模板財產的來源,仍設置爲風格...

我想現在我在看的方式來設置樣式爲隱式或默認或者什麼......(在顧客組裝中定義的那個)或源組件中定義的控件)。

+0

爲了清楚起見,我現在在WpfApp客戶應用程序中使用定義在庫類庫中的簡單CustomControl來簡化解釋並找到解決方案。 – Joseph

回答

0

這是不完全清楚,我想要給你的同事修改默認的外觀部分沒有工作,什麼模板,但這是你需要自定義控制做在WPF什麼:

創建自定義控件類從

System.Windows.Controls.Control 

繼承你有正確的構造部分,所以下一步是建立在同一個項目中的文件夾名稱爲您的自定義控制「主題」,並在那裏加入Generic.xaml 。這就是WPF將尋找默認控制模板的地方。

定義一個實現你的目標類型風格:

<Style TargetType="{x:Type local:Tire}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:Tire}"> 
       <!-- there goes your template: grids/stackpanels, etc --> 

對於您要使用的控制,你有機會重新定義樣式每個項目:

<Style TargetType="{x:Type custom:Tire}"> 
    <Setter Property="Width" Value="150"/> 
    <!-- any customer dependency property can be set here as well--> 

如果你想那些屬性來實際改變你需要在源程序集中使用TemplatedParent來綁定這些屬性,而不是對它們進行硬編碼。

+0

感謝您的支持,但我在這裏尋找的是用隱含的maneer在客戶程序集中定義此默認樣式。這樣,如果有人想在同一個應用程序中定義一個顯式樣式(所以不是實際定義了控件的程序集),它可以在不添加「basedon」指令的情況下進行並保持Template定義(不在源代碼中部件)。 – Joseph

+0

沒錯,只要你有在generic.xaml的源程序集中定義的樣式,它就會適用於客戶程序集。如果客戶想要重寫默認樣式,他們總是可以這樣做。最後一個xaml示例顯示了這一點。 – bc004346

+0

是的,但如果我不想在源程序集中定義任何樣式,那麼在客戶程序集中以隱式方式定義爲默認樣式,以便我們可以在同一個程序集中添加明確覆蓋所有樣式(for例如設置保證金,或尺寸..)? – Joseph

相關問題