本文介绍了带有复选框解析的TreeViewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi all,
I have a TreeView whereby each item has a CheckBox for selection:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ToggleButton x:Name="Expander" Content="+" Grid.Column="0" Grid.Row="0" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press" Style="{StaticResource ResourceKey=ExpanderStyle}" VerticalAlignment="Top">
</ToggleButton>
<CheckBox Margin="3" Grid.Row="0" Grid.Column="1" IsChecked="False" VerticalAlignment="Bottom" Name="chkTreeViewItem" Checked="chkTreeViewItem_Checked">
</CheckBox>
<ContentPresenter x:Name="PART_Header"
ContentSource="Header"
Grid.Row="0"
Grid.Column="2"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
<ItemsPresenter x:Name="ItemsHost"
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
VerticalAlignment="Bottom"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="False">
<Setter TargetName="ItemsHost"
Property="Visibility"
Value="Collapsed" />
</Trigger>
<Trigger Property="HasItems" Value="False">
<Setter TargetName="Expander"
Property="Visibility"
Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now I have an event for the checkbox which I need to do two things.
Firstly, I require it to check the checkbox of it's parent nodes. I've been trying to use the VisualTreeHelper.GetParent for this. I can find the TreeViewItems above it, but am struggling to then access their checkboxes to check them.
Secondly, I then need to parse the entire TreeView and check any nodes that match the original selection (and repeat the above step for each of those)
Here's my TreeView:
<TreeView Height="289" HorizontalAlignment="Left" Margin="10,10,0,0" Name="tvSettings" VerticalAlignment="Top" Width="265" >
<TreeViewItem Header="IIS-ISAPIFilter">
<TreeViewItem Header="ADFS-WebAgentToken" />
<TreeViewItem Header="IIS-ASPNET">
<TreeViewItem Header="ADFS-FederationService" />
<TreeViewItem Header="ADFS-FederationServiceProxy" />
<TreeViewItem Header="ADFS-WebAgentClaims">
<TreeViewItem Header="RMS-Federation" />
</TreeViewItem>
<TreeViewItem Header="RightsManagementServices">
<TreeViewItem Header="RMS-Federation" />
</TreeViewItem>
<TreeViewItem Header="WebAccess" />
</TreeViewItem>
<TreeViewItem Header="Printing-InternetPrinting-Server" />
</TreeViewItem>
<TreeViewItem Header="IIS-NetFxExtensibility">
<TreeViewItem Header="ADFS-WebAgentToken" />
<TreeViewItem Header="BITSExtensions-Upload" />
<TreeViewItem Header="CertificateEnrollmentPolicyServer" />
<TreeViewItem Header="CertificateEnrollmentServer" />
<TreeViewItem Header="IIS-ASPNET">
<TreeViewItem Header="ADFS-FederationService" />
<TreeViewItem Header="ADFS-FederationServiceProxy" />
<TreeViewItem Header="ADFS-WebAgentClaims">
<TreeViewItem Header="RMS-Federation" />
</TreeViewItem>
<TreeViewItem Header="RightsManagementServices">
<TreeViewItem Header="RMS-Federation" />
</TreeViewItem>
<TreeViewItem Header="WebAccess" />
</TreeViewItem>
<TreeViewItem Header="MSMQ-HTTP" />
<TreeViewItem Header="Printing-InternetPrinting-Server" />
<TreeViewItem Header="RightsManagementServices">
<TreeViewItem Header="RMS-Federation" />
</TreeViewItem>
<TreeViewItem Header="WCF-HTTP-Activation">
<TreeViewItem Header="CertificateEnrollmentPolicyServer" />
<TreeViewItem Header="CertificateEnrollmentServer" />
</TreeViewItem>
<TreeViewItem Header="WebAccess" />
</TreeViewItem>
<TreeViewItem Header="IIS-ServerSideIncludes" />
</TreeViewItem>
<TreeViewItem Header="IIS-CommonHttpFeatures">
<TreeViewItem Header="IIS-DefaultDocument">
<TreeViewItem Header="ADFS-FederationService" />
<TreeViewItem Header="ADFS-FederationServiceProxy" />
<TreeViewItem Header="BITSExtensions-Upload" />
<TreeViewItem Header="CertificateEnrollmentPolicyServer" />
<TreeViewItem Header="CertificateEnrollmentServer" />
<TreeViewItem Header="HCAP-Server" />
</TreeView>
I'm really struggling here so any help or direction will be really appreciated.
Thanks,
Jib.
推荐答案
这篇关于带有复选框解析的TreeViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!