繁体   English   中英

WPF新手:更新文本框值

[英]WPF Newbie: updating textbox value

我将派生自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;
    }
}

最后但并非最不重要的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的虚拟值,但文本框未更新。 如果我逐步运行,则可以看到setraFit的“ MaxBoreDimension”属性已更改。

我做错了什么?

预先感谢您的帮助

您的方法是更新私有值,而不是Property:

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的贡献。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM