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... I read several topics about databinding and everything seems to be OK... I’m using the MVVM standard with WPF
The model class:
namespace TagPort.Models
{
public class EPMConnection
{
public string epmServerMachine { get; set; }
public string serverPort { get; set; }
public string serverUser { get; set; }
public string serverPwd { get; set; }
}
}
Viewmodel:
namespace TagPort.ViewModels
{
class ConnectionViewModel : INotifyPropertyChanged
{
public EPMConnection epmConnection;
public ConnectionViewModel()
{
epmConnection = new EPMConnection { 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 CS view
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());
}
}
Did you noticed that the site is in Portuguese? So why did you post in English?
– Victor Stafusa
Oops, I didn’t see hahahahaha
– Arthuro Verissimo
I fixed the post already, thanks
– Arthuro Verissimo