WPF新手:更新文本框值

本文关键字:文本 更新 新手 WPF | 更新日期: 2023-09-27 17:53:29

我绑定了一个从INotifyPropertyChange派生的类到一个数据上下文。在一些交互之后,将计算一个值并更新输出属性。我的问题是结果文本框根本没有更新。

public partial class setraSubWpfTolerance : UserControl
{
        public setraFit objSource = new setraFit();
        public setraSubWpfTolerance()
        {
            InitializeComponent();
            this.DataContext = objSource;
        }
}

和class:

public class setraFit : INotifyPropertyChanged
{          
      private readonly CollectionView _BoreSystems;
      public CollectionView BoreSystems
      {
        get { return _BoreSystems; }
      }
      private decimal? _MaxBoreDimension;
      public decimal? MaxBoreDimension 
      {
        get { return _MaxBoreDimension; }
        set
        {
            if (_MaxBoreDimension == value) return;
            _MaxBoreDimension = value;
            onPropertyChanged("MaxBoreDimension");
        }   
      }
      private string _BoreSystem;
      public string BoreSystem
      {
            get { return _BoreSystem; }
            set
            {
                if (_BoreSystem == value) return;
                _BoreSystem = value;
                calcBoreDimension();
                onPropertyChanged("BoreSystem");                
            }
      }
    public setraFit()
    {
        IList<string> listBore = setraStaticTolerance.getBoreList();
        _BoreSystems = new CollectionView(listBore);
    }
    public event PropertyChangedEventHandler PropertyChanged;                
    private void onPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    private void calcBoreDimension()
    {
        _MaxBoreDimension = (decimal)100.035;
    }
}
最后但并非最不重要的XAML
    <UserControl x:Class="SetraSubForms.setraSubWpfTolerance"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="375">    
        <Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="194,10,0,0" Name="BoreSystemComboBox" VerticalAlignment="Top" Width="120"
                                      ItemsSource="{Binding Path=BoreSystems}" 
                                      SelectedValue="{Binding Path=BoreSystem}"/>
          <TextBox HorizontalAlignment="Left" Margin="194,67,0,37" Name="MaxDimBoreTextBox" Width="120" IsReadOnly="False"
                                     Text="{Binding Path=MaxBoreDimension, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
        </Grid>
    </UserControl>

我希望在更改组合框后收到哑值100.035,但文本框没有更新。如果我一步一步地运行,我可以看到setafit的"MaxBoreDimension"属性被改变了。

我做错了什么?

提前感谢你的帮助sittingDuck

WPF新手:更新文本框值

你的方法正在更新私有值,而不是属性:

private void calcBoreDimension()
{
    _MaxBoreDimension = (decimal)100.035;
}

改为

private void calcBoreDimension()
{
    MaxBoreDimension = (decimal)100.035;
}

你在构造函数中做同样的事情,这导致你的calcBoreDimension方法不能运行:

public setraFit()
{
    IList<string> listBore = setraStaticTolerance.getBoreList();
    _BoreSystems = new CollectionView(listBore);
}
应该

public setraFit()
{
    IList<string> listBore = setraStaticTolerance.getBoreList();
    BoreSystems = new CollectionView(listBore); //this line!
}

当您创建指向私有字段的属性时,您几乎不必在属性之外的任何地方设置私有字段。这就是属性存在的原因——这样无论何时获得或设置它们,您都可以在getset块中运行代码,而不仅仅是检索当前值。

解决!关键是为"MaxBoreDimension"启动PropertyChanged事件

public decimal? NominalDimension
        {
            get { return _NominalDimension; }
            set
            {
                if (_NominalDimension == value) return;
                _NominalDimension = value;
                calcBoreDimension();
                onPropertyChanged("NominalDimension");
                onPropertyChanged("MaxBoreDimension");
            }
        }

感谢DLeh的贡献