XMLA,数据绑定不起作用

时间:2014-09-05 10:41:23

标签: c# xml data-binding xmla

<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和这段代码,当我没有执行按钮绑定时。 我知道这是一个微不足道的错误,但我简单不能罚款,我尝试在网上搜索,但我失败了。欢迎大家帮忙。

1 个答案:

答案 0 :(得分:0)

可能是因为您在使用INotifyPropertyChanging界面时使用INotifyPropertyChanged