0

I would like to know if there is any possibility to use a property of an object bounded to the Datagrid in ConverterParameter , this is the xaml code :

<DataGrid ItemsSource="{Binding Students}" AutoGenerateColumns="False"  CanUserAddRows="False" CanUserDeleteRows="False" CanUserSortColumns="False" >
    <DataGrid.Columns >
        <DataGridTextColumn  x:Name="Id_Column"  Header="{StaticResource PersonId}"   
                                          Binding="{Binding Id}" IsReadOnly="True" FontSize="11"     FontWeight="Normal"  Width="Auto" MinWidth="60" />
        <DataGridTextColumn  x:Name="NameColumn" Header="{DynamicResource Name}"    Binding="{Binding Name}" IsReadOnly="True" 
                                         FontSize="11"  FontWeight="Normal"  MinWidth="130" Width="*" />
        <DataGridTemplateColumn  x:Name="Average"  Header="{DynamicResource Average}"  IsReadOnly="False"  Width="*" >
            <DataGridTemplateColumn.CellTemplate >
                <DataTemplate>
                    <TextBox Text="{Binding Path=AvgValue,Converter={StaticResource DoubleConverter, ConverterParameter = FailProp},Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="Auto"  Height="28" IsEnabled="True" >
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

And This is the class that I'm using:

 public class Student : INotifyPropertyChanged
    
   {
        private int _personId;

        void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }


        public int PersonId
        {

            get { return _personId; }
            set
            {
                _personId= value;
                OnPropertyChanged("PersonId");
            }
        }

        // ....

        private int _failprop;

        public int FailProp
        {

            get { return _failprop; }
            set
            {
                _failprop= value;
                OnPropertyChanged("FailProp");
            }
        }
   }

Using this code , I'm getting an exception :

'Binding' can only be set on a DependencyProperty of a DependencyObject.".

How can I fix this issue?

abdou93
  • 167
  • 10

1 Answers1

0

Use an IMultiValueConverter and a MultiBinding to bind to both AvgValue and FailProp:

public class DoubleConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        ...
        double avgValue = (double)values[0];
        int failProp = (int)values[1];
        ...
        return avgValue;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) =>
        throw new NotSupportedException();
}

XAML:

<TextBox Width="Auto" Height="28" IsEnabled="True">
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource DoubleConverter}" UpdateSourceTrigger="PropertyChanged">
            <Binding Path="AvgValue" />
            <Binding Path="FailProp" />
        </MultiBinding>
    </TextBox.Text>
</TextBox>
mm8
  • 163,881
  • 10
  • 57
  • 88