【问题标题】:Update another cell in the same row after editing inside DataGrid在 DataGrid 内编辑后更新同一行中的另一个单元格
【发布时间】:2018-12-06 02:03:14
【问题描述】:

我有两个文本框 Discount(%) 和 Price(€)。 价格是只读。 DataGrid 中的 Row 是通过名为 MyData 的类构建的,并且 Discount 绑定到值 discount, 价格到价格。当我更改 Discount 的百分比值时,它应该计算新价格并将源值 price 更改为新价格。但它不会更新文本框 price。我需要调用什么来触发目标更新?

Xaml:

<DataGridTemplateColumn Header="Discount(%)" Width="0.5*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox Name="tbDiscount" Text="{Binding Path=discount, Mode=TwoWay}" TextChanged="tbDiscount_TextChanged"></TextBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Price (€)" Width="0.5*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox Name="tbPrice" Text="{Binding Path=price, Mode=TwoWay}" IsReadOnly="True"></TextBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

C#:

我的数据:

public class MyData
{
    public decimal price { set; get; }
    public int discount { get; set; }
}

如何将行添加到 DataGrid

dataRow.discount = 0;
dataRow.price = price;
dataGridBasket.Items.Add(dataRow);

我如何尝试更改 TextBox Discount 中 textvalueChanged 上的 price 值:

private void tbDiscount_TextChanged(object sender, TextChangedEventArgs e)
{
    var text = sender as TextBox;

    text.GetBindingExpression(TextBox.TextProperty).UpdateSource();

    foreach(MyData item in dataGridBasket.Items)
    {
        item.price = item.price - (item.price * (Convert.ToDecimal(item.discount) / 100));
    }
}

【问题讨论】:

  • 改变价格的逻辑应该在discountsetter的MyData类中。在那里设置新的price 值并在price setter 中引发 PropertyChangedEvent
  • 另外:为什么 tbDiscount_TextChanged 更新所有 dataGridBasket.Items 时仅更改 1 个项目的折扣?
  • @ASh 因为我不知道它是如何工作的。 DataBinding 的新手...我不知道如何获取其他特定的文本框,因此我尝试仅更新每个文本框。还是谢谢你
  • @ASh 这个 setter 方法会是什么样子?

标签: c# wpf xaml data-binding datagrid


【解决方案1】:

改变价格的逻辑应该在discount setter 中的MyData 类中。在那里设置新的price 值并在price setter 中提高PropertyChangedEvent。

您还需要在某些字段/属性中存储原始价格以正确计算折扣价。

public class MyData : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private decimal _basePrice;
    public decimal basePrice
    {
       get { return _basePrice; }
       set { _basePrice = value;  UpdatePrice(); }
    }

    private decimal _price;
    public decimal price
    {
       get { return _price; }
    }

    private int _discount;
    public int discount
    {
       get { return _discount; }
       set { _discount = value; UpdatePrice(); }
    }

    private void UpdatePrice()
    {
         _price = _basePrice * (100 - _discount) * 0.01M;
        OnPropertyChanged("price");
    } 
}

tbDiscount_TextChanged 变得多余,DataGrid 列定义可以简化:

<DataGridTemplateColumn Header="Discount(%)" Width="0.5*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=discount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Price (€)"
                    Width="0.5*"
                    Binding="{Binding Path=price}"
                    IsReadOnly="True" />

【讨论】:

  • 谢谢!不幸的是,它无法正常工作。一开始我设置了baseprice。在 XAML 中,我将 textValueChanged 上的绑定设置为折扣。但是我遵循失败消息:未处理的异常:System.Windows.Markup.XamlParseException:来自““System.Windows.Data.Binding”的评估程序的“51”行和“108”行导致异常。”。 ---> System.InvalidCastException:“System.Reflection.RuntimeEventInfo”类型的对象无法在“System.Reflection.MethodInfo”中转换。
  • 抱歉来早了。见上文:)
  • TextChanged="tbDiscount_TextChanged" 也可以从 xaml 中删除,因为我们不再需要该事件处理程序
  • 是的,我删除了它。抱歉拼错了,它是第 51 行和第 108 位...尽管如此,它还是遵循 xaml 行: 当我不使用“TextChange”时,它什么也不做。我应该删除“文本”吗?
  • @flyingAlemannian,检查编辑。无法为属性price 创建双向绑定,因为它现在没有设置器。还要注意 UpdateSourceTrigger=PropertyChanged 在 discount 绑定中。它允许在每次更改后更新price
猜你喜欢
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多