我正在开发一个用户控件,该控件显示用户的名字和姓氏、与他们关联的机构以及DataGrid中的其他项。并且对于本讨论的目的来说最重要的是他们的社会保障号码的最后4位数字。我们加密SSN,所以为了显示SSN,我必须解密它。然而,编写加密/解密代码并对其进行测试的同事说,他所做的测试导致代码运行了45分钟,与我们所掌握的所有关于人的数据相比。显然,如果呈现数据网格需要45分钟,那么没有人会等待用户控件出现。
所以,我的老板让我在SSN列中放置一些文本,说明它是隐藏的,然后当用户将鼠标悬停在控件上时,它会弹出一个工具提示,显示SSN的最后4个。起初,我认为这很容易实现,但后来我意识到,当我在过去这样做时,我要检索所有的数据,然后显示工具提示中没有显示的内容。我不能那么做。那么,我如何使它在工具提示弹出时,只有在那时候它才会检索给定ID的SSN,然后显示SSN的最后4个呢?
下面是我目前所拥有的,首先是我为工具提示创建的样式:
<UserControl.Resources>
<Style TargetType="TextBlock" x:Key="DelayToolTip">
<Setter Property="ToolTipService.Placement" Value="Top" />
<Setter Property="ToolTipService.InitialShowDelay" Value="1000" />
<Setter Property="ToolTipService.ShowDuration" Value="10000" />
</Style>
</UserControl.Resources>
下面是DataGrid列的XAML:
<DataGrid.Columns>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="Agency Name" Binding="{Binding Agency.AgencyName}" />
<DataGridTextColumn Header="Operator #" Binding="{Binding PersonnelCertifications[0].CertIdentifier}" />
<DataGridTemplateColumn Header="SSN">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="Hidden"
Foreground="DarkOrange"
Background="Aquamarine"
Margin="3,1"
Style="{StaticResource DelayToolTip}"
Padding="3">
<TextBlock.ToolTip>
<StackPanel Orientation="Horizontal">
<TextBlock Text="SSN: "
FontWeight="Bold" />
<TextBlock Text="{Binding ID}" />
</StackPanel>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
当前显示的是ID值。我需要使用这个ID值来调用我同事编写的存储过程来解密SSN。
我正在使用VS2019,.NET 4.5.2
例如,您可以处理工具提示
中TextBlock
的Loaded
事件,如下所示:
private async void TextBlock_Loaded(object sender, RoutedEventArgs e)
{
TextBlock textBlock = (TextBlock)sender;
int id = (textBlock.DataContext as Person)?.ID;
string ssn = await Task.Run(() => { /*call SP and return the SSN here...*/ });
textBlock.Text = ssn;
}
XAML:
<DataTemplate>
<TextBlock Text="Hidden"
Foreground="DarkOrange"
Background="Aquamarine"
Margin="3,1"
Style="{StaticResource DelayToolTip}"
Padding="3">
<TextBlock.ToolTip>
<StackPanel Orientation="Horizontal">
<TextBlock Text="SSN: " FontWeight="Bold" />
<TextBlock Text="Loading...." Loaded="TextBlock_Loaded" />
</StackPanel>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>