You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将DataGrid列值绑定到外部TextBox并实现FT AMNT价格动态更新?

实现DataGrid的Rate值与外部TextBox动态关联的方案

核心思路是利用WPF的数据绑定机制+INotifyPropertyChanged接口,让属性变化时自动通知UI更新。下面是具体的实现步骤,你可以根据自己的业务逻辑调整细节:

1. 定义带属性变更通知的实体类

首先你的数据模型(比如命名为ProductItem)需要实现INotifyPropertyChanged接口,确保修改Rate属性时能触发UI更新,同时添加计算属性来生成FT AMNT的值(这里假设FT AMNT是Rate乘以PCS数量,你可以替换成自己的计算逻辑):

public class ProductItem : INotifyPropertyChanged
{
    private int _pcs;
    private decimal _rate;

    public int Pcs
    {
        get => _pcs;
        set
        {
            _pcs = value;
            OnPropertyChanged(nameof(Pcs));
            OnPropertyChanged(nameof(FtAmnt)); // 数量变化也同步更新FT AMNT
        }
    }

    public decimal Rate
    {
        get => _rate;
        set
        {
            _rate = value;
            OnPropertyChanged(nameof(Rate));
            OnPropertyChanged(nameof(FtAmnt)); // Rate修改时触发FT AMNT更新
        }
    }

    // 计算FT AMNT的只读属性,逻辑可自定义
    public decimal FtAmnt => Rate * Pcs;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

2. 配置ViewModel与UI绑定

根据你需要展示的FT AMNT类型(单选中行/所有行总和),分两种情况处理:

情况1:显示DataGrid选中行的FT AMNT

如果TextBox要展示当前选中行的FT AMNT,XAML和ViewModel可以这么写:

XAML代码

<!-- DataGrid部分 -->
<DataGrid x:Name="dgItems" ItemsSource="{Binding ProductItems}" 
          SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="PCS" Width="40" Binding="{Binding Pcs, UpdateSourceTrigger=PropertyChanged}"/>
        <DataGridTextColumn Header="RATE" Width="60" Binding="{Binding Rate, UpdateSourceTrigger=PropertyChanged}"/>
        <!-- 其他业务列 -->
    </DataGrid.Columns>
</DataGrid>

<!-- 外部TextBox -->
<TextBox Header="FT AMNT" Text="{Binding SelectedItem.FtAmnt, Mode=OneWay}" IsReadOnly="True"/>

ViewModel补充代码

ViewModel需要实现INotifyPropertyChanged,并管理数据集合和选中项:

public class MainViewModel : INotifyPropertyChanged
{
    public ObservableCollection<ProductItem> ProductItems { get; set; }
    private ProductItem _selectedItem;

    public ProductItem SelectedItem
    {
        get => _selectedItem;
        set
        {
            _selectedItem = value;
            OnPropertyChanged(nameof(SelectedItem));
        }
    }

    public MainViewModel()
    {
        ProductItems = new ObservableCollection<ProductItem>();
        // 可以在这里初始化测试数据
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

情况2:显示所有行的FT AMNT总和

如果TextBox要展示所有行的FT AMNT总和,需要在ViewModel里添加总和计算属性,并监听集合内项的变化:

ViewModel补充代码

public class MainViewModel : INotifyPropertyChanged
{
    public ObservableCollection<ProductItem> ProductItems { get; set; }

    // 计算所有行的FT AMNT总和
    public decimal TotalFtAmnt => ProductItems.Sum(item => item.FtAmnt);

    public MainViewModel()
    {
        ProductItems = new ObservableCollection<ProductItem>();
        ProductItems.CollectionChanged += ProductItems_CollectionChanged;
    }

    // 监听集合内项的属性变化,更新总和
    private void ProductItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (ProductItem item in e.NewItems)
            {
                item.PropertyChanged += Item_PropertyChanged;
            }
        }
        if (e.OldItems != null)
        {
            foreach (ProductItem item in e.OldItems)
            {
                item.PropertyChanged -= Item_PropertyChanged;
            }
        }
    }

    private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(ProductItem.FtAmnt))
        {
            OnPropertyChanged(nameof(TotalFtAmnt));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

对应XAML代码

<TextBox Header="Total FT AMNT" Text="{Binding TotalFtAmnt, Mode=OneWay}" IsReadOnly="True"/>

3. 关键注意事项

  • 绑定Rate和Pcs时,一定要设置UpdateSourceTrigger=PropertyChanged,这样在DataGrid里修改值时会立即同步到数据源,而不是等到失去焦点。
  • 如果FT AMNT是只读计算值,给TextBox设置IsReadOnly="True"避免用户手动修改。
  • 确保ViewModel和实体类都正确实现INotifyPropertyChanged,否则属性变化无法通知UI更新。

内容的提问来源于stack exchange,提问作者Eddie Yoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:48:38