이 이상한 현상을 발견했을 때 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에보고해야 할 버그입니까?
댓글을 주셔서 감사합니다.하지만 내 질문이 거꾸로 붙었습니다. 이미 TemplateParent를 상속 받았기 때문에 원하지 않습니다. – Rhaokiel
내가 이해한다면, GroupBox가 파란색 (예 : TestControl Foreground)이 될 것입니까? 나는 이것과 ResourceDictionary에서 가져 오는 다른 모든 속성들을 테스트합니다. 네가 원하는게 아니라면 나 한테 잘 설명해 줘. 감사합니다 –
아니, 전 스타일과 일치로 GroupBox의 전경 빨간색으로하고 싶습니다. GroupBox가 자체 템플릿을 구현하지 않았거나 _group.SetResourceReference_를 사용하지 않은 경우에 해당됩니다.나는 이것들 중 어느 쪽이 어떻게 컨트롤의 전경에 영향을 주는지 어떻게 보이는지 간단히 알지 못합니다. – Rhaokiel