2012-10-15 3 views
1

이 이상한 현상을 발견했을 때 UserControl을 만들고있었습니다. C# 코드를 사용하여 GroupBox를 UserControl의 템플릿에 배치 한 다음 GroupBox에서 SetResourceReference를 호출하면 GroupBox가 갑자기 TemplateParent (UserControl)의 전경을 상속합니다. 내가이 상황에 대한 다음과 같은 요구 사항을 발견 지금까지WPF : GroupBox.SetResourceReference가 갑자기 TemplateParent.Foreground를 상속받습니다.

:

  • 의 UserControl의 기본 형식은
  • 영향을받는 템플릿 자녀가 그룹 상자 (그러나 반드시 첫 번째 템플릿 자식)이어야 중요하지 않습니다를
  • GroupBox의 전경이 템플릿에서 명시 적으로 설정되어 상속을 재정의합니다.
  • GroupBox에서 어떤 종류의 참조 호출을 사용해야합니다.
  • th 만

    MainWindow.xaml :

    <Window x:Class="WpfApplication1.MainWindow" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:my="clr-namespace:WpfApplication1" 
         Title="MainWindow" Height="350" Width="350"> 
        <Window.Resources> 
         <Thickness x:Key="TestPadding">5</Thickness> 
         <Style TargetType="{x:Type GroupBox}"> 
          <Setter Property="Foreground" Value="Red" /> 
          <Setter Property="Background" Value="Orange" /> 
         </Style> 
        </Window.Resources> 
        <Grid> 
         <my:TestControl Foreground="Blue" Background="Purple" /> 
        </Grid> 
    </Window> 
    

    TestControl.cs :

    using System; 
    using System.ComponentModel; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Data; 
    using System.Windows.Media; 
    using System.Windows.Media.Effects; 
    using System.Windows.Markup; 
    
    namespace WpfApplication1 
    { 
        public class TestControl : UserControl 
        { 
         public TestControl() 
         { 
          FrameworkElementFactory group = new FrameworkElementFactory(typeof(GroupBox)); 
          group.SetValue(GroupBox.ContentProperty, "My Child"); 
          group.SetResourceReference(GroupBox.MarginProperty, "TestPadding"); 
          this.SetValue(TestControl.TemplateProperty, new ControlTemplate(typeof(TestControl)) { VisualTree = group }); 
         } 
        } 
    } 
    


    전자 전경 속성은 다음

내 샘플 코드의 영향을받을 것으로 보인다 당신은 어떻게 생각합니까? Microsoft에보고해야 할 버그입니까?

답변

0

Microsoft WPF 개발팀에 연락했습니다. 그들은 이것을 버그라고 인정하지만, 우선 순위를 낮게 잡고 수정하지 않을 것입니다.

이 예제에서는 내 작업 : 다른 컨트롤을 * .SetResourceReference 호출을 GroupBox 대신 패딩 수행 할 수 있습니다.

0

저는 Microsoft 문제라고 생각하지 않습니다. 사실 나는 그것이 잘 동작한다고 생각한다. TestControl에 대한 코드에서 Templeate를 정의하고 있으며 템플릿의 루트 요소로 GroupBox를 설정하고 있습니다. 여기서 일어나는 일은 UserControl.Foreground 속성이 템플릿의 루트 인 GroupBox와 다르다는 것입니다. 그러면 GroupBox (GroupBox로)는 리소스 (이 경우 Window의 Resources)에서 상속되는 Foreground를 가져옵니다.

namespace WpfApplication1 
{ 
    public class TestControl : UserControl 
    { 
     public TestControl() 
     { 
      FrameworkElementFactory group = new FrameworkElementFactory(typeof(GroupBox)); 
      group.SetValue(GroupBox.ContentProperty, "My Child"); 
      group.SetResourceReference(GroupBox.MarginProperty, "TestPadding"); 

      //This line will work as a TeplateBinding 
      group.SetBinding(GroupBox.ForegroundProperty, new Binding() { Path = new PropertyPath("Foreground"), RelativeSource = RelativeSource.TemplatedParent }); 

      this.SetValue(TestControl.TemplateProperty, new ControlTemplate(typeof(TestControl)) { VisualTree = group }); 
     } 
    } 
} 

희망이 대답은 당신을 위해 도움이 될 것입니다 : 당신은이 문제를 해결하려면

, 당신은 "TemplateBindings"뭔가를 할 수 있으며, 다음과 코드는 당신을위한 작품은 TemplateBinding을 좋아합니다.

+0

댓글을 주셔서 감사합니다.하지만 내 질문이 거꾸로 붙었습니다. 이미 TemplateParent를 상속 받았기 때문에 원하지 않습니다. – Rhaokiel

+0

내가 이해한다면, GroupBox가 파란색 (예 : TestControl Foreground)이 될 것입니까? 나는 이것과 ResourceDictionary에서 가져 오는 다른 모든 속성들을 테스트합니다. 네가 원하는게 아니라면 나 한테 잘 설명해 줘. 감사합니다 –

+0

아니, 전 스타일과 일치로 GroupBox의 전경 빨간색으로하고 싶습니다. GroupBox가 자체 템플릿을 구현하지 않았거나 _group.SetResourceReference_를 사용하지 않은 경우에 해당됩니다.나는 이것들 중 어느 쪽이 어떻게 컨트롤의 전경에 영향을 주는지 어떻게 보이는지 간단히 알지 못합니다. – Rhaokiel