嗨 我在使用绑定(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/