【发布时间】:2009-04-23 23:25:32
【问题描述】:
我想实现一种效果,我可以将鼠标悬停在按钮上并让 TextBlock 更新其内容(通过绑定)。更复杂的是,Button 是 ItemsControl/DataTemplate 中定义的众多按钮之一。 TextBlock 超出了 ItemsControl 的范围。
问题的一些简化标记如下:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ItemsControl Grid.Row="0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Title}"
Command="{Binding Command}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock x:Name="TitleTextBox" Grid.Row="1" />
</Grid>
假设在这个例子中,我可能想将数据项的“Title”属性绑定到 TextBlock 的“Text”属性。
我假设我想点击按钮的 IsMouseOver,但我似乎无法正确连接它。
【问题讨论】:
-
如果我有误解,请纠正我,但听起来你只是想使用 XAML 来做到这一点?我很确定它不能单独使用 XAML 来完成。如果您使用 ListView 而不是 ItemsControl,您可以将 TextBlock 绑定到 ListView 的 SelectedItem 属性,但这仍然无法解决悬停问题。为此,您将(我认为)需要使用代码隐藏。不过,我不是 WPF 专家。
标签: wpf