c# - WPF 中带有文本 block 的动态工具提示

标签 c# wpf xaml tooltip textblock

我在文本 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}" 

当您更改应向用户显示的属性时,需要提及modeUpdateSourceTrigger

关于c# - WPF 中带有文本 block 的动态工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356606/

相关文章:

c# - 运行没有 .exe 扩展名的外部应用程序

c# - CopyFromScreen 不工作

wpf - xaml Scrollviewer - 禁用 Overscrolling/rubber-band-effect/snapback-effect/bouncing of whole window

c# - ISupportIncrementalLoading 在离开后不会停止

wpf - x :Key ="{x:Type TextBox}" do? 是什么意思

xaml - Winrt StreamWriter & StorageFile 没有完全覆盖文件

c# - 绑定(bind)到附加属性

c# - WPF - 视觉树不用于绘图?

c# - 在手动调整尺寸之前,窗口无法最大化

c# - 通过 XPathSelectElements 获取具有命名空间的 XElement