Hi,
I recently made the switch from WInforms to WPF.
I'm trying to learn the mvvm pattern and am having a few issues, I'm a novice, so if any of you guys would have the time to take a look and give me a nudge in the right direction I would be extremely grateful.
I have a DataGrid which populates fine from my ViewModel, I then have a child window with TextBoxes I use for update purpose. My research has led me to believe I need to bind the 'SelectedItem' of the DataGrid to the TextBoxes, I have tried this, but either get no results or just the top row of the Grid regardless of my row selection.
What I have tried.
This produces no results
My ViewModel
This works in non mvvm Enviroment but not here.
I am learning how to use ICommand to replace code behind
Thank you in advance.
I recently made the switch from WInforms to WPF.
I'm trying to learn the mvvm pattern and am having a few issues, I'm a novice, so if any of you guys would have the time to take a look and give me a nudge in the right direction I would be extremely grateful.
I have a DataGrid which populates fine from my ViewModel, I then have a child window with TextBoxes I use for update purpose. My research has led me to believe I need to bind the 'SelectedItem' of the DataGrid to the TextBoxes, I have tried this, but either get no results or just the top row of the Grid regardless of my row selection.
What I have tried.
XML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="159*" />
</Grid.ColumnDefinitions>
<DataGrid x:Name="dgCustomers"
Margin="5,60,10,10"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=GetCustomers}"
SelectedItem="{Binding Path=SelectedItem}"
Grid.Column="1">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding custid}"
Header="CustomerID" />
<DataGridTextColumn Binding="{Binding name}"
Header="Customer" />
<DataGridTextColumn Binding="{Binding address}"
Header="Address" />
<DataGridTextColumn Binding="{Binding city}"
Header="City" />
<DataGridTextColumn Binding="{Binding postcode}"
Header="Postcode" />
<DataGridTextColumn Binding="{Binding email}"
Header="Email" />
<DataGridTextColumn Binding="{Binding phone}"
Header="Phone" />
</DataGrid.Columns>
</DataGrid>
</Grid>
This produces no results
XML:
<Label Name="lblCustomerName"
Grid.Column="0"
Grid.Row="2"
VerticalAlignment="Center"
HorizontalAlignment="Left">Customer Name:</Label>
<TextBox x:Name="txtCustomerName"
Text="{Binding Path=SelectedItem.name}"
Grid.Column="1"
Grid.Row="2"
VerticalContentAlignment="Center"
Height="22"
AcceptsReturn="True"
TextWrapping="Wrap"
SpellCheck.IsEnabled="True"
Language="en-U" />
My ViewModel
C#:
public class CustomersViewModel
{
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Business_MySQL"].ConnectionString;
public CustomersViewModel()
{
LoadCustomers();
}
public ObservableCollection<Customer>? GetCustomers
{
get;
set;
}
public Customer? Selected { get; set; }///Tried binding TextBox to this
public void LoadCustomers()
{
ObservableCollection<Customer> customers = new();
{
try
{
using (MySqlConnection? con = new(connectionString))
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
using (MySqlCommand cmd = new("AllCustomerRecords", con))
{
cmd.CommandType = CommandType.StoredProcedure;
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
customers.Add(new Customer()
{
id = (int)reader["ID"],
custid = reader["CustomerID"].ToString(),
name = reader["Customer_Name"].ToString(),
address = reader["Customer_Address"].ToString(),
city = reader["City"].ToString(),
postcode = reader["Postcode"].ToString(),
email = reader["Email"].ToString(),
phone = reader["Phone"].ToString()
});
customers = new ObservableCollection<Customer>(customers.OrderBy(x => x.name));
}
GetCustomers = customers;
}
}
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
public Customer SelectedItem { get; set; }///and this
}
This works in non mvvm Enviroment but not here.
I am learning how to use ICommand to replace code behind
C#:
private void btnEditCustomer_Click(object sender, RoutedEventArgs e)
{
if (this.edit == null || this.edit.IsClosed)
{
this.edit = new CustomersAddEdit();
}
if (this.dgCustomers.SelectedItems.Count == 0)
{
MessageBox.Show("Please select a row!");
}
else if (this.dgCustomers.SelectedItems.Count == 1)
{
//var owner = this;
//DataContext = dgCustomers.SelectedItem;
var cus = (Customer)dgCustomers.SelectedItem;
// cus = Selected;
}
this.edit.Show();
}
Thank you in advance.