2010-08-12 4 views
0

내 문제 요약 : GradientStop의 Color 속성에 "Scheme"이라는 종속성 속성을 바인딩 한 UserControl을 만들었고 완벽하게 작동했습니다. 이제, 내 UserControl을 사용자 지정 컨트롤로 변환 한 후에이 바인딩이 더 이상 작동하지 않으며 그 이유를 알 수 없습니다.GradientStop Color를 내 컨트롤의 Dependency 속성에 더 이상 바인딩 할 수없는 이유는 무엇입니까?

편집 : 사용자 지정 컨트롤 문제의 복제 : http://www.megaupload.com/?d=0006XVYD

편집 : 작품을 바인딩 상응하는 사용자 제어 : http://www.megaupload.com/?d=W13GTD4E

이 내가 UserControl1.xaml 내 UserControl을 내 리소스 사전을 선언하는 방법이다 파일.

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}"/> 

종속성 재산 제도의가 바인딩에서 : Colors.xaml 내

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

UserControl1.xaml에서

, 나는 다음과 같은이 GradientStop의 color 속성은 완벽하게 작동합니다. 정확히 내가 원하는 방식입니다. 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와 TextBlock을 보면 : 키 "텍스트 2"를. colorBrush는 scheme에 대한 바인딩을 사용하는 colorSchemeGradient의 그래디언트 스톱을 사용합니다. 그러나 그 바인딩은 실패하고 아무것도 작동하지 않습니다. 당신이 그 바인딩을 작동시킬 수 있다면, 당신은 최고입니다.

편집 : 여기에 해당 사용자 컨트롤이 어디 바인딩 작품 : http://www.megaupload.com/?d=W13GTD4E

이유는 무엇입니까 사용자 지정 컨트롤에 여기가 아닌 작업을 바인딩?

당신은 당신이 정적 필요 DynamicResources, 필요에 전달하지 참조를 갖고있는 것 같다, http://megaupload.com/?d=0006XVYD에서 테스트 프로젝트에서, 당신에게

달랄

+0

사용자 지정 컨트롤의 XAML과 같은 코드를 추가하여 바인딩이 실패한 위치를 명확하게 확인할 수 있습니까? {enjoy} –

+0

바인딩이 작동하지 않는 사용자 지정 컨트롤에 대한 테스트 프로젝트에 대한 링크를 포함하도록 내 게시물을 편집했으며 바인딩이 작동하는 테스트 사용자 컨트롤에 대한 링크도 포함했습니다. 무슨 일 이니? 확인해 볼 수 있기를 바랍니다. – Dalal

답변

3

를 대단히 감사합니다.

<!-- 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과 텍스트 2 참조를 변경 : Dictionary1.xaml에서

는에 내용을 변경할 수 있습니다.

앞으로 몇 자리에서 DynamicResource이 아닌 StaticResource과 앞으로 참조를 제거하는 순서를주의하십시오. http://msdn.microsoft.com/en-us/library/ms750613.aspx을 읽으면 dynamicresource 조회 동작이 정적이 아닌 다른 좋은 결과임을 알 수 있습니다.

정적으로 전환해도 컨트롤이 Scheme 속성의 변경에 응답하지 않으므로 리소스 의 조회는 해당 리소스의 속성 값이 아니라 정적입니다.

+0

모든 DynamicResources를 StaticResources로 변경 했으므로 이제는 작동합니다. – Dalal

관련 문제