开发者

Underline Text in Label which is in a DataTemplate

i have a ListView which contains objects bound from an collection. The representation of the objects I have set with a DataTemplate. Now I want to do the following. There are two TextBlocks in my DataTemplate:

<DataTemplate>
   <StackPanel>
       <TextBlock Text="{Binding Name}"></TextBlock>
       <TextBlock Text="{Binding Path}"></TextBlock>
   </StackPanel>
</DataTemplate>

I already have specified an ItemContainerStyle which I use to realize a hover-effect.

<Style TargetType="ListViewItem" x:Key="ContainerStyle">
       <Style.Triggers>
                 <EventTrigger RoutedEvent="Mouse.MouseEnter">
  ... and so on

My aim is to underline the TextBlock w开发者_运维知识库hich contains the Name, when user moves mouse over the ListViewItem. The Path shouldn't be underlined. How can this be realized? How can an element in DataTemplate can be accessed for each ListViewItem?

Greetings, Martin


You can do this either by specifying the ControlTemplate for the ListViewItem, or by changing the DataTemplate. The example below shows both methods. Note that you lose the blue background for the selected ListViewItem when you use the ControlTemplate (when you comment it out it returns) EDIT: I did not read your requirement well. You only want to underline the Name. Then the only possibility is to use the Datatemplate, or to rewrite the ControlTemplate of the TextBox.

<Window x:Class="Underlining.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
   >
    <StackPanel>
        <ListView ItemsSource="{Binding}">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">
                                <Border Name="UnderlineInControlTemplate" BorderThickness="2,0,2,0"
                                        BorderBrush="Transparent">
                                    <ContentPresenter HorizontalAlignment="Left"
                                                      VerticalAlignment="Center"/>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter TargetName="UnderlineInControlTemplate"
                                                Property="BorderBrush"
                                                Value="BlueViolet"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                <StackPanel>
                    <Border Name="UnderlineInDataTemplate" BorderThickness="0,0,0,2"
                        BorderBrush="Transparent">
                         <TextBlock Text="{Binding Name}"/>
                    </Border>
                    <TextBlock Text="{Binding Path}"/>
                </StackPanel>
                    <DataTemplate.Triggers>
                        <Trigger Property="TextBlock.IsMouseOver" Value="True">
                            <Setter TargetName="UnderlineInDataTemplate"
                                    Property="BorderBrush"
                                    Value="Red"/>
                        </Trigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</Window>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜