繁体   English   中英

在列表框中更改数据绑定文本块的前景色

[英]Change foreground color of databinded textblock in listbox

我试图根据绑定值更改我在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();
    }
}

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

前景需要刷子,而不是颜色。 尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM