Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# datagrid WPF中的计算列_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# datagrid WPF中的计算列

C# datagrid WPF中的计算列,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个应用程序,在WPF中有一个数据网格,带有C#,我有4列,我需要这4列是相对于其他列计算的。我使用了IValueConverter,它工作得很好,但是当我更改行时,只计算第四列的值,当我更改单元格2和单元格3时,我需要它,更改第四列 这是我的XAML: <Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我有一个应用程序,在WPF中有一个数据网格,带有C#,我有4列,我需要这4列是相对于其他列计算的。我使用了IValueConverter,它工作得很好,但是当我更改行时,只计算第四列的值,当我更改单元格2和单元格3时,我需要它,更改第四列

这是我的XAML:

<Window x:Class="WpfApplication6.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication6"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid Name="grid">
        <DataGrid.Resources>
            <local:CalculatingConverter x:Key="CalculatingConverter" />
        </DataGrid.Resources>
        <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" />
        <DataGridTextColumn Binding="{Binding Count}"/>
        <DataGridTextColumn Binding="{Binding Price}" />
        <DataGridTextColumn 
                Header="Total"                  
                Binding="{Binding Converter={StaticResource CalculatingConverter}}"
                />
    </DataGrid.Columns>
    </DataGrid>

</Grid>

我该怎么做呢?

您必须使用
IValueConverter
进行此操作。使用第一列作为源(将第二列绑定到第一列),并使用转换器转换值


.

为此,您必须使用
IValueConverter
。使用第一列作为源(将第二列绑定到第一列),并使用转换器转换值


.

您的
类需要实现
INotifyPropertyChanged
并在其任何属性更改时引发
PropertyChanged
事件

问题是绑定无法知道需要监视哪些属性以进行更改。一个解决方案是将转换器更改为
IMultiValueConverter
,并将该绑定更改为一个
MultiBinding
,它显式地绑定
Price
Count

您的另一个选项(这是一个简单得多的选项)是将只读的
TotalPrice
属性添加到
项目
类中。这就是我要做的

public decimal TotalPrice {
    get { return Price * Count; }
}
然后,在
Price
属性中,当
Price
更改时,您将为
TotalPrice
以及
Price
引发
PropertyChanged
事件。对于
Count
,您也可以这样做

然后您可以在XAML中非常简单地绑定到
TotalPrice
,而无需任何转换器。您可能需要使绑定
Mode=one-way
,因为它是只读的


如果您需要更多的代码示例,请告诉我

您的
类需要实现
INotifyPropertyChanged
并在其任何属性更改时引发
PropertyChanged
事件

问题是绑定无法知道需要监视哪些属性以进行更改。一个解决方案是将转换器更改为
IMultiValueConverter
,并将该绑定更改为一个
MultiBinding
,它显式地绑定
Price
Count

您的另一个选项(这是一个简单得多的选项)是将只读的
TotalPrice
属性添加到
项目
类中。这就是我要做的

public decimal TotalPrice {
    get { return Price * Count; }
}
然后,在
Price
属性中,当
Price
更改时,您将为
TotalPrice
以及
Price
引发
PropertyChanged
事件。对于
Count
,您也可以这样做

然后您可以在XAML中非常简单地绑定到
TotalPrice
,而无需任何转换器。您可能需要使绑定
Mode=one-way
,因为它是只读的


如果您需要更多的代码示例,请告诉我

最后感谢Ed Plunkett,我在我的
项目
类和
Price
Count
中实现了
INotifyPropertyChanged
,并强制调用
OnPropertyChanged(“TotalPrice”)
,效果非常好:

public class Item : INotifyPropertyChanged
{
    private string name;
    private decimal price;
    private int count;

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public string Name {            
        get
        {
            return this.name;
        }

        set
        {
            if (value != name)
            {
                name = value;
                OnPropertyChanged();
            }
        }
    }
    public decimal Price
    {
        get
        {
            return price;
        }

        set
        {
            if (value != this.Price)
            {
                price = value;
                OnPropertyChanged();
                OnPropertyChanged("TotalPrice");
            }
        }
    }
    public int Count {
        get
        {
            return count;
        }

        set
        {
            if (value != count)
            {
                count = value;
                OnPropertyChanged();
                OnPropertyChanged("TotalPrice");
            }
        }
    }
    public decimal TotalPrice
    {
        get { return Price * Count;  }
    }
}

最后,感谢Ed Plunkett,我在我的
项目
类和
价格
计数
中实现了
INotifyPropertyChanged
,并强制调用
OnPropertyChanged(“TotalPrice”)
,效果非常好:

public class Item : INotifyPropertyChanged
{
    private string name;
    private decimal price;
    private int count;

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public string Name {            
        get
        {
            return this.name;
        }

        set
        {
            if (value != name)
            {
                name = value;
                OnPropertyChanged();
            }
        }
    }
    public decimal Price
    {
        get
        {
            return price;
        }

        set
        {
            if (value != this.Price)
            {
                price = value;
                OnPropertyChanged();
                OnPropertyChanged("TotalPrice");
            }
        }
    }
    public int Count {
        get
        {
            return count;
        }

        set
        {
            if (value != count)
            {
                count = value;
                OnPropertyChanged();
                OnPropertyChanged("TotalPrice");
            }
        }
    }
    public decimal TotalPrice
    {
        get { return Price * Count;  }
    }
}

您知道这些列中的值来自哪里吗?你能澄清一下“立即执行或更改第一列”是什么意思吗?我需要知道,当我在第一列中更改值时,第二列也会更改值。你知道值来自哪里吗?您是如何将它们放入网格的?在上面的代码中,源代码是如何放置的?您知道这些列中的值来自何处吗?你能澄清一下“立即执行或更改第一列”是什么意思吗?我需要知道,当我在第一列中更改值时,第二列也会更改值。你知道值来自哪里吗?你是如何把它们放在网格中的?在上面的代码中是放置源代码的