WPF数据绑定无法正常工作

本文关键字:工作 常工作 数据绑定 WPF | 更新日期: 2023-09-27 18:00:16

如果这很难阅读,我很抱歉,但我想不通。我想问题出在我的xaml上,但我不知道。我只想在重量文本框中显示180,在高度文本框中显5。谢谢你的建议,如果需要更多信息,请告诉我。

这是我的MainWindow.xamml.cs

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

这是我的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>

我的型号:

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)); 
            }
        }
    }
}

我的视图模型:

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));
        }
    }
}

WPF数据绑定无法正常工作

{Binding Path=Model.替换为{Binding Path=Measurement.。您在ViewModel 上有不同名称的属性

另请参阅:调试WPF或Silverlight应用程序中的数据绑定

使用测量而不是模型更正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>