【问题标题】:How can I use property of an object in Converter C# WPF?如何在 Converter C# WPF 中使用对象的属性?
【发布时间】:2022-01-19 09:02:42
【问题描述】:

我想知道是否有可能在 ConverterParameter 中使用绑定到 Datagrid 的对象的属性,这是 xaml 代码:

<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>

这是我正在使用的类:

 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");
            }
        }
   }

使用此代码,我得到一个异常:

'Binding' 只能在 DependencyObject 的 DependencyProperty 上设置。"。

我该如何解决这个问题?

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

使用IMultiValueConverterMultiBinding 绑定AvgValueFailProp

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    相关资源
    最近更新 更多