我在文本 block wpf 中的工具提示方面遇到一些问题。
在我完成 1 项任务后,如果任务出错,我想使用工具提示更新错误信息。 但完成后工具提示永远不会显示。请帮我 。 谢谢
这是我的代码 C# 代码
if (status == "Error")
{
LogCreateSite item = (LogCreateSite)gridLog.Items[rowIndex];
item.ErrorInfo = "Error";
DataTemplate template = cellTitle.ContentTemplate;
Canvas canvas = (Canvas)template.LoadContent();
TextBlock txtError = (TextBlock)canvas.Children[1];
ToolTip toolTip = new ToolTip();
toolTip.Content = "asdfasdf";
txtError.ToolTip = toolTip;
txtError.UpdateLayout();
}
还有我的 Xaml:
<DataTemplate x:Key="error">
<Canvas Margin="10,15,0,0">
<!--<Ellipse Fill="#FF5050" Width="12" Height="12">
</Ellipse>-->
<Viewbox Width="16" Height="16">
<Frame Source="../Icon/Error_16.xaml" />
</Viewbox>
<TextBlock Text="Error" Margin="25,-3,0,0">
</TextBlock>
<TextBlock Cursor="Hand" Name="txtErrorInfo" ToolTip="{Binding ErrorInfo, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" FontSize="14" Text="?" Margin="60,-3,0,0" FontWeight="Bold" Foreground="Blue">
</TextBlock>
</Canvas>
</DataTemplate>
最佳答案
您需要绑定(bind)工具提示以向用户显示错误/消息。
请阅读本教程以进行 wpf 绑定(bind)。 Introduction to WPF data binding来自 WPF 教程。
为了正确的绑定(bind),您的 XAML 应该像这样。
Name="txtErrorInfo" ToolTip="{binding path=error mode=OneWay UpdateSourceTrigger=PropertyChanged}"
当您更改应向用户显示的属性时,需要提及mode
和UpdateSourceTrigger
。
关于c# - WPF 中带有文本 block 的动态工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356606/