将工具提示添加到WPF TreeView中的DataTemplate

时间:2018-12-02 00:33:23

标签: c# wpf treeview tooltip treeviewitem

我试图在WPF的TreeView中为子级添加工具提示。但是,它甚至出现在XAML中,我也无法获得在树状视图子级中呈现的工具提示。有人可以告诉我如何解决这个问题吗?

<Window x:Class="Client_Invoice_Auditor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Client_Invoice_Auditor"
        xmlns:self="clr-namespace:Client_Invoice_Auditor.CoreClientAR"
        mc:Ignorable="d"
        Title="Client Invoice Auditor" Height="450" Width="1000">
    <Window.Resources>
        <self:SPConverter x:Key="SPConverter"/>
        <self:ErrorExpandConverter x:Key="ErrorExpandConverter"/>
        <self:AssignRowConverter x:Key="AssignRowConverter"/>
        <self:IssueIconConverter x:Key="IssueIconConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDeitions>

        </Grid.ColumnDeitions>
        <Grid.RowDeitions>
            <RowDeition Height="20*"/>
            <RowDeition Height="80*"/>
        </Grid.RowDeitions>
        <Grid Grid.Row="0" Grid.Column="0">
            <StackPanel Orientation="Vertical">
                <DockPanel VerticalAlignment="Top" Height="20" Panel.ZIndex="1">
                    <Menu Name="fileMenu" Width="Auto" DockPanel.Dock="Top">
                        <MenuItem Header="File">
                            <MenuItem Header="Open Account File" Click="menuOpenFile_Click"/>

                            <MenuItem Header="Exit" Click="menuExit_Click"/>
                        </MenuItem>
                        <MenuItem Header="Options">
                            <!--<MenuItem Header="Update" Click="update_Click"/>-->
                            <MenuItem Header="About" Click="about_Click"/>
                        </MenuItem>
                    </Menu>
                </DockPanel>
                <WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" Height="Auto">
                    <StackPanel Width="Auto" Orientation="Horizontal" HorizontalAlignment="Center">
                        <Border BorderBrush="MediumAquamarine" BorderThickness="2">
                            <Label Name="AccountNumber"/>
                        </Border>
                        <Border BorderBrush="MediumAquamarine" BorderThickness="2">
                            <Label Name="AcctDesc"/>
                        </Border>
                        <Border BorderBrush="MediumAquamarine" BorderThickness="2">
                            <Label Name="Organization"/>
                        </Border>
                    </StackPanel>
                </WrapPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                    <Label Margin="20,10,0,0" Content="Activity Date Time" />
                    <Label Margin="60,10,0,0" Content="Beginning Balance" />
                    <Label Margin="10,10,0,0" Content="Charge Amount" />
                    <Label Margin="30,10,0,0" Content="Adjustments" />
                    <Label Margin="40,10,0,0" Content="Payments" />
                    <Label Margin="60,10,0,0" Content="End Balance" />
                    <Label Margin="50,10,0,0" Content="Issues" />
                </StackPanel>
            </StackPanel>

        </Grid>
        <Grid Grid.Row="1" Grid.Column="0">
            <Grid Name="IssuesGrid">
                <Grid.ColumnDeitions>
                    <!--<ColumnDeition Width="80*"/>-->
                    <!--<ColumnDeition Width="20*"/>-->
                </Grid.ColumnDeitions>
                <TreeView Name="View">
                    <!--<Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" 
                                                Value="{Binding Class, Converter={StaticResource ErrorExpandConverter}}" />
                    </Style>-->
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}">
                            <Setter Property="IsExpanded" 
                                                Value="{Binding Dummies, Converter={StaticResource ErrorExpandConverter}}" />
                        </Style>
                    </TreeView.ItemContainerStyle>
                    <TreeView.ItemTemplate>

                        <HierarchicalDataTemplate DataType="{x:Type self:dailyAccountBalance}" ItemsSource="{Binding Dummies}">

                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" IsEnabled="False" Grid.Row="{Binding RowIndex}" ToolTip="This info">
                                    <TextBlock Width="150" Text="{Binding ActivityDate}" />
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding BegBalance}"/>
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding ChrgAmount}" />
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding AdjAmount}" />
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding PmtAmount}" />
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding EndBalance}" ToolTipService.IsEnabled="True" ToolTipService.ToolTip="This balance thing" ToolTip="This balance"/>

                                <ContentControl HorizontalAlignment="Left" Margin="20,0,0,0" Width="100" ToolTip="This">
                                    <ContentControl.Content>
                                        <MultiBinding Converter="{StaticResource IssueIconConverter}">
                                            <Binding Path="RowIndex"/>
                                            <Binding Path="Errors"/>
                                        </MultiBinding>
                                    </ContentControl.Content>
                                    <!--<TextBlock Text="Test"/>-->

                                </ContentControl>
                            </StackPanel>

                            <HierarchicalDataTemplate.ItemTemplate>

                                <DataTemplate DataType="{x:Type self:DummyItem}">

                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                        <!--<TextBlock Text="{Binding Text}" />-->
                                        <DataGrid ItemsSource="{Binding ChargeActivities}" AutoGenerateColumns="False">

                                            <DataGrid.Style>
                                                <Style TargetType="{x:Type DataGrid}">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Path=Items.Count,
                                                                RelativeSource={RelativeSource Self}}"  Value="0">
                                                            <Setter Property="Visibility" Value="Collapsed" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </DataGrid.Style>

                                            <DataGrid.Columns>
                                                <DataGridTextColumn x:Name="ChrgID"  Header=" Charge" Binding="{Binding ChargeID}" />
                                                <DataGridTextColumn x:Name="ChrgType"  Header="Charge Type" Binding="{Binding ChargeType}">
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style TargetType="{x:Type TextBlock}">
                                                            <Style.Triggers>
                                                                <Trigger Property="Text" Value="CR">
                                                                    <Setter Property="Foreground" Value="Red"/>
                                                                </Trigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn x:Name="ChrgAmt"  Header="Amount" Binding="{Binding ChargeAmount}">
                                                    <DataGridTextColumn.ElementStyle>

                                                        <Style TargetType="{x:Type TextBlock}">
                                                            <Style.Triggers>
                                                                <DataTrigger Binding="{Binding ChargeType}" Value="CR">
                                                                    <Setter Property="Foreground" Value="Red"/>
                                                                </DataTrigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn x:Name=""  Header="" Binding="{Binding Stuff}" />
                                                <DataGridTextColumn x:Name="Class"  Header=" Class" Binding="{Binding Class}">
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style TargetType="{x:Type TextBlock}">
                                                            <Setter Property="Foreground" Value="{Binding Class, Converter={StaticResource SPConverter}}"/>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                            </DataGrid.Columns>
                                        </DataGrid>
                                    </StackPanel>
                                </DataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>

                    </HierarchicalDataTemplate>

                        <!--<TreeViewItem x:Key="Test">
                            <TextBlock Text="Me gusta"></TextBlock>
                        </TreeViewItem>-->
                    </TreeView.ItemTemplate>
                </TreeView>

            </Grid>
        </Grid>
    </Grid>
</Window>

任何获得工具提示以在树状视图子级中呈现的建议​​都将受到赞赏。在此先感谢一吨。

1 个答案:

答案 0 :(得分:0)

您没有在模板内部设置元素的tooltip属性,而是在TreeViewItem本身上进行设置。

这是一个非常简化的示例:

    <TreeView>
        <TreeViewItem Header="Item #1" DataContext="Test1" />
        <TreeViewItem Header="Item #2" DataContext="Test2" />
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Style.Setters>
                    <Setter Property="ToolTip" Value="{Binding}" />
                </Style.Setters>
            </Style>
        </TreeView.Resources>
    </TreeView>

在这里,我必须手动设置DataContext并对其进行硬编码,以使该示例正常工作,因为仅使用Header属性没有设置它,但是在现实世界中,您将受到数据绑定的约束,因此将对其进行设置。