我的ListView的自定义ToopTip

时间:2015-06-30 09:25:08

标签: wpf listview tooltip

这是我的ListView

    <ListView Name="lvAdapters" Background="Transparent" HorizontalAlignment="Left" Height="84" Margin="0,51,0,0"
          VerticalAlignment="Top" Width="785" BorderThickness="0,1,0,0" SelectionMode="Single" BorderBrush="Gainsboro"
              ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Foreground" Value="Gainsboro"/>
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="Padding" Value="4,1"/>
                <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Setter Property="Height" Value="20"></Setter>
                <Style.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Foreground" Value="Black"></Setter>
                        <Setter Property="Background" Value="#FFD8D5D5"/>
                        <Setter Property="BorderBrush" Value="White"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="True"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="#FF15669E"/>
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Setter Property="BorderThickness" Value="1"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="False"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="#FF15669E"/>
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Setter Property="BorderThickness" Value="1"/>
                    </MultiTrigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView ColumnHeaderContainerStyle="{StaticResource ListViewHeaderStyle}" >
                <GridViewColumn Width="40" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Name="cbSelectInterface" IsThreeState="False" IsChecked="{Binding IsSelected,
                                      RelativeSource={RelativeSource
                                      AncestorType={x:Type ListViewItem}}}"></CheckBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Description" DisplayMemberBinding="{Binding Description}" />
                <GridViewColumn Width="300" Header="ID" DisplayMemberBinding="{Binding ID}" />
                <GridViewColumn Width="150" Header="IP Address" DisplayMemberBinding="{Binding IPAddress}" />
            </GridView>
        </ListView.View>
        <ListView.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text }"></Setter>
            </Style>
        </ListView.Resources>
    </ListView>

我的模特:

public class NetworkAdapter
{
    public string ID { get; set; }
    public string Description { get; set; }
    public string IPAddress { get; set; }
}

将项目插入我的ListView

NetworkAdapter[] adapters = NetworkAdapter.MachineAdapters;

for (int i = 0; i < adapters.Length; i++)
{
    string adapterProperties =
        (i + 1) + ". " +
        adapters[i].Description + " | " +
        adapters[i].ID + " | " +
        adapters[i].IPAddress;
    lvAdapters.Items.Add(adapters[i]);
}

所以我有ToolTip显示column值,但我想创建自己的ToolTip

    "Name: " + Name + '\' + 
    "ID: " + ID + '\' + 
    "IP Address: " + 

的IPAddress

如何为此更改我的ToolTip

1 个答案:

答案 0 :(得分:1)

删除它:

<ListView.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text }"></Setter>
        </Style>
    </ListView.Resources>

并将Setter添加到ListView.ItemContainerStyle

<Setter Property="ToolTip" >
                    <Setter.Value>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Name, StringFormat=Name: {0:#}}"/>
                            <TextBlock Text="{Binding ID, StringFormat=ID: {0:#}}"/>
                            <TextBlock Text="{Binding IPAddress, StringFormat=IP Address: {0:#}}"/>
                        </StackPanel>
                    </Setter.Value>
                </Setter>