Problem in textbox Binding with C # using MVVM

0

I'm trying to update the properties of an object every time a character is added / removed from the textboxes but I'm not getting it ... I've read several topics on databinding and it seems to be OK ... I'm using the MVVM standard with WPF

Themodelclass:

namespaceTagPort.Models{publicclassEPMConnection{publicstringepmServerMachine{get;set;}publicstringserverPort{get;set;}publicstringserverUser{get;set;}publicstringserverPwd{get;set;}}}

ViewModel:

namespaceTagPort.ViewModels{classConnectionViewModel:INotifyPropertyChanged{publicEPMConnectionepmConnection;publicConnectionViewModel(){epmConnection=newEPMConnection{epmServerMachine="Tteste2" };
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }



        public EPMConnection Connection
        {
           get { return epmConnection; }
           set
            {
                epmConnection = value;
            }
        }
        public string EpmServerMachine
        {
            get { return epmConnection.epmServerMachine; }
            set
            {
                if(epmConnection.epmServerMachine != value)
                {
                    epmConnection.epmServerMachine = value;
                    RaisePropertyChanged();
                }
            }
        }
       public string EpmServerPort
        {
            get { return epmConnection.serverPort; }
            set
            {
                if (epmConnection.serverPort != value)
                {
                    epmConnection.serverPort = value;
                    RaisePropertyChanged();
                }
            }

        }
        public string EpmServerUser
        {
            get { return epmConnection.serverUser; }
            set
            {
                if (epmConnection.serverUser != value)
                {
                    epmConnection.serverUser = value;
                    RaisePropertyChanged();
                }
            }

        }
        public string EpmServerPwd
        {
            get { return epmConnection.serverPwd; }
            set
            {
                if (epmConnection.serverPwd != value)
                {
                    epmConnection.serverPwd = value;
                    RaisePropertyChanged();
                }
            }
        }
        public string ConnectionToString()
        {
            string p1 = (" EpmServer:"+ EpmServerMachine + "\n EpmPort:" + EpmServerPort + " \n EpmUser:" + EpmServerUser + " \n EpmPwd" + EpmServerPwd + "");
            return p1;
        }
    }
}

This is View's XAML:

   <UserControl.Resources>
    <local:ConnectionViewModel x:Key="ConnectionVM"/>
</UserControl.Resources>


<Grid DataContext="{StaticResource ConnectionVM}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="180"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="34"></RowDefinition>
        <RowDefinition Height="34"></RowDefinition>
        <RowDefinition Height="34"></RowDefinition>
        <RowDefinition Height="34"></RowDefinition>
        <RowDefinition Height="34"></RowDefinition>
    </Grid.RowDefinitions>
    <Label Content="EPM Sever Machine:" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0"/>
    <Label Content="EPM Server Port:" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="1"/>
    <Label Content="EPM Server User:" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="2"/>
    <Label Content="EPM Server Password:" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="3"/>

    <TextBox TextWrapping="Wrap" Text="{Binding EpmServerMachine,UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" Height="23" Margin="0,0,4,0"/>
    <TextBox TextWrapping="Wrap" Text="{Binding EpmServerPort,UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Height="23" Margin="0,0,4,0"/>
    <TextBox TextWrapping="Wrap" Text="{Binding EpmServerUser,UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Height="23" Margin="0,0,4,0"/>
    <TextBox TextWrapping="Wrap" Text="{Binding EpmServerPwd,UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="3" VerticalAlignment="Center" Height="23" Margin="0,0,4,0"/>



    <Button Content="Test Connection" Grid.Column="1" Grid.Row="4" Width="Auto" HorizontalAlignment="Right" Height="23" VerticalAlignment="Bottom" Margin="0,0,4,0" Click="TestConnection_Click"/>

</Grid>

And here's the view CS

public partial class EPMConnectionTab : UserControl
    {
        ConnectionViewModel _viewModel;
        public EPMConnectionTab()
        {
            InitializeComponent();
            _viewModel = new ConnectionViewModel();
            this.DataContext = _viewModel;
        }

        private void TestConnection_Click(object sender, RoutedEventArgs e)
        {

            MessageBox.Show(_viewModel.ConnectionToString());
        }

    }
    
asked by anonymous 24.05.2018 / 17:15

0 answers