【问题标题】:Is there any way to change the foreground of a TextBlock using MultiBinding?有没有办法使用 MultiBinding 更改 TextBlock 的前景?
【发布时间】: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


【解决方案1】:

XAML 和转换器代码看起来都不错。实际上,转换器在其中一种情况下返回一个字符串,这不是可接受的输出,但据我所知,这种情况是不可达的,所以应该没问题。我的猜测是属性之一“TypeId”、“IsMuted”...在更新其值时不会引发INotifyPropertyChanged.PropertyChanged 事件。即使 INPC 接口没有实现,绑定最初也会起作用,但是当绑定属性更新时,它们的未来值永远不会改变。

public class MyClass : INotifyPropertyChanged
{
    private bool isMuted;

    public bool IsMuted
    {
        get { return isMuted; }
        set
        {
            if (isMuted != value)
            {
                isMuted = value;
                OnPropertyChanged("IsMuted");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string prN)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(prN));
    }
}

【讨论】:

  • 这可能没有任何好处。我们只是在转换器中使用这 3 个属性作为前景色。据我所知,这里不需要使用 INPC 接口,除非我们更改属性值。无论如何,感谢 zaphod-ii 的帮助。
  • 您可以尝试打开“输出”窗口并查找任何绑定错误。确保在工具 > 选项 > 调试 > 输出窗口 > 数据绑定中启用它们。
猜你喜欢
  • 2021-02-25
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 2018-11-29
  • 2012-12-04
  • 2011-11-25
相关资源
最近更新 更多