2012-09-11 31 views
2

我的要求是,在一個文本框應用多個樣式有以下情況:結合DynamicResource來支持算法FMP風格

  1. 我有一個風格(例如MyTextStyle)在另一個文件中說,「Generic.xaml
  2. 我的文本框是ABC.xaml
  3. 我想一些觸發器適用於這個文本框,所以我不得不使用Textbox.Style
  4. 我人所以想申請「MyTextStyle

當初我跟隨它給了我,我不能適用DynamicResource來支持算法FMP錯誤:

<TextBox.Style> 
        <Style BasedOn="{DynamicResource MyTextStyle}" TargetType="{x:Type TextBox}"> 
         <Setter Property="Text" Value="{Binding SelectedCall.Name}" /> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding SelectedCall.Name}" Value="N/A"> 
           <Setter Property="Text" Value="" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBox.Style> 

請建議我一些解決方案,使我能夠把這個Dynamicresource還有我datatrigger風格

+0

爲什麼你想在BasedOn中使用DynamicResource而不是StaticResource? – Alexander

+0

,因爲我的解決方案中的幾乎所有文本框都使用了樣式「MyTextStyle」。所以如果我爲此文本框的目的而專門定義(和本地),這將是多餘的。 – deathrace

回答

8

變化DynamicResourceStaticResource這樣的:

<Style BasedOn="{StaticResource MyTextStyle}" TargetType="{x:Type TextBox}"> 

在BasedOn中有意不允許使用DynamicResource。

編輯: 你得到「無法找到指定的資源‘EmptyTextBoxStyle’」,因爲應用程序找不到這個特殊的靜態資源。爲了幫助應用程序找到它,你需要使用MergedDictionary。這裏是如何使用它的例子,例如窗口:

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Generic.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

內的另一個ResourceDictionary中,你應該使用如下所示:

<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="/Generic.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

您需要引用ResourceDictionary中包含了這樣EmptyTextBoxStyle樣式定義。例如,如果在Generic.xaml文件中聲明'EmptyTextBoxStyle',並且您在ABC.xaml中使用它,則可以使用上述XAML(當然,您需要根據項目結構更新Source屬性)。

+0

我也試過。但它會引發運行時異常。抱歉,現在我無法向您展示我得到的例外情況。將再次嘗試,並明天通知你。 – deathrace

+0

發生以下異常: 「組合產生了單個組合錯誤,其根本原因如下:查看CompositionException.Errors屬性以獲取更多詳細信息 1)無法找到名爲'EmptyTextBoxStyle'的資源資源名稱區分大小寫」。 和名稱是否正確(即'EmptyTextBoxStyle'拼寫正確) – deathrace

+0

請參閱我的更新 – Alexander