user2799180
user2799180

Reputation: 749

Use IValueConverter within WPF DataGrid depending on several values of the same row

I'm working on a WPF project following the MVVM pattern. I do have the following Model:

Name...¦.Value.¦.Unit.¦.Type.¦.Min.¦.Max.¦

Voltage¦.....3.....¦...mV..¦....X....¦...0...¦....5....¦

Current¦.....1.....¦...mA..¦....Y....¦...2...¦....7....¦

This is what the datagrid shows:

Name...¦.Value.¦.Unit.¦

Voltage¦....3.....¦.mV..¦

Current¦....1.....¦.mA..¦

Templates(XAML):

<DataTemplate x:Key="NumTemplate">
        <wpfToolkit:IntegerUpDown Value="{Binding Value, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource StimVoltageConverter},ConverterParameter=XXX}" />
    </DataTemplate>

    <DataTemplate x:Key="ComboboxTemplate">
        <ComboBox ItemsSource="{Binding Path=XXX}" 
DisplayMemberPath="Name" 
SelectedValuePath="Value" 
SelectedValue="{Binding Value, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>

DataGrid(XAML):

<DataGrid ItemsSource="{Binding FixParaCollectionView}"  AutoGenerateColumns="False">
    <DataGrid.Columns>
          <DataGridTextColumn IsReadOnly="True" Header="Name" Binding="{Binding Id}"/>
          <DataGridTemplateColumn Header="Value">
              <DataGridTemplateColumn.CellTemplateSelector>
                  <helper:TemplateSelector ComboboxTemplate="{StaticResource ComboboxTemplate}" NumTemplate="{StaticResource NumTemplate}"/>
              </DataGridTemplateColumn.CellTemplateSelector>
           </DataGridTemplateColumn>
           <DataGridTextColumn Header="Unit" Binding="{Binding Unit,NotifyOnTargetUpdated=True}">
           </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

StimVoltageConverter : IValueConverter:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int val = (int)value;

        return (int)(val / 0.41);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int val = (int)value;
        return (int)(val * 0.41);
    }

I would like to do 2 things now:

  1. Convert values (e.g. Value*0.41 if Type==X) based on another value of the same row, like "Name" or "Type"

  2. I want to validate the values written into the datagrid (by the user) (e.g. validate min/max value from the model)

I added a converter to the "NumTemplate" template.

Is it a good idea to do this using converter/validate? I really would like to implement this into a converter because it is nicely separated from all the other logic.

Thanks in advance.

Upvotes: 0

Views: 1495

Answers (1)

Lee Louviere
Lee Louviere

Reputation: 5262

For validating, you want to use the validate ability on the property binding.

As far as calculated values, I usually avoid using dependency properties if I have a calculated value. Usually preferring INotifyPropertyChanged. int Value { get { computation...; } set { reverse computation...; PropertyChanged("Value");}}

You can have a propertychanged notification on the parent values of the dependent, and PropertyChanged(new PropertyChangedArgs("Value"));

In fact, I usually avoid DependencyProperties on viewmodels.

Upvotes: 0

Related Questions