Hi guys I am working on a project in C# WPF where I need to display a client's status in a listview So I have the following enum that defines a Client Status
大家好我正在使用C#WPF中的项目,我需要在列表视图中显示客户端的状态所以我有以下定义客户端状态的枚举
//Values used here for Bitwise Operations
public enum ClientStatus
{
NONE = 0,
NEWCLIENT = 1,
MONITORED = 2,
IMPORTAND = 4,
DISATISFIED = 8,
DETERIORATING = 16,
SATISFIED = 32
};
To Convert Each to a specific Brush I have the following code, Tested and it works
要将每个转换为特定的画笔我有以下代码,经测试并且它可以工作
[ValueConversion(typeof(Enums.ClientStatus), typeof(Brush))]
public class StateValueColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Brush Brush = Brushes.Red;
if (value is Enums.ClientStatus)
{
Enums.ClientStatus sv = (Enums.ClientStatus)value;
switch (sv)
{
case Enums.ClientStatus.IMPORTAND:
Brush = Brushes.Blue;
break;
case Enums.ClientStatus.MONITORED:
Brush = Brushes.Purple;
break;
case Enums.ClientStatus.NEWCLIENT:
Brush = Brushes.Orange;
break;
case Enums.ClientStatus.SATISFIED:
Brush = Brushes.Green;
break;
case Enums.ClientStatus.DETERIORATING:
Brush = Brushes.Yellow;
break;
case Enums.ClientStatus.DISATISFIED:
Brush = Brushes.Red;
break;
}
}
return Brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
to do the Binding I did the following, which is not what our lecturer exactly wanted
做绑定我做了以下,这不是我们的讲师完全想要的
<GridViewColumn Header="Status" Width="110">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="50" Background="{Binding Path=Status, Converter={StaticResource ColorConverter}}"></TextBox>
<TextBox Width="50" Background="{Binding Path=SatisFactory, Converter={StaticResource ColorConverter}}"></TextBox>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
We need to display each of the enums as a different color however a client can have multiple statuses i.e
我们需要将每个枚举显示为不同的颜色,但客户端可以有多种状态,即
var status = ClientStatus.NEWCLIENT | ClientStatus.SATISFIED;
This will now return a int value of 33
现在返回int值为33
My Problem now is I used two properties in my Client to show seperate statuses which is incorrect we need to use Bitwise Operations ie.
我的问题现在是我在我的客户端中使用两个属性来显示单独的状态,这是不正确的我们需要使用Bitwise操作,即。
if ((status & ClientStatus.NEWCLIENT) == ClientStatus.NEWCLIENT)
{
//do whatever
}
so I can do the above code but how would I do the binding on one property to show multiple color's in the one gridviewcollumn, I search the forum but I missed it if this question or something similar was there
所以我可以做上面的代码但是如何在一个属性上进行绑定以在一个gridviewcollumn中显示多个颜色,我搜索论坛但我错过了如果这个问题或类似的东西在那里
I am not really sure how to state the question so I will try it in other words
我不太确定如何陈述这个问题所以我会用其他的方式来试试
I want to display all selected enums in the column.
我想在列中显示所有选定的枚举。
Thanks for any help or suggestions Hope I made sence
感谢任何帮助或建议希望我做了
2 个解决方案
#1
0
If I understand your question correctly, you'd like to display an icon, image, or UI element for each value in the enumeration and have them be visible if the object's status matches that value.
如果我正确理解您的问题,您希望为枚举中的每个值显示图标,图像或UI元素,并在对象的状态与该值匹配时使其可见。
I think I would create a simple StackPanel containing all the UI elements likes so:
我想我会创建一个包含所有UI元素的简单StackPanel,如下所示:
<StackPanel Orientation="Horizontal">
<Rectangle Fill="Blue"
Visibility="{Binding Status,
Converter={StaticResource StatusToVisibilityConverter},
ConverterParameter=NEWCLIENT}" />
<Rectangle Fill="Green"
Visibility="{Binding Status,
Converter={StaticResource StatusToVisibilityConverter},
ConverterParameter=SATISFIED}" />
...
</StackPanel>
Then in your StatusToVisibilityConverter compare the value of Status with the value in ConverterParameter (you can convert that to you enumeration's value with TryParse static method.
然后在StatusToVisibilityConverter中将Status的值与ConverterParameter中的值进行比较(您可以使用TryParse静态方法将其转换为枚举值)。
Hope that helps.
希望有所帮助。
#2
0
Define DataTemplate name StatusDataTemplate, or a default DataTemplate for Enums.ClientStatus as TargetType.
In this template, define a 3 columns X 2 rows Grid.
In each grid cell, define a Border, having as background the binding using one of the 6 enum as ConverterParameter, with a converter that returns the 'right' color if value AND parameter = parameter, transparent otherwise.
将DataTemplate名称定义为StatusDataTemplate,或将Enums.ClientStatus的默认DataTemplate定义为TargetType。在此模板中,定义3列X 2行Grid。在每个网格单元格中,定义一个边框,使用6个枚举中的一个作为ConverterParameter作为背景绑定,如果值AND参数=参数,则返回“右”颜色的转换器,否则为透明。
Then you can use :
然后你可以使用:
<ContentPresenter Content="{Binding Status}" />
... if you used a default DTpl
...如果您使用默认的DTpl
Or :
<ContentPresenter Content="{Binding Status}" ContentTemplate="{StaticResource StatusTemplate}"/>
#1
0
If I understand your question correctly, you'd like to display an icon, image, or UI element for each value in the enumeration and have them be visible if the object's status matches that value.
如果我正确理解您的问题,您希望为枚举中的每个值显示图标,图像或UI元素,并在对象的状态与该值匹配时使其可见。
I think I would create a simple StackPanel containing all the UI elements likes so:
我想我会创建一个包含所有UI元素的简单StackPanel,如下所示:
<StackPanel Orientation="Horizontal">
<Rectangle Fill="Blue"
Visibility="{Binding Status,
Converter={StaticResource StatusToVisibilityConverter},
ConverterParameter=NEWCLIENT}" />
<Rectangle Fill="Green"
Visibility="{Binding Status,
Converter={StaticResource StatusToVisibilityConverter},
ConverterParameter=SATISFIED}" />
...
</StackPanel>
Then in your StatusToVisibilityConverter compare the value of Status with the value in ConverterParameter (you can convert that to you enumeration's value with TryParse static method.
然后在StatusToVisibilityConverter中将Status的值与ConverterParameter中的值进行比较(您可以使用TryParse静态方法将其转换为枚举值)。
Hope that helps.
希望有所帮助。
#2
0
Define DataTemplate name StatusDataTemplate, or a default DataTemplate for Enums.ClientStatus as TargetType.
In this template, define a 3 columns X 2 rows Grid.
In each grid cell, define a Border, having as background the binding using one of the 6 enum as ConverterParameter, with a converter that returns the 'right' color if value AND parameter = parameter, transparent otherwise.
将DataTemplate名称定义为StatusDataTemplate,或将Enums.ClientStatus的默认DataTemplate定义为TargetType。在此模板中,定义3列X 2行Grid。在每个网格单元格中,定义一个边框,使用6个枚举中的一个作为ConverterParameter作为背景绑定,如果值AND参数=参数,则返回“右”颜色的转换器,否则为透明。
Then you can use :
然后你可以使用:
<ContentPresenter Content="{Binding Status}" />
... if you used a default DTpl
...如果您使用默认的DTpl
Or :
<ContentPresenter Content="{Binding Status}" ContentTemplate="{StaticResource StatusTemplate}"/>