条件文本绑定 XAML
Conditional text binding XAML
我有 3 个属性要绑定到 XAML 中的文本块。一个是有条件的,另外两个是我要根据该条件显示的字符串。
<TextBlock Text="{Binding TrueText}" Style="{StaticResource styleSimpleText}" Visibility="{Binding ShowTrueText, Converter={StaticResource boolToVisibilityConverter}}"/>
<TextBlock Text="{Binding FalseText}" Style="{StaticResource styleSimpleText}" Visibility="{Binding ShowTrueText, Converter={StaticResource invertedBoolToVisibilityConverter}}"/>
这可行,但现在文本块必须有不同的名称。我可以把它变成一个带有条件的 TextBlock 吗?
可以,只需将它们包装在一个 TextBlock 中,如下所示:
<TextBlock x:name="myTextBlock" Style="{StaticResource styleSimpleText}">
<TextBlock Text="{Binding TrueText}" Visibility="{Binding ShowTrueText, Converter={StaticResource boolToVisibilityConverter}}"/>
<TextBlock Text="{Binding FalseText}" Visibility="{Binding ShowTrueText, Converter={StaticResource invertedBoolToVisibilityConverter}}"/>
</TextBlock>
不过,我认为最好的答案是 Clemens 提供的答案(使用 DataTrigger)。
您可以使用 Style 和 DataTrigger 来实现:
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding FalseText}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ShowTrueText}" Value="True">
<Setter Property="Text" Value="{Binding TrueText}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
另一种方法是使用带有多值转换器的 MultiBinding:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource TextConverter}">
<Binding Path="TrueText"/>
<Binding Path="FalseText"/>
<Binding Path="ShowTrueText"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
转换器看起来像这样:
public class TextConverter : IMultiValueConverter
{
public object Convert(
object[] values, Type targetType, object parameter, CultureInfo culture)
{
var trueText = (string)values[0];
var falseText = (string)values[1];
var showTrueText = (bool)values[2];
return showTrueText ? trueText : falseText;
}
public object[] ConvertBack(
object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
您可以在您的视图模型中设置它并让它确定要显示的文本。
private static readonly string TRUETEXT = "This is the text to show when true";
private static readonly string FALSETEXT = "This is the text to show when false";
private bool _myBooleanProperty;
public bool MyBooleanProperty
{
get { return _myBooleanProperty; }
set
{
if (_myBooleanProperty != value)
{
_myBooleanProperty = value;
OnPropertyChanged("MyBooleanProperty");
OnPropertyChanged("ResultText");
}
}
}
public string ResultText
{
get
{
return MyBooleanProperty ? TRUETEXT : FALSETEXT;
}
}
然后你只用一个文本块绑定到它。不需要能见度转换器。
如果存在不应显示文本的状态,您也可以在其中工作。
<TextBlock Text="{Binding ResultText}" Style="{StaticResource styleSimpleText}" />
在我看来,解决此问题的最佳方法是在您的视图模型中添加一个新字符串 属性,其中 returns TrueText
或 FalseText
取决于有条件的。有了这样的 属性,你可以只使用普通绑定。
public string TheNewProperty
{
get
{
return ShowTrueText ? TrueText : FalseText;
}
}
<TextBlock Text="{Binding TheNewProperty}" Style="{StaticResource styleSimpleText}"/>
我们为 MVVM 做这类事情的方法是为此在您的视图模型中创建一个 属性。这允许您对视图模型上的条件进行单元测试。
您的视图模型中的 属性 将是 TextBlock 绑定到的字符串值。 viewmodel 在某些时候将根据您需要的条件逻辑确定该字符串的值。
我有 3 个属性要绑定到 XAML 中的文本块。一个是有条件的,另外两个是我要根据该条件显示的字符串。
<TextBlock Text="{Binding TrueText}" Style="{StaticResource styleSimpleText}" Visibility="{Binding ShowTrueText, Converter={StaticResource boolToVisibilityConverter}}"/>
<TextBlock Text="{Binding FalseText}" Style="{StaticResource styleSimpleText}" Visibility="{Binding ShowTrueText, Converter={StaticResource invertedBoolToVisibilityConverter}}"/>
这可行,但现在文本块必须有不同的名称。我可以把它变成一个带有条件的 TextBlock 吗?
可以,只需将它们包装在一个 TextBlock 中,如下所示:
<TextBlock x:name="myTextBlock" Style="{StaticResource styleSimpleText}">
<TextBlock Text="{Binding TrueText}" Visibility="{Binding ShowTrueText, Converter={StaticResource boolToVisibilityConverter}}"/>
<TextBlock Text="{Binding FalseText}" Visibility="{Binding ShowTrueText, Converter={StaticResource invertedBoolToVisibilityConverter}}"/>
</TextBlock>
不过,我认为最好的答案是 Clemens 提供的答案(使用 DataTrigger)。
您可以使用 Style 和 DataTrigger 来实现:
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding FalseText}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ShowTrueText}" Value="True">
<Setter Property="Text" Value="{Binding TrueText}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
另一种方法是使用带有多值转换器的 MultiBinding:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource TextConverter}">
<Binding Path="TrueText"/>
<Binding Path="FalseText"/>
<Binding Path="ShowTrueText"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
转换器看起来像这样:
public class TextConverter : IMultiValueConverter
{
public object Convert(
object[] values, Type targetType, object parameter, CultureInfo culture)
{
var trueText = (string)values[0];
var falseText = (string)values[1];
var showTrueText = (bool)values[2];
return showTrueText ? trueText : falseText;
}
public object[] ConvertBack(
object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
您可以在您的视图模型中设置它并让它确定要显示的文本。
private static readonly string TRUETEXT = "This is the text to show when true";
private static readonly string FALSETEXT = "This is the text to show when false";
private bool _myBooleanProperty;
public bool MyBooleanProperty
{
get { return _myBooleanProperty; }
set
{
if (_myBooleanProperty != value)
{
_myBooleanProperty = value;
OnPropertyChanged("MyBooleanProperty");
OnPropertyChanged("ResultText");
}
}
}
public string ResultText
{
get
{
return MyBooleanProperty ? TRUETEXT : FALSETEXT;
}
}
然后你只用一个文本块绑定到它。不需要能见度转换器。
如果存在不应显示文本的状态,您也可以在其中工作。
<TextBlock Text="{Binding ResultText}" Style="{StaticResource styleSimpleText}" />
在我看来,解决此问题的最佳方法是在您的视图模型中添加一个新字符串 属性,其中 returns TrueText
或 FalseText
取决于有条件的。有了这样的 属性,你可以只使用普通绑定。
public string TheNewProperty
{
get
{
return ShowTrueText ? TrueText : FalseText;
}
}
<TextBlock Text="{Binding TheNewProperty}" Style="{StaticResource styleSimpleText}"/>
我们为 MVVM 做这类事情的方法是为此在您的视图模型中创建一个 属性。这允许您对视图模型上的条件进行单元测试。
您的视图模型中的 属性 将是 TextBlock 绑定到的字符串值。 viewmodel 在某些时候将根据您需要的条件逻辑确定该字符串的值。