2009-11-04 3 views
2

WPF에서는 TreeView에서 상속하는 사용자 지정 컨트롤이 있습니다. 코드는 내가 generic.xaml을 코드를 생각WPF에서 Generic.xaml의 스타일이 내 사용자 정의 컨트롤에 적용되지 않는 이유는 무엇입니까?

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfTestCustomControl"> 

    <HierarchicalDataTemplate DataType="{x:Type local:ViewModel}"> 
     <TextBlock Foreground="Blue" Text="{Binding Path=TextValue}"></TextBlock> 
    </HierarchicalDataTemplate> 

    <Style TargetType="{x:Type local:CustomTRV}"> 
     <Setter Property="ItemContainerStyle"> 
      <Setter.Value> 

       <Style TargetType="{x:Type TreeViewItem}"> 
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
        <Setter Property="FontWeight" Value="Bold" /> 
        <Style.Triggers> 
         <Trigger Property="IsSelected" Value="True"> 
          <Setter Property="FontWeight" Value="Normal" /> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 

      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

public class CustomTRV : TreeView 
{ 
    static CustomTRV() 
    { 
     //Removed this because I want the default TreeView look. 
     //......CustomTRV.DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTRV), new FrameworkPropertyMetadata(typeof(CustomTRV))); 
    } 

    public void Connect(string entityHierarchyToken) 
    { 
     //build viewModel classes... 
     this.ItemsSource = new List<ViewModel>() 
     { 
      new ViewModel() { TextValue = "aaaa" }, 
      new ViewModel() { TextValue = "bbb" }, 
      new ViewModel() { TextValue = "ccc" }, 
      new ViewModel() { TextValue = "ddd" }, 
      new ViewModel() { TextValue = "eee" }, 
     }; 
    } 
} 
다음과 같이 generic.xaml을의 콘텐츠가 보이는

가 ... 나의 제어에 적용하고 얻을해야 ... 다음과 같다 이러한 ItemContainer 속성 값을 설정해야합니다. 그러나 ItemContainerStyle은 아무런 효과가없는 것처럼 보입니다.

참고 : Generic.xaml의 HierarchicalDataTemplate은 정상적으로 작동하므로 파일이 해석됩니다.

아이디어가 있으십니까? 따로 지정 컨트롤 대 MVVM의

+0

MVVM을 사용하고 있다면 모델과 ViewModels가 섞여 있습니다. – Will

+0

나는이 CodeProject 기사에 따라 일반 ViewModel을하고 있다고 생각한다. http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx 그래서 'ViewModel'은 아마도 내 데이터 클래스의 혼란스러운 이름 일 것이다. . 오히려 'MyDataObjectToDisplay'와 같아야합니다. – willem

답변

3

질문, 문제는 당신이 당신의 사용자 지정 컨트롤과 스타일을 연관 라인을 주석 한된다

//CustomTRV.DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTRV), new FrameworkPropertyMetadata(typeof(CustomTRV))); 

인체 공학적를 컨트롤은 TreeView의 표준 스타일이 단지 것 에스.

+0

이 점이 중요합니까? 이후로 나는 ItemContainerStyle을 오버라이드하고 Template가 아닌가? – willem

+0

예, ItemContainerStyle이 CustomTRV에 적용되는 스타일 내에 포함되어 있습니다. CustomTRV의 스타일은 CustomTRV 클래스와 연결되지 않았기 때문에 적용되지 않습니다. 선의 주석을 제거하십시오. –

+1

환상적입니다. 그건 속임수 야. 내 스타일이 일반 TreeView 스타일을 기반으로해야했기 때문에 나는 약간 어려움을 겪었습니다. 이것에 대한 조언은 다음과 같습니다 :