数据模板中标签中的下划线文本

发布于 2024-08-13 20:57:27 字数 835 浏览 3 评论 0原文

我有一个 ListView,其中包含从集合绑定的对象。我使用 DataTemplate 设置的对象的表示形式。现在我想做以下事情。 我的 DataTemplate 中有两个 TextBlock

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

我已经指定了一个 ItemContainerStyle,用它来实现悬停效果。

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

我的目标是当用户将鼠标移到 ListViewItem 上时,在包含名称的 TextBlock 下划线。路径不应加下划线。如何实现这一点?如何为每个 ListViewItem 访问 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 which 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2024-08-20 20:57:27

您可以通过为 ListViewItem 指定 ControlTemplate 或更改 DataTemplate 来完成此操作。下面的示例显示了这两种方法。请注意,当您使用 ControlTemplate 时,您会丢失所选 ListViewItem 的蓝色背景(当您将其注释掉时,它会返回)
编辑:
我没有很好地阅读你的要求。您只想在名称下划线。那么唯一的可能就是使用Datatemplate,或者重写TextBox的ControlTemplate。

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文