与其使用 hack,不如尝试简单而整洁的方法?有两个不同的文本怎么样?例如
<Grid>
<Label Text="{Binding YourNormalTextComesHere}"
Visibility="{Binding IsUserNew, Converter={StaticResource BoolToVisibilityConv}, ConverterParameter=Not}" />
<StackPanel Orientation=Horizontal
Visibilty="{Binding IsUserNew, Converter={StaticResource BoolToVisibilityConv}}">
<Label Text="Your not registered with us. Please register "/>
<HyperLink NavigateUri="...">here</HyperLink>
</StackPanel>
</Grid>
根据用户是否是新用户,显示欢迎文本或文本链接组合。这篇SO 帖子展示了如何Hyperlink
使用 a 。
由于我不知道内置BooleanToVisibilityConverter
(doc)是否支持否定,我已经为您提供了我的实现。请注意,我没有在示例代码中实例化转换器。
[ValueConversion(typeof (bool?), typeof (Visibility))]
public class BoolToVisibilityConverter : IValueConverter
{
public const string Invert = "Not";
private const string TypeIsNotAllowed = "The type '{0}' is not allowed.";
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var boolValue = value as bool?;
if (boolValue == null)
throw new NotSupportedException(String.Format(TypeIsNotAllowed, value.GetType().Name));
return ((boolValue.Value && !IsInverted(parameter)) || (!boolValue.Value && IsInverted(parameter)))
? Visibility.Visible
: Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var visibilityValue = value as Visibility?;
if (visibilityValue == null)
throw new NotSupportedException(String.Format(TypeIsNotAllowed, value.GetType().Name));
return visibilityValue == Visibility.Visible && !IsInverted(parameter);
}
#endregion
private static bool IsInverted(object param)
{
var strParam = param as string;
if (param == null || string.IsNullOrEmpty(strParam))
return false;
return strParam.Equals(Invert, StringComparison.InvariantCultureIgnoreCase);
}
}
我假设其余的都很清楚,因为您熟悉MVVM
aso
希望这个对你有帮助。