【发布时间】:2020-04-09 03:00:44
【问题描述】:
我需要根据三个属性更改文本块的前景。我使用 Multibinding 和转换器来做到这一点。转换器正确返回值,但前景没有改变。
<TextBlock
Margin="10,1,0,1"
Grid.Column="1"
Grid.ColumnSpan="2"
FontSize="15.5"
Text="{Binding Path=FullName}"
TextTrimming="CharacterEllipsis"
HorizontalAlignment="Stretch"
VerticalAlignment="Center">
<TextBlock.FontWeight>
<MultiBinding Converter="{StaticResource FontWeightConverter}">
<Binding ElementName="MessageCountBorder" Path="Visibility"/>
<Binding Path="IsMuted"/>
</MultiBinding>
</TextBlock.FontWeight>
<TextBlock.Foreground>
<MultiBinding Converter="{StaticResource ForegroundConverter}">
<Binding Path="TypeId"/>
<Binding Path="IsMuted"/>
<Binding Path="UserOnlineStatus"/>
</MultiBinding>
</TextBlock.Foreground>
</TextBlock>
我在 ListView 中使用这个 TextBlock。我绑定到前景的属性来自模型。
TextBlockForegroundConverter转换器如下。
public class TextBlockForegroundConverter: IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
// Value[0] ==> For TypeId
// Value[1] ==> For IsMuted
// Value[2] ==> For UserOnlineStatus
try
{
var typeId = (int)values[0];
var isMuted = (bool)values[1];
var userOnlineStatus = (int)values[2];
if (typeId == 1)
return isMuted ? Brushes.Gray : Brushes.White;
else
{
if (isMuted || userOnlineStatus == 2 || userOnlineStatus == 5)
return Brushes.Gray;
else
return Brushes.White;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
return Brushes.White;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
【问题讨论】:
-
return "white" 将返回字符串 white 而不是画笔。至少在这种情况下,您的转换器无法返回正确的值。
-
我尝试归还画笔和字符串。 @Andy 没用。
标签: wpf textblock multibinding