Mai*_*das 5 wpf binding tooltip
我Textblock在网格内有一个,并且有一个ToolTip应该显示“工具提示”绑定,但如果它是Null或Empty它应该显示“名称”绑定。
我如何在 XAML 中实现这一目标?
<TextBlock Margin="3" TextAlignment="Left" VerticalAlignment="Center" FontSize="12" Foreground="Black" HorizontalAlignment="Left" ToolTip="{Binding Tooltip}" Text="{Binding Name}"/>
如果字符串是NULL
创建 MultiValueConverter 并声明为静态资源:
<local:MyConverter x:Key="MyConverter"/>
Run Code Online (Sandbox Code Playgroud)
您的转换器类应如下所示:
public class MyConverter: IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string tooltip = values.ElementAtOrDefault(0) as string,
name = values.ElementAtOrDefault(1) as string;
if(!String.IsNullOrEmpty(tooltip))
return tooltip;
else
return name;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
和你的文本块:
<TextBlock FontSize="12" Foreground="Red" Text="{Binding MyName}">
<TextBlock.ToolTip>
<MultiBinding Converter="{StaticResource NameConverter}">
<Binding Path="Tooltip" IsAsync="True"/>
<Binding Path="MyName"/>
</MultiBinding>
</TextBlock.ToolTip>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2882 次 |
| 最近记录: |