Wpf 使用 GridViewColumn 绑定到前景

发布于 2024-08-22 08:19:18 字数 1048 浏览 10 评论 0原文

我正在做:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2024-08-29 08:19:18

很简单,您无法绑定到 Color前景需要设置为Brush。因此,我将该值设置为 SolidColorBrush 并将 Brush 的颜色属性绑定到 TheColor DependencyProperty

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground">
        <Setter.Value>
            <SolidColorBrush Color="{Binding Path=TheColor}" />
        </Setter.Value>
    </Setter>
</Style>

在我的示例中,我只是将属性 TheColor 绑定到 DependencyProperty

public static readonly DependencyProperty TheColorProperty = 
DependencyProperty.Register("TheColor", typeof(System.Windows.Media.Color), typeof(YourWindow));

public System.Windows.Media.Color TheColor
{
    get { return (System.Windows.Media.Color)GetValue(TheColorProperty); }
    set { SetValue(TheColorProperty, value); }
}

之后,您可以绑定到 TheColor DependencyProperty >。就我而言,我只是为主 Window/UserControl/Page 指定了 x:Name 并绑定到该名称:

<SolidColorBrush Color="{Binding Path=TheColor, ElementName=yourWindowVar}" />

Easy, you can't bind to a Color. The Foreground need to be set to a Brush. So I would set the value to a SolidColorBrush and bind the Brush's color property to your TheColor DependencyProperty:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground">
        <Setter.Value>
            <SolidColorBrush Color="{Binding Path=TheColor}" />
        </Setter.Value>
    </Setter>
</Style>

In my example I just bound the property TheColor to a DependencyProperty:

public static readonly DependencyProperty TheColorProperty = 
DependencyProperty.Register("TheColor", typeof(System.Windows.Media.Color), typeof(YourWindow));

public System.Windows.Media.Color TheColor
{
    get { return (System.Windows.Media.Color)GetValue(TheColorProperty); }
    set { SetValue(TheColorProperty, value); }
}

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:

<SolidColorBrush Color="{Binding Path=TheColor, ElementName=yourWindowVar}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文