Wpf 如何使工具提示显示在固定位置(在本例中:页面左下角)
我有几个项目(以矩形的形式)在一个“旋转木马”(这基本上只是一个花哨的PathListBox)中 当鼠标悬停在其中一个矩形上时,将显示包含一些信息的工具提示。矩形的外观由数据模板决定。此处显示此数据模板的xaml的开头:Wpf 如何使工具提示显示在固定位置(在本例中:页面左下角),wpf,xaml,tooltip,wpf-positioning,Wpf,Xaml,Tooltip,Wpf Positioning,我有几个项目(以矩形的形式)在一个“旋转木马”(这基本上只是一个花哨的PathListBox)中 当鼠标悬停在其中一个矩形上时,将显示包含一些信息的工具提示。矩形的外观由数据模板决定。此处显示此数据模板的xaml的开头: <Carousel:CarouselControl.DataTemplateToUse> <DataTemplate> <Grid ShowGridLines="True"> <Grid.ToolTip>
<Carousel:CarouselControl.DataTemplateToUse>
<DataTemplate>
<Grid ShowGridLines="True">
<Grid.ToolTip>
<ToolTip Placement="Right"
PlacementRectangle="50,0,0,0"
HorizontalOffset="10"
VerticalOffset="20"
HasDropShadow="false"
PlacementTarget="{Binding ElementName=Box512}"
>
<BulletDecorator>
<TextBlock Text="{Binding Text}"/>
</BulletDecorator>
</ToolTip>
</Grid.ToolTip>
//further xaml code defining the DataTemplate
这不管用。工具提示不是显示在页面的左下角,而是显示在鼠标悬停的矩形上。然后我试着:
PlacementTarget="{x:Bind Box512}"
。。。这也没用
因此,我的问题是:如何使工具提示显示在Box512附近的同一位置,而不受鼠标悬停在哪个矩形上的影响
****************************补充资料*********************** MainWindow.xaml:
<Carousel:CarouselControl x:Name="CarouselControl"
ScaleRange="1.0,1.0"
MaxNumberOfItemsOnPath="4"
SelectedItem="{Binding CurrentData,Mode=TwoWay}"
SelectionChanged="CarouselControl_SelectionChanged"
ItemsSource="{Binding GraphItems}"
CustomPathElement="{Binding ElementName=customPath}">
<Carousel:CarouselControl.DataTemplateToUse>
<DataTemplate>
with this line of code the text inside the tooltip would get displayed BUT
the tooltip would not be in the desired location:
<!--<Grid ShowGridLines="True">-->
with this line of code the tooltip is empty but the tooltip
is displayed in the desired location:
<Grid ShowGridLines="True" ToolTipService.PlacementTarget="{Binding
ElementName=Box512}">
<Grid.ToolTip>
<ToolTip Placement="Right"
PlacementRectangle="50,0,0,0"
HorizontalOffset="10"
VerticalOffset="20"
HasDropShadow="false">
<BulletDecorator>
<BulletDecorator.Bullet>
<Ellipse Height="10" Width="20" Fill="Blue"/>
</BulletDecorator.Bullet>
<TextBlock Text="{Binding Text}"/>
</BulletDecorator>
</ToolTip>
</Grid.ToolTip>
//further xaml code defining the DataTemplate
</Grid>
</DataTemplate>
</Carousel:CarouselControl.DataTemplateToUse>
</Carousel:CarouselControl>
GraphItems列表定义为MainViewModel的属性:
private List<GraphNode> _GraphItems;
public List<GraphNode> GraphItems
{
get { return _GraphItems; }
private set
{
_cGraphItems = value;
this.RaisePropertyChangedEvent("GraphItems");
}
}
应使用TooltipService指定放置目标:
<TextBlock Text="Header" Grid.Row="0" Background="Green" Margin="0,0,0,0" ToolTipService.PlacementTarget="{Binding ElementName=Box512}">
<TextBlock.ToolTip>
<ToolTip Placement="Right"
PlacementRectangle="50,0,0,0"
HorizontalOffset="10"
VerticalOffset="20"
HasDropShadow="false"
>
<BulletDecorator>
<TextBlock Text="This is the tooltip text"/>
</BulletDecorator>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
谢谢您的回答。。。工具提示现在显示在所需位置。。。但是,工具提示为空,因为现在行中的绑定不再工作。。。我试图再次为TextBlock元素建立旧的datacontext,但我对datacontext不是很精通,所以我失败了。。。我尝试的一件事是:{Binding Path=Text,RelativeSource={RelativeSource DataTemplate}}。。。您知道如何使绑定重新工作吗?TextBlock datacontext必须具有Text属性。关于XAML,对于Texblock、Grid和相关的Datatemplate,此datacontext是相同的。关于您的描述,此Datacontext必须是“若干项(以矩形形式)”之一。这些项目是否具有文本属性?你能分享“item/rectangle”类的代码,至少是与Text属性有关的部分吗?你好,鲍勃,我编辑了这篇文章,添加了很多附加信息(请参阅下面的“附加信息”)。。。我认为使用ToolTipService.PlacementTarget=“{Binding ElementName=Box512}”会将工具提示的datacontext更改为Box512之一。当我简单地使用而不是时,文本将显示在工具提示中。添加属性“ToolTipService.PlacementTarget”会导致工具提示为空。
public partial class MainWindow : Window
{
///View Model for MainWindow.
private ViewModels.MainWindowVM mainViewModel = null;
public MainWindow()
{
InitializeComponent();
//Initialize View Model
mainViewModel = new ViewModels.MainWindowVM();
//Set it as Data Context
this.DataContext = mainViewModel;
//Close-Event bound
mainViewModel.RequestClose += delegate
(object sender, EventArgs e) { this.Close(); };
}
}
private List<GraphNode> _GraphItems;
public List<GraphNode> GraphItems
{
get { return _GraphItems; }
private set
{
_cGraphItems = value;
this.RaisePropertyChangedEvent("GraphItems");
}
}
public class GraphNode : ObservableObject
{
///GENERAL INFORMATION
private string _Text;
public string Text {
get { return _Text; }
set { _Text = value; this.RaisePropertyChangedEvent("Text"); }
}
//more code
}
<TextBlock Text="Header" Grid.Row="0" Background="Green" Margin="0,0,0,0" ToolTipService.PlacementTarget="{Binding ElementName=Box512}">
<TextBlock.ToolTip>
<ToolTip Placement="Right"
PlacementRectangle="50,0,0,0"
HorizontalOffset="10"
VerticalOffset="20"
HasDropShadow="false"
>
<BulletDecorator>
<TextBlock Text="This is the tooltip text"/>
</BulletDecorator>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>