XMLA,数据绑定不起作用

本文关键字:不起作用 数据绑定 XMLA | 更新日期: 2023-09-27 18:20:41

<Window x:Class="RMT_MMO.Starter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="304" Width="472"
        ResizeMode="NoResize">
    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100*"/>
            <ColumnDefinition Width="200*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="10*"/>
            <RowDefinition Height="40*"/>
            <RowDefinition Height="40*"/>
            <RowDefinition Height="80*"/>
        </Grid.RowDefinitions>
<ProgressBar Grid.ColumnSpan="2" Maximum="100" Value="{Binding Percent}"/>
<Label Grid.ColumnSpan="2" Content="{Binding Status }" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="14" FontWeight="Bold" Margin="0,0,0,10"/>
        <Label Grid.Row="2" Content="Username" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontSize="14" FontWeight="Bold"/>
        <TextBox IsEnabled="{Binding CanLogin}"   Grid.Row="2" Grid.Column="1" Margin="5"  Text="{Binding Username}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
        <Label Grid.Row="3" Content="Password" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontSize="14" FontWeight="Bold"/>
        <PasswordBox  IsEnabled="{Binding CanLogin}" Grid.Row="3" Grid.Column="1" Margin="5" x:Name="Password" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
        <Button Click="Login" IsDefault="True" IsEnabled="{Binding CanLogin}"   Content="asdasd"  Grid.Row="4" HorizontalAlignment="Left"  Padding="10"  Margin="10,25,0,19" RenderTransformOrigin="7.273,0.484" Width="247" Grid.Column="1"/>
    </Grid>
</Window>

所以这里MainWindow看起来如何,当我单击按钮执行此代码时

using System.Windows;
namespace RMT_MMO.Starter
{
    public partial class MainWindow
    {
        private readonly MainWindowViewModel _viewModel;
        public MainWindow()
        {
            InitializeComponent();
            _viewModel = new MainWindowViewModel();
            DataContext = _viewModel;
            _viewModel.CanLogin = true;
        }
        public void Login(object sender,RoutedEventArgs e)
        {
            //    MessageBox.Show(_viewModel.Percent.ToString());
                _viewModel.CanLogin = false;
                _viewModel.Percent =50;
           //     _viewModel.Status = "Stuff happened"; 
        }
    }
}

MainWindowViewModel类看起来像这样:

using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace RMT_MMO.Starter
{
   public class MainWindowViewModel:INotifyPropertyChanging
    {
       private bool _canLogin;
       private string _status;
       private double _percent;
       private string _username;
       public event PropertyChangingEventHandler PropertyChanging;
        public bool CanLogin
        {
            get { return _canLogin; }
            set
            {
                _canLogin = value;
                OnPropertyChanged();
            }
        }
       public string Status
       {
           get { return _status; }
           set
           {
               _status = value;
               OnPropertyChanged();
           }
       }
       public double Percent
       {
           get { return _percent; }
           set
           {
               _percent = value;
               OnPropertyChanged();
           }
       }
       public string Username
       {
           get { return _username; }
           set
           {
               _username = value;
               OnPropertyChanged();
           }
       }
       private void OnPropertyChanged([CallerMemberName] string propertyName = null)
       {
           var handler = PropertyChanging;
           if (handler != null)
               handler(this, new PropertyChangingEventArgs(propertyName));
       }
    }
}

我正在尝试做的是绑定xmla和这段代码,当我点击按钮绑定时没有执行。我知道这是一个微不足道的错误,但我根本无法罚款,我尝试在线搜索但失败了。欢迎所有帮助。

XMLA,数据绑定不起作用

这可能是

因为您在应该使用INotifyPropertyChanged接口时使用了INotifyPropertyChanging