取决于实现,但基本的原理跟winForm差不多。
直接创建控件到Items这个直接判断,生成不同的控件就行了。
至于通过DataBind,那么只需要 ItemControl 去判断就行了,这里又可以很多路径去实现,比如在绑定事件中直接判断然后控制。
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,你绑定给里面的行元素就行了