wpf - 如何将颜色绑定(bind)到文本框背景wpf

标签 wpf binding colors background

嗨 我在使用绑定(bind)设置文本框背景颜色时遇到问题。 我用这个代码

<TextBlock Width="Auto" Height="Auto"
                                   Text="{Binding ConnectionType}"
                                   Canvas.Left="{Binding LabelPosition.X}"
                                   Canvas.Top="{Binding LabelPosition.Y}" Background="{Binding ParentCanvasColor}">

                          <TextBlock.RenderTransform>
                            <TranslateTransform X="5" Y="5"/>
                          </TextBlock.RenderTransform>
                        </TextBlock>

ParentCanvasColoris 属性在我的类中称为连接。这个属性看起来像这样

 public Color ParentCanvasColor
    {
        get
        {
            if (parentCanvas != null && parentCanvas is DesignerCanvasNetDiag)
            {
                return Colors.Red;
            }
            return Colors.Transparent;
        }
    }

当然,我将 Connection 类的对象添加到了 textBlock 的数据上下文中

最佳答案

绑定(bind)SolidColorBrush而不是像下面这样的Color

    public SolidColorBrush ParentCanvasColor
    {
        get
        {
            if (parentCanvas != null && parentCanvas is DesignerCanvasNetDiag)
            {
                return  new SolidColorBrush(Colors.Red);
            }
            return  new SolidColorBrush(Colors.Transparent);
        }
    }

关于wpf - 如何将颜色绑定(bind)到文本框背景wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4136855/

相关文章:

c# - WPF RoutedEventHandler 不适用于内容

wpf - WPF 样式中 IsMouseOver 和 IsHighlighted 的区别

c# - 在绑定(bind)字符串上使用时无法使简单转换器工作

JSF UIComponent 绑定(bind)、可序列化和 View 范围

wpf - TemplateBinding 不绑定(bind)到效果的属性?

java - 以编程方式生成 Material Design 颜色集的方法有哪些?

c# - 将 FlowDocument 转换为 PDF 的最佳方式是什么

c# - 如何在 wpf 应用程序的代码隐藏中创建 Collection View 源

html - 在 Tumblr 中为每个帖子定义样式

matlab - 在 MATLAB 中对图进行剖面线或着色