C# WPF新手:更新文本框值

C# WPF新手:更新文本框值,c#,wpf,xaml,C#,Wpf,Xaml,我将从INotifyPropertyChange派生的类绑定到Datacontext。 在一些交互之后,将计算一个值并更新输出属性。 我的问题是结果文本框根本没有更新 public partial class setraSubWpfTolerance : UserControl { public setraFit objSource = new setraFit(); public setraSubWpfTolerance() {

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

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

        }
}
班级:

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;
    }
}
public类setraFit:INotifyPropertyChanged
{          
私有只读收集视图系统;
公共集合视图系统
{
获取{return}
}
私有十进制?\u MaxBoreDimension;
公共十进制?MaxBoreDimension
{
获取{return\u MaxBoreDimension;}
设置
{
if(_MaxBoreDimension==值)返回;
_MaxBoreDimension=值;
onPropertyChanged(“MaxBoreDimension”);
}   
}
私有字符串系统;
公共字符串系统
{
获取{return}
设置
{
if(_peresystem==值)返回;
_系统=数值;
calcBoreDimension();
不动产变更(“管道系统”);
}
}
公共设置
{
IList listBore=setraStaticTolerance.getBoreList();
_钻孔系统=新的采集视图(列表钻孔);
}
公共事件属性更改事件处理程序属性更改;
私有void onPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
私有void calcBoreDimension()
{
_MaxBoreDimension=(十进制)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>

在更改combobox后,我希望收到100.035的伪值,但textbox没有更新。如果我一步一步地运行,我可以看到setraFit的“MaxBoreDimension”属性已更改

我做错了什么

提前谢谢你的帮助
sittingDuck

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

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 listBore=setraStaticTolerance.getBoreList();
_钻孔系统=新的采集视图(列表钻孔);
}
应该是

public setraFit()
{

    IList<string> listBore = setraStaticTolerance.getBoreList();
    BoreSystems = new CollectionView(listBore); //this line!
}
public setraFit()
{
IList listBore=setraStaticTolerance.getBoreList();
BoreSystems=newcollectionview(listBore);//这一行!
}
创建指向专用字段的属性时,几乎不必将专用字段设置在属性以外的任何位置。这就是属性存在的原因-因此,无论何时获取或设置属性,都将在
get
set
块中运行代码,而不仅仅是检索当前值。

已解决! 关键是为“MaxBoreDimension”初始化PropertyChanged事件


感谢DLeh的贡献。

此外,我认为值得一提的是,您可能希望将变量大小写约定规范化为MSDN。通常这只是一种偏好,我通常不会对此发表评论,但您的问题实际上源自您的命名约定。谢谢你的意见。但正如我之前所说,我看到了调试模式的价值。如果我使用setraFit sf=DataContext作为setraFit进行提取,我可以得到该值。我的问题是我看不到WPF中的值。您在构造函数中也在做同样的事情。编辑我的回答几个月前我开始使用WPF,从代码审查的角度来看,您可能会发现其他一些东西很有用:当绑定到值时,您不必指定
Path=
。您可以说:
Text=“{Binding MaxBoreDimension}”
,它知道MaxBoreDimension是路径。此外,您不必为文本框指定
Mode=TwoWay
UpdateSourceTrigger=PropertyChanged
,因为默认情况下已经设置了这些属性(实际上,文本框的焦点设置为LoseFocus,因此不会在键入每个字符后运行calc)
public decimal? NominalDimension
        {
            get { return _NominalDimension; }
            set
            {
                if (_NominalDimension == value) return;
                _NominalDimension = value;
                calcBoreDimension();
                onPropertyChanged("NominalDimension");
                onPropertyChanged("MaxBoreDimension");
            }
        }