数据模板中标签中的下划线文本
我有一个 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 TextBlock
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过为 ListViewItem 指定 ControlTemplate 或更改 DataTemplate 来完成此操作。下面的示例显示了这两种方法。请注意,当您使用 ControlTemplate 时,您会丢失所选 ListViewItem 的蓝色背景(当您将其注释掉时,它会返回)
编辑:
我没有很好地阅读你的要求。您只想在名称下划线。那么唯一的可能就是使用Datatemplate,或者重写TextBox的ControlTemplate。
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.