2015-01-13 48 views
0

因此,我在Visual Studio中創建了一個UI,並且由於所有重複的元素,我在參考資料部分中使用了很多元素我的用戶控件。我正在嘗試使用文件中的另一個StaticResource作爲項目的模板迭代項目。XAML和Caliburn.Micro MVVM:引用數據模板中另一個樣式資源的樣式資源

問題是Datatemplate當然是將DataContext切換到它迭代的項目。所以我試圖找出如何獲取靜態資源的上下文,這是在UserControl的上下文中。我考慮使用相對採購,但後來意識到StaticResource沒有該選項,只能讓您訪問上下文的資源。

有什麼建議在這裏做什麼?

<Style TargetType="ItemsControl" x:Key="BeneficiaryList"> 
     <Setter Property="ItemTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <Expander Header="{Binding Name_1}"> 
         <Expander.Resources> 
          <utils:BindingProxy x:Key="proxy" Data="{Binding}"/> 
         </Expander.Resources> 
         <!--Attempt at a proxy binding to access another style resource in the file. Does not work.--> 
         <ContentControl Style="{StaticResource proxy.Person_Template}"/> 
        </Expander> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

謝謝!

回答

0

你只需給你的UserControl一個名字,例如「root」,然後綁定使用ElementName

<ContentControl Style="{Binding ElementName=root,Path=DataContext.WhateverProperty}" /> 
+0

還有一個問題,我怎麼才能到'Style'元素在''元素?就像使用'Path = DataContext.Resources.Person_Template'一樣簡單嗎?或者我需要使用不同的訪問路徑? – Saxophlutist

+0

編輯:我想通了。它應該是'Path = Resources [Person_Template]' – Saxophlutist

+0

你可以嘗試使用{StaticResource Person_Template}而不是綁定嗎? – Frank