WPF C# Statusbar label content from current control#39;s tooltip(当前控件的工具提示中的 WPF C# 状态栏标签内容)
问题描述
好的,所以我想弄清楚如何设置状态栏标签文本以显示有关鼠标悬停的当前控件的信息.我在很多程序上都看到过很多次,所以我知道这是可以做到的,我相信那里有可以帮助我的解释,但不幸的是,我似乎找不到合适的词来寻找答案......我能找到的最接近的东西在下面的链接中.我尝试使用它,但是当我尝试设置 text 属性时它给了我一个错误.
Ok, so I'm trying to figure out how to set a status bars label text to show information about the current control that a mouse is hovering over. I have seen this numerous times on many programs so I know it can be done and I'm sure there are explanations out there that could help me but I can't seem to find the right words to search for the answer unfortunately... The closest thing I could find was in the link below. I tried to utilize this but it gave me an error when I tried to set the text property.
有人有一些信息或链接可以帮助我吗?谢谢,瑞恩
Anyone have some information or a link to help me by chance? Thanks, Ryan
在不使用事件的情况下将鼠标悬停在控件上时在标签中显示文本
我的 XAML 代码:
My XAML Code:
<StatusBar>
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="75" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
<StatusBarItem Grid.Column="0">
<Label Content="New Lead Inquiry" />
</StatusBarItem>
<Separator Grid.Column="1" Style="{StaticResource StylingStatusBarSeparator}" />
<StatusBarItem Grid.Column="2">
<Label x:Name="infoStatusBar" Content="Label for text about the currently hovered item" />
</StatusBarItem>
<Separator Grid.Column="3" Style="{StaticResource StylingStatusBarSeparator}" />
<StatusBarItem Grid.Column="4">
<Label Content="Not Saved" />
</StatusBarItem>
</StatusBar>
推荐答案
这是一个不需要您修改每个子控件或使用任何框架的解决方案.
Here's a solution that doesn't require you to modify each child control or use any frameworks.
这与 MVVM 并没有真正的关系,因为它是纯 UI 的东西.这里没有任何涉及视图模型的内容.
This isn't really related to MVVM, since it's pure UI stuff. There's nothing here that would involve a viewmodel.
句柄 Window.PreviewMouseMove:
Handle Window.PreviewMouseMove:
MainWindow.xaml
MainWindow.xaml
<Window
...
PreviewMouseMove="Window_PreviewMouseMove"
>
MainWindow.xaml.cs
MainWindow.xaml.cs
定义一个 Object 类型的依赖属性,并在预览 mousemove 处理程序中,为其提供鼠标所在控件最近的父工具提示:
Define a dependency property of type Object, and in the preview mousemove handler, give it the nearest parent tooltip of the control the mouse is over:
private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
{
var element = Mouse.DirectlyOver as FrameworkElement;
HoverToolTip = GetTooltip(element);
}
#region HoverToolTip Property
public object HoverToolTip
{
get { return (object)GetValue(HoverToolTipProperty); }
set { SetValue(HoverToolTipProperty, value); }
}
public static readonly DependencyProperty HoverToolTipProperty =
DependencyProperty.Register(nameof(HoverToolTip), typeof(object), typeof(MainWindow),
new PropertyMetadata(null));
#endregion HoverToolTip Property
protected static Object GetTooltip(FrameworkElement obj)
{
if (obj == null)
{
return null;
}
else if (obj.ToolTip != null)
{
return obj.ToolTip;
}
else
{
return GetTooltip(VisualTreeHelper.GetParent(obj) as FrameworkElement);
}
}
并将其绑定到 XAML 中的任何内容.
And bind that to whatever in the XAML.
<Label
x:Name="StatusBar"
Content="{Binding HoverToolTip, RelativeSource={RelativeSource AncestorType=Window}}"
Grid.Row="2"
/>
该标签只是我在测试 XAML 中放入的快速工具.这个绑定是其中的重要部分:
That Label is just the quickie I put in my test XAML. This binding is the important part there:
{Binding HoverToolTip, RelativeSource={RelativeSource AncestorType=Window}}
<小时>
如果你愿意接受更少的样式点,你可以在没有依赖属性或绑定的情况下做到这一点:
If you're willing to settle for fewer style points, you can do it without the dependency property or the binding:
MainWindow.xaml
MainWindow.xaml
<Label
x:Name="StatusBarLabel"
Grid.Row="2"
/>
MainWindow.xaml.cs
MainWindow.xaml.cs
private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
{
var element = Mouse.DirectlyOver as FrameworkElement;
StatusBarLabel.Content = GetTooltip(element);
}
protected static Object GetTooltip(FrameworkElement obj)
{
if (obj == null)
{
return null;
}
else if (obj.ToolTip != null)
{
return obj.ToolTip;
}
else
{
return GetTooltip(VisualTreeHelper.GetParent(obj) as FrameworkElement);
}
}
这篇关于当前控件的工具提示中的 WPF C# 状态栏标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当前控件的工具提示中的 WPF C# 状态栏标签内容
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01