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

WPF双向绑定未按预期工作问题求助

解决WPF TwoWay绑定失效及自动计算字段的问题

看起来你遇到的核心问题有两个:TwoWay绑定未正常同步数据,以及依赖字段无法自动更新。下面一步步帮你解决:


1. 根源:模型未实现INotifyPropertyChanged接口

WPF的TwoWay绑定依赖INotifyPropertyChanged接口来同步UI和数据模型的变化。你的InregistrareModel类只是简单的自动属性,没有属性变更通知机制——这会导致:

  • UI输入的内容无法正确触发模型属性更新(或者更新后无法通知其他依赖逻辑)
  • 模型内部计算的字段(比如CantitateMarfa)无法自动刷新到UI

修复方案:改造InregistrareModel

让模型继承INotifyPropertyChanged,给每个属性添加变更通知,同时增加依赖字段的自动计算逻辑:

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class InregistrareModel : INotifyPropertyChanged
{
    // 基础字段
    private double _cantitateInitiala;
    private double _cantitateScazuta;
    private double _cantitateMarfa;
    private int _id;
    private int _idProdus;
    private int _idFurnizor;
    private string _numeProdus;
    private string _numeFurnizor;
    private string _nrAuto;
    private string _numeSofer;
    private double _cantitateIesire;
    private double _umiditate;
    private DateTime _dataIntrare;
    private DateTime _dataIesire;
    private FurnizorModel _furnizor;
    private int _nir;

    // 变更通知事件
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    // 带通知的属性(以关键属性为例,其余属性同理改造)
    public double CantitateInitiala
    {
        get => _cantitateInitiala;
        set
        {
            if (value != _cantitateInitiala)
            {
                _cantitateInitiala = value;
                NotifyPropertyChanged();
                CalculateCantitateMarfa(); // 变更时自动计算Net值
            }
        }
    }

    public double CantitateScazuta
    {
        get => _cantitateScazuta;
        set
        {
            if (value != _cantitateScazuta)
            {
                _cantitateScazuta = value;
                NotifyPropertyChanged();
                CalculateCantitateMarfa(); // 变更时自动计算Net值
            }
        }
    }

    public double CantitateMarfa
    {
        get => _cantitateMarfa;
        private set
        {
            if (value != _cantitateMarfa)
            {
                _cantitateMarfa = value;
                NotifyPropertyChanged();
            }
        }
    }

    // 其余属性按相同模式改造(Id、IdProdus等)
    public int Id
    {
        get => _id;
        set
        {
            if (value != _id)
            {
                _id = value;
                NotifyPropertyChanged();
            }
        }
    }

    // ... 此处省略其他属性的get/set实现,全部参照上面的模式改造 ...

    // 计算Net值的业务逻辑(根据你的需求调整)
    private void CalculateCantitateMarfa()
    {
        // 示例逻辑:Net = 初始数量 - 扣减数量
        CantitateMarfa = CantitateInitiala - CantitateScazuta;
    }
}

2. 优化窗口绑定细节

(1)修正时间格式错误

你XAML中DataIesire的StringFormat写错了,HH:HH会重复显示小时,改成HH:mm才是正确的时分格式:

<TextBox Grid.Row="7" Grid.Column="3" Text="{Binding InregistrareModel.DataIesire,StringFormat='{}{0:HH:mm dd/M/yyyy}'}"/>

(2)明确绑定模式(可选但更清晰)

虽然TextBox.Text的默认绑定模式就是TwoWay,但显式声明可以让代码更易读:

<TextBox Grid.Row="2" Grid.Column="3" Text="{Binding InregistrareModel.CantitateInitiala, Mode=TwoWay}"/>

3. 验证数据同步

改造完成后:

  1. 打开窗口时,模型数据会正常填充到UI
  2. 你在txtCantitateScazuta中输入值时,CantitateMarfa会自动计算并同步到对应的TextBox
  3. 当你需要更新数据库时,直接使用窗口的InregistrareModel属性即可——TwoWay绑定已经把UI的输入同步到了模型中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 09:13:15