0

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.

Astro
  • 367
  • 1
  • 3
  • 17
  • This question seems garbled. Why a IMultiValueConverter? The method signatures are for IValueConverter. Why is column readonly? There is nothing in the code that indicates readonly. – AQuirky Jul 29 '17 at 02:16
  • You seem to be asking if what you show here would possibly work. Didn't you test it? What specific problem did you get then (besides that MyConverter should implement IValueConverter instead of IMultiValueConverter)? – Clemens Jul 29 '17 at 06:40
  • @AQuirky It was a mistake. Updated. Replaced IMultiValueConverter by IValueConverter. Column is readonly because data is only for presentation It cannot be modified. My listview by default is readonly, only data is displayed for presentation, I do not introduce any code in listview to make it editable. In fact when I execute my app you cannot edit any cell. – Astro Jul 29 '17 at 08:20
  • @Clemens No, I didnt'. I do not want to make extra work. Before making any implementation I want to be sure which is the best way to do it, then when I decide which is the best option, I'll try. Here I am asking for you, which is the best option, using a converter in binding or using some king of trigger, datatrigger. It was a mistake putting here IMultiValueConverter, I have changed it. – Astro Jul 29 '17 at 08:23
  • @Clemens also My doubt if I decide to implement the binding with the converter is the following: User can select multiple listviewitems (rows), then once selected, he/she click on a button "delete" to delete them. Then a delete method is carried out. This method consists on read all the current selected listviewitems (rows) in the listview, store them in a some kind of data structure, for example, a datatable, and pass it to a store procedure which process all the listviewitems in the datatable and remove them from the database table. So when reading the each selected... – Astro Jul 29 '17 at 08:26
  • @Clemens ... listviewitem (I iterate over them) before sending the datatable to the stored procedure, I would read the value column "Employee Type" (among other columns) for the current listview item in the iteration. Then when reading the "Employee Type" column I would need to convert the displayed item in the listview back to its original value, so at this point, Will I l need to call convertBack method in the converter class to obtain the original value? Is this the correct way to proceed? – Astro Jul 29 '17 at 08:54
  • @Clemens As far as I know convertback method in converter class would only be called by the control in the view if it has Mode set to "TwoWay", so In my case it will not be called since column is readonly so I suppose I would need to call convertback manually when iterating over each selected item in the listview previous to store it in the datatable and then pass it through a parameter to the stored procedure. Are all my assumptions correct? or Am I wrong in something? – Astro Jul 29 '17 at 08:54
  • Now you're asking question upon question in comments. I'd recommend that you try something yourself and come back when you have specific problems with your approach. Making "extra work" is sometimes necessary to get an own idea of how things work. – Clemens Jul 29 '17 at 11:18

0 Answers0