[英]WPF TreeView with custom style
你可以幫幫我嗎? 我需要在每個項目上都帶有一個CheckBox的TreeView。 我不明白,我是這樣開始的,似乎是絕對錯誤的方式:
<TreeView Grid.Row="0" Grid.Column="0" Name="StagesTreeView" Margin="5">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<!-- <Setter Property="?????"> WHAT SHOULD BE HERE?
<Setter.Value>
</Setter.Value>
</Setter> -->
</Style>
</TreeView.Resources>
</TreeView>
給我看看 一些簡單的例子
UPD:哦...我想我需要一個ControlTemplate,但我仍然不知道如何制作它
UPD2:天哪,我正在加深對此的迷惑。 我應該在這里的某個地方使用RelativeSource標記擴展嗎? 誰來幫幫我!
UPD3:現在,它不能像TreeBox一樣工作-我無法展開\\折疊項目,盡管我稍微向前移動了-我可以看到復選框。
<TreeView Grid.Row="0" Grid.Column="0" Name="StagesTreeView" Margin="5">
<TreeView.Resources>
<Style x:Key="{x:Type TreeViewItem}" TargetType="TreeViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsActive}"/>
<TextBlock Text="{Binding Path=Alias.UserName}"/>
</StackPanel>
<ItemsPresenter Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
像這樣:
<TreeView Grid.Row="0" Grid.Column="0" Name="StagesTreeView" Margin="5">
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="2" Name="checkBox1"/>
<Border Padding="2">
<ContentPresenter Name="PART_header" ContentSource="Header"/>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
您可能會發現這很有用: 在WPF TreeView中使用復選框
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.