首页 新闻 会员 周边 捐助

请问wpf怎么给listbox不同行设置不同颜色

0
悬赏园豆:50 [待解决问题]

RT 刚接触wpf,现在想要根据listbox里不同的数据来变换背景色,
比如说数据源里有个字段的值是1,然后在显示的时候只要是那个字段是1的所有数据的行背景色都变红色,
如果值是2,就都变蓝色这种,
这个要怎么实现呢

黄焖女朋友的主页 黄焖女朋友 | 初学一级 | 园豆:124
提问于:2021-08-25 15:43
< >
分享
所有回答(2)
0

取决于实现,但基本的原理跟winForm差不多。

直接创建控件到Items这个直接判断,生成不同的控件就行了。

至于通过DataBind,那么只需要 ItemControl 去判断就行了,这里又可以很多路径去实现,比如在绑定事件中直接判断然后控制。

花飘水流兮 | 园豆:13617 (专家六级) | 2021-08-25 16:42
0

xaml:
<Grid.Resources>
<convert:DataTableCellColorConvert x:Key="DataTableCellColorConvert"/>
</Grid.Resources>

DataTableCellColorConvert.cs:
namespace XDS_SmartC_Client.Custom.CustomConverter
{
public class DataTableCellColorConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
return "LightGreen";
else
return "default";
}

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
class DataTable1CellColorConvert : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch ((int)value)
        {
            case 1:
                return "palevioletred ";
            case 2:
                return "default";
            case 3:
                return "Green";
            case 4:
                return "red";
            default:
                return "default";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
class DataTableCellColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value)
            return "Blue";
        else
            return "default";
    }

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

class DataGridBackColorConverterToGreen : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            if (Common.LoginEmp.Theme1 == "BaseLight")
            {
                return "White"; 

            }
            else
            {
                return "Black";
            }
        }
        if ((bool)value)
        {
            if (Common.LoginEmp.Theme1 == "BaseLight")
            {
                return "SpringGreen";
            }
            else
            {
                return "DarkGreen";
            }
        }
        else
        {
            if (Common.LoginEmp.Theme1 == "BaseLight")
            {
                return "White";
            }
            else
            {
                return "Black";
            }
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (Common.LoginEmp.Theme1 == "BaseLight")
        {
            return "White";
        }
        else
        {
            return "Black";
        }
    }
}

class DataGridColorConverterToGreen : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value)
            return "Red";
        else
            return "Black";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value)
            return "Red";
        else
            return "Black";
    }
}

class DataGridBackColorConverterToWhiteSmoke : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            if (Common.LoginEmp.Theme1 == "BaseLight")
            {
                return "White";

            }
            else
            {
                return "Black";
            }
        }
        if (!(bool)value)
        {
            if (Common.LoginEmp.Theme1 == "BaseLight")
            {
                return "WhiteSmoke";
            }
            else
            {
                return "White";
            }
        }
        else
        {
            if (Common.LoginEmp.Theme1 == "BaseLight")
            {
                return "White";
            }
            else
            {
                return "Black";
            }
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (Common.LoginEmp.Theme1 == "BaseLight")
        {
            return "White";
        }
        else
        {
            return "Black";
        }
    }
}

class DataGridCellColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((bool)value)
            return "LightGray";
        else
            return "default";
    }

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

}

界面调用:
<TextBlock FontSize="12" Margin="0,0,0,0" Text="{Binding RealMaterialName}" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="{Binding JudgeColors,Mode=TwoWay,Converter={StaticResource DataTableCellColorConverter}}" ></TextBlock>
差不多就是上面这些步骤,可以看的了解下,至于listbox,你绑定给里面的行元素就行了

勤奋的二牛 | 园豆:8 (初学一级) | 2021-08-27 11:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册