简体   繁体   English

在WPF DataGrid中使用IValueConverter,具体取决于同一行的多个值

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

I'm working on a WPF project following the MVVM pattern. 我正在按照MVVM模式进行WPF项目。 I do have the following Model: 我有以下模型:

Name...¦.Value.¦.Unit.¦.Type.¦.Min.¦.Max.¦ 名称...值。单位。类型。最小。最大。最大

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

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

This is what the datagrid shows: 这是数据网格显示的内容:

Name...¦.Value.¦.Unit.¦ 名称... ¦.Value.¦.Unit.¦

Voltage¦....3.....¦.mV..¦ 电压¦ .... 3 ..... ¦.mV..¦

Current¦....1.....¦.mA..¦ 电流..... 1 ....... mA..¦

Templates(XAML): 模板(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(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: 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 (eg Value*0.41 if Type==X) based on another value of the same row, like "Name" or "Type" 根据同一行的另一个值(例如“名称”或“类型”)转换值(例如,如果Type == X,则为Value * 0.41)

  2. I want to validate the values written into the datagrid (by the user) (eg validate min/max value from the model) 我想验证(由用户)写入数据网格的值(例如,验证模型的最小/最大值)

I added a converter to the "NumTemplate" template. 我在“ NumTemplate”模板中添加了一个转换器。

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. 提前致谢。

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. 通常更喜欢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")); 您可以在依赖项的父值和PropertyChanged(new PropertyChangedArgs("Value"));上进行propertychanged通知PropertyChanged(new PropertyChangedArgs("Value"));

In fact, I usually avoid DependencyProperties on viewmodels. 实际上,我通常避免在视图模型上使用DependencyProperties。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM