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의
MVVM을 사용하고 있다면 모델과 ViewModels가 섞여 있습니다. – Will
나는이 CodeProject 기사에 따라 일반 ViewModel을하고 있다고 생각한다. http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx 그래서 'ViewModel'은 아마도 내 데이터 클래스의 혼란스러운 이름 일 것이다. . 오히려 'MyDataObjectToDisplay'와 같아야합니다. – willem