简体   繁体   English

WPF数据绑定无法正常工作

[英]WPF data binding not properly working

Sorry if this is hard to read but I can't figure it out. 抱歉,如果很难看懂,但我不知道。 I think the problem is with my xaml but I have no idea. 我认为问题出在我的xaml,但我不知道。 All I want it to do is to display 180 in the weight text box and 5 in the height text box. 我要做的就是在重量文本框中显示180,在高度文本框中显示5。 Thanks for any advice and let me know if more info is needed. 感谢您的任何建议,如果需要更多信息,请告诉我。

This is my MainWindow.xamml.cs 这是我的MainWindow.xamml.cs

namespace Simple_BMI.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }
}

This is my MinWindow.xaml 这是我的MinWindow.xaml

<Window x:Class="Simple_BMI.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
            <Label>Weight: </Label>
            <TextBox Text="{Binding Model.Weight}" Width="136" />
            <Button>Update</Button>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label>Height: </Label>
            <TextBox Text="{Binding Model.Height}" Width="136" />
        </StackPanel>
    </StackPanel>
</Window>

My model: 我的模特:

namespace Simple_BMI.Models
{
    public class Model : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public Model(double weight, double height)
        {
            Weight = weight;
            Height = height;
        }

        private double _Weight;
        public double Weight
        {
            get
            {
                return _Weight;
            }
            set
            {
                _Weight = value;
                OnPropertyChanged("Weight");
            }
        }

        private double _Height;
        public double Height
        {
            get
            {
                return _Height;
            }
            set
            {
                _Height = value;
                OnPropertyChanged("Height");
            }
        }

        private void OnPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;          
            if (handler != null)                                           
            {
                handler(this, new PropertyChangedEventArgs(propertyName)); 
            }
        }
    }
}

And my view model: 而我的视图模型:

namespace Simple_BMI.ViewModels
{
    class ViewModel
    {
        public ViewModel()
        {
            _Measurement = new Model(180, 6);
        }

        private Model _Measurement;
        public Model Measurement
        {
            get
            {
                return _Measurement;
            }
        }
        public void SaveChanges()
        {
            Debug.Assert(false, String.Format("{0} {1} was updated.", Measurement.Weight, Measurement.Height));
        }
    }
}

Replace {Binding Path=Model. 替换{Binding Path=Model. with {Binding Path=Measurement. {Binding Path=Measurement. . You have the property named differently on ViewModel 您在ViewModel不同的名称命名

See also: Debugging Data Bindings in a WPF or Silverlight Application 另请参见: 在WPF或Silverlight应用程序中调试数据绑定

Correct your xaml file, using Measurement instead of Model : 使用Measurement而不是Model来更正您的xaml文件:

<StackPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
        <Label>Weight:</Label>
        <TextBox Text="{Binding Measurement.Weight}" Width="136" />
        <Button>Update</Button>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label>Height:</Label>
        <TextBox Text="{Binding Measurement.Height}" Width="136" />
    </StackPanel>
</StackPanel>

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

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