Wpf 使用 GridViewColumn 绑定到前景
我正在做:
<ListView Margin="34,42,42,25" Name="listView1">
<ListView.View>
<GridView>
<GridViewColumn Width="550" Header="Value" DisplayMemberBinding="{Binding Path=MyValue}"/>
</GridView>
</ListView.View>
<ListView.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green"/>
</Style>
</ListView.Resources>
</ListView>
这正在起作用,我可以看到我的项目呈绿色。
现在,我想对此使用绑定值,因此我有一个属性:
private Color _theColor;
public System.Windows.Media.Color TheColor
{
get { return _theColor; }
set
{
if (_theColor != value)
{
_theColor = value;
OnPropertyChanged("TheColor");
}
}
}
但是如果我使用此绑定:
<Setter Property="Foreground" Value="{Binding Path=TheColor}"/>
它不起作用...
我该如何纠正?
当然,我将 TheColor 设置为 Colors.Green
...
感谢您的帮助
I'm doing :
<ListView Margin="34,42,42,25" Name="listView1">
<ListView.View>
<GridView>
<GridViewColumn Width="550" Header="Value" DisplayMemberBinding="{Binding Path=MyValue}"/>
</GridView>
</ListView.View>
<ListView.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green"/>
</Style>
</ListView.Resources>
</ListView>
and this is working, I can see my items in Green.
Now, I want to use a binding value with this, so I have a property :
private Color _theColor;
public System.Windows.Media.Color TheColor
{
get { return _theColor; }
set
{
if (_theColor != value)
{
_theColor = value;
OnPropertyChanged("TheColor");
}
}
}
but If I use this binding :
<Setter Property="Foreground" Value="{Binding Path=TheColor}"/>
It's not working...
How can I correct that ?
Of course, I'm setting the TheColor to Colors.Green
...
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很简单,您无法绑定到
Color
。前景
需要设置为Brush
。因此,我将该值设置为SolidColorBrush
并将Brush
的颜色属性绑定到TheColor
DependencyProperty
:在我的示例中,我只是将属性
TheColor
绑定到DependencyProperty
:之后,您可以绑定到
TheColor
DependencyProperty
>。就我而言,我只是为主 Window/UserControl/Page 指定了 x:Name 并绑定到该名称:Easy, you can't bind to a
Color
. TheForeground
need to be set to aBrush
. So I would set the value to aSolidColorBrush
and bind theBrush
's color property to yourTheColor
DependencyProperty
:In my example I just bound the property
TheColor
to aDependencyProperty
:After that, you can bind to the
TheColor
DependencyProperty
. In my case, I just gave the main Window/UserControl/Page an x:Name and bound to that: