I have a listview and within it a gridview. I define its columns as below, for example for an employee type column:
<GridViewColumn Header="Employee Type" Width="80" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Left" Text="{Binding EmployeeType}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
As you can see it is bound to a "EmployeeType" property in view model.
private string _employeeType;
public string EmployeeType
{
get
{
return this._employeeType;
}
set
{
if (this._employeeType == value)
return;
this._employeeType = value;
OnPropertyChanged("EmployeeType");
}
}
EmployeeType can have below possibilities (They are strings ids):
- 1000A
- 1000B
- 1000C
so what I am trying to do is:
- If EmployeeType is 1000A I want to show in column "Employee Type" the string Manager
- If EmployeeType is 1000B I want to show in column "Employee Type" the string Engineer
- If EmployeeType is 1000C I want to show in column "Employee Type" the string Technical
Also, additionally, I do not know if there is the possibility to display in the column "Manager", "Engineer", "Technical", but internally (not displayed) linked to 1000A, 1000B, 1000C respectively so when I need to read the selected items from listview, I need to read 1000A, 1000B, 1000C instead of the displayed strings, something like a combobox in winforms that you display a member but this member is associated to a value that is not displayed and you can read.
How can I do this?
Maybe using a converter like below:
namespace Test.Converter
{
public class MyConverter : IValueConverter
{
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
// Do the conversion from string to visibility
if (value == "1000A")
return "Manager";
else if (value == "1000B")
return "Engineer";
else if (value == "1000C")
return "Technical";
else
throw new Exception(string.Format("Cannot convert, unknown value {0}", value));
}
public object ConvertBack(
object value, Type targetType, object parameter, CultureInfo culture)
{
// Do the conversion from visibility to string
string s = (string) value;
if (s.Equals("Manager", StringComparison.InvariantCultureIgnoreCase))
return "1000A";
else if (s.Equals("Engineer", StringComparison.InvariantCultureIgnoreCase))
return "1000B";
else if (s.Equals("Technical", StringComparison.InvariantCultureIgnoreCase))
return "1000C";
else
throw new Exception(string.Format("Cannot convert, unknown value {0}", value));
}
}
}
Also, where to place this converter? within view model?
View:
xmlns:l="clr-namespace:Test.Converter"
<Window.Resources>
<l:MyConverter x:Key="converter" />
</Window.Resources>
<GridViewColumn Header="Employee Type" Width="80" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Left" Text="{Binding EmployeeType, Converter={StaticResource converter}}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
So using this converter when displaying column to listview the "Convert" method is called and when reading selected items from listview, If I need to read back the value of this column how can I read using ConvertBack Method?
Also, another possibility is using maybe a trigger in xaml view and when reading selected items from listview to implement some method to do the contrary conversion in order to restore original value.
What you suggest? Note that this column is read-only (Displayed but not modifiable), cannot be modified.