Search code examples
c#xmldata-bindingxmla

XMLA, data-binding does not working


<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>

So here how MainWindow look, when I click to button this code execute

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

And MainWindowViewModel class looks like this:

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

What I am trying to do is to bind xmla and this code, when I hit button binding is not performed. I know it it is trivial mistake but I simple can't fined it, I tried searching online but I failed. All help is welcome.


Solution

  • It could be because you are using INotifyPropertyChanging when you should be using INotifyPropertyChanged interface.