如何更改列表框(wpf)中项目的字体颜色

发布于 2024-11-05 18:41:16 字数 327 浏览 1 评论 0原文

我有一个列表框,它绑定到一个可观察的集合。集合中的元素包含一个称为颜色的变量。我的列表框的项目已经绑定到集合,但是如何将项目字体颜色绑定到该集合?我已经有一个数据模板,可以很好地用这样的颜色名称替换项目的名称

<DataTemplate x:Key="myListBox">
        <TextBlock Padding="0,0,10,0" 
    Text="{Binding Path=Color, Mode=Default}"/>
    </DataTemplate>

,但我似乎找不到必须设置哪个属性才能绑定颜色。

I have a listbox which is bounded to an observable collection. The elements in the collection contain a variable called color. My listbox's items are already bounded to the collection, but how do I also bind the items font color to that? I already have a data template which works fine replacing item's name with the color name like this

<DataTemplate x:Key="myListBox">
        <TextBlock Padding="0,0,10,0" 
    Text="{Binding Path=Color, Mode=Default}"/>
    </DataTemplate>

but I can't seem to find which property I have to set in order to bind the color.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

樱桃奶球 2024-11-12 18:41:16

不确定您指的是哪种颜色,但这将设置背景和文本/前景色。

<TextBlock Padding="0,0,10,0" 
    Text="{Binding Path=Color, Mode=Default}"
    Background="{Binding myBackgroundColour}"
    Foreground="{Binding myTextColour}"
/>

编辑:依赖属性 -

public string Color
{
    get { return (string)GetValue(ColorProperty); }
    set { SetValue(ColorProperty, value); }
}

// Using a DependencyProperty as the backing store for Color.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty ColorProperty =
    DependencyProperty.Register("Color", typeof(string), typeof(CLASSNAMEHERE), new UIPropertyMetadata("Black"));

将 CLASSNAMEHERE 替换为您要放入的类的名称,即视图模型类或代码隐藏类名称。

使用:

this.Color = "Yellow";

Not sure which colour you're referring to, but this will set the background and text/foreground colours.

<TextBlock Padding="0,0,10,0" 
    Text="{Binding Path=Color, Mode=Default}"
    Background="{Binding myBackgroundColour}"
    Foreground="{Binding myTextColour}"
/>

EDIT: dependancy prop -

public string Color
{
    get { return (string)GetValue(ColorProperty); }
    set { SetValue(ColorProperty, value); }
}

// Using a DependencyProperty as the backing store for Color.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty ColorProperty =
    DependencyProperty.Register("Color", typeof(string), typeof(CLASSNAMEHERE), new UIPropertyMetadata("Black"));

Replace CLASSNAMEHERE with the name to the class you're putting it in, ie the viewmodel class or codebehind class name.

use:

this.Color = "Yellow";
甜尕妞 2024-11-12 18:41:16

你可以使用这个资源样式

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Foreground" Value="></Setter>
        <Setter Property="FontWeight" Value="Bold"></Setter>
        bla bla bla
    </Style>

you can use this resource style

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Foreground" Value="></Setter>
        <Setter Property="FontWeight" Value="Bold"></Setter>
        bla bla bla
    </Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文