2010-08-12 104 views
0

我的問題摘要:我創建了一個UserControl,將我的依賴項屬性「Scheme」綁定到GradientStop的Color屬性,並且工作完美。現在,在將我的UserControl轉換爲自定義控件後,此綁定不再起作用,我不知道爲什麼。爲什麼我不能再將GradientStop Color綁定到我的控件的依賴項屬性?

編輯: 複製自定義控件問題:http://www.megaupload.com/?d=0006XVYD

編輯: 用戶等效控制,其中綁定的工作:http://www.megaupload.com/?d=W13GTD4E

這是我宣佈我的資源字典我在UserControl1.xaml用戶控件文件。

在UserControl1.xaml

<!-- Resources --> 
<UserControl.Resources> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="Colors.xaml"/> 
    <ResourceDictionary Source="Styles.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 

在Colors.xaml,我有以下幾點:

在Colors.xaml

<GradientStopCollection x:Key="colorSchemeGradient"> 
<GradientStop Color="{Binding Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Offset="0"/> 
<GradientStop Color="#000000" Offset="3"/> 
</GradientStopCollection> 

<LinearGradientBrush x:Key="colorBrush" StartPoint="0,0" EndPoint="0,1" GradientStops="{DynamicResource colorSchemeGradient}"/> 

依賴屬性計劃的結合完美的GradientStop WORKS的顏色屬性,完全如我所願。 LinearGradientBrush使用Scheme顏色創建一個從方案的顏色漸變爲稍暗的陰影的漸變。 UserControl中有一個使用colorBrush作爲背景的矩形。這一切都很完美,我喜歡它。

現在,情況正在使我將此UserControl轉換爲CustomControl。我通過獲取UserControl的XAML文件並將其所有內容複製到控件模板中的Generic.xaml中來完成此操作。嘗試不同的東西出來後,這似乎工作得很好在Generic.xaml宣佈我的資源字典:

在Generic.xaml現在

<Style TargetType="{x:Type local:MyCustomControl}"> 

    <!-- Style Resources --> 
    <Style.Resources> 

    <!-- Resource Dictionaries --> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/MyCustomControl;component/themes/Colors.xaml"/> 
     <ResourceDictionary Source="/MyCustomControl;component/themes/Styles.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 

    </Style.Resources> 

      . 
      . 
      . 
</Style> 

,在我Colors.xaml文件,我具備以下條件:

在Colors.xaml爲我的自定義控件

<GradientStopCollection x:Key="colorSchemeGradient"> 
<GradientStop Color="{Binding Path=Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType=l:MyCustomControl}}" Offset="0"/> 
<GradientStop Color="#000000" Offset="3"/> 
</GradientStopCollection> 


<!-- FOR DEBUG PURPOSES --> 
<TextBlock x:Key="whoaText" Text="{Binding Path=Title, RelativeSource={RelativeSource FindAncestor, AncestorType=l:MyCustomControl}}"/> 

<LinearGradientBrush x:Key="colorBrush" StartPoint="0,0" EndPoint="0,1" GradientStops="{DynamicResource colorSchemeGradient}"/> 

現在,對於我無法理解的一些非常令人沮喪的理由,綁定到GradientStop不再起作用。我如何組織我的資源沒有任何問題,因爲我製作的調試文本塊完全綁定到Title依賴項屬性。 GradientStop顏色不綁定到Scheme依賴項屬性,這讓我很煩。

任何人都可以告訴我發生了什麼事以及如何修復它?

編輯:我複製了我的問題在測試項目:http://www.megaupload.com/?d=0006XVYD

在Dictionary1.xaml看與x的文本塊:鍵「文本2」。 colorBrush使用來自colorSchemeGradient的漸變色,它使用對Scheme的綁定。但是那個綁定失敗了,所以沒有任何作用如果你可以讓綁定工作,你真棒。

編輯:下面是一個等價的用戶控制,其中結合的作品: http://www.megaupload.com/?d=W13GTD4E

爲什麼中自定義控件這裏,不具約束力的工作?

非常感謝你,

達拉爾

+0

您可以添加一些代碼,比如您的CustomControl的XAML,以便我們可以清楚地看到綁定失敗的位置。 {enjoy} – 2010-08-13 00:14:36

+0

我編輯了我的帖子以包含指向綁定不起作用的自定義控件的測試項目的鏈接,並且還包含了一個指向綁定確實工作的測試用戶控件的鏈接。這裏發生了什麼?希望你有機會檢查出來。 – Dalal 2010-08-13 01:07:02

回答

3

在您的測試項目在http://megaupload.com/?d=0006XVYD,你似乎有你需要的靜態DynamicResources,並在不需要向前引用。

在Dictionary1.xaml,改變內容:

<!-- Color Scheme Gradient: Used in the label background and border brush. 
           SUBTLE gradient from the colorScheme color 
           to a slightly darker shade --> 
    <GradientStopCollection x:Key="colorSchemeGradient"> 
     <GradientStop Color="{Binding Path=Scheme, RelativeSource={RelativeSource FindAncestor, AncestorType=local:CustomControl1}}" Offset="0"/> 
     <GradientStop Color="#000000" Offset="3"/> 
    </GradientStopCollection> 

    <LinearGradientBrush x:Key="colorBrush" GradientStops="{StaticResource colorSchemeGradient}"/> 

    <TextBlock x:Key="text1" Text="{Binding Path=Name, RelativeSource={RelativeSource FindAncestor, AncestorType=local:CustomControl1}}" Foreground="Blue"/> 
    <TextBlock x:Key="text2" Text="This text doesn't show! Grr..." Foreground="{StaticResource colorBrush}"/> 

我也改變了Generic.xaml的文本1和文本引用。

請注意StaticResource而不是DynamicResource在幾個地方,以及刪除前向引用的重新排序。如果您閱讀http://msdn.microsoft.com/en-us/library/ms750613.aspx,則可能會注意到動態資源查找行爲與靜態行爲有很大不同。

請注意,切換到靜態不會保持控件不響應對Scheme屬性的更改 - 資源的查找是靜態的,而不是這些資源中屬性的值。

+0

我將所有DynamicResources都更改爲StaticResources,現在可以使用了,謝謝。 – Dalal 2010-08-13 16:54:53

相關問題