【问题标题】:Change foreground color of databinded textblock in listbox更改列表框中数据绑定文本块的前景色
【发布时间】:2014-03-13 22:53:56
【问题描述】:

我正在尝试根据绑定值更改 ListBox 中的数据绑定 TextBlock 的前景色。

我的代码如下:xaml

<Grid.Resources>
   <converters:ColorConverter x:Key="ColorConverter"/>
</Grid.Resources>


<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Name="TitleText">
                <Run Foreground="{Binding Type, Converter={StaticResource ColorConverter}}" Text="&#x20b9;" />
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

ColorConverter 类:

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
             String Value = (String)value;

             if (Value.Equals("Credit"))
                return Colors.Green;
            else
                return Colors.Red;

        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

当我运行代码时,没有错误,但颜色不会改变。

【问题讨论】:

    标签: c# data-binding windows-phone-8 listbox foreground


    【解决方案1】:

    前景使用画笔,而不是颜色。试试这个:

    <Run Text="...">
        <Run.Foreground>
            <SolidColorBrush Color="{Binding Type, Converter={StaticResource ColorConverter}}"/>
        </Run.Foreground>
    </Run>
    

    【讨论】:

    • 它有效!!..您为我节省了很多时间..!!非常感谢!!
    • Wpf 如果充满了可爱的小陷阱。
    猜你喜欢
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多