Im building a WPF application using MVVM Pattern but i 've been testing even for simple binding and i dont get it work the code is basically like this:
I think that the problem is in the xaml Bindings
Model:
public class Product
{
private string modelNumber;
public string ModelNumber
{
get { return modelNumber; }
set { modelNumber = value; }
}
private string modelName;
public string ModelName
{
get { return modelName; }
set { modelName = value; }
}
private decimal unitCost;
public decimal UnitCost
{
get { return unitCost; }
set { unitCost = value; }
}
private string description;
public string Description
{
get { return description; }
set { description = value; }
}
public Product(string modelNumber, string modelName, decimal unitCost, string description)
{
ModelNumber = modelNumber;
ModelName = modelName;
UnitCost = unitCost;
Description = description;
}
public static Product GetProduct()
{
return new Product("1","A6",20000,"Description");
}
}
ViewModel:
class ProductViewModel
{
public Product p;
public ProductViewModel()
{
p= Product.GetProduct();
}
}
Xaml:
<Grid Name="gridProductDetails" >
<TextBlock Margin="7">Model Number:</TextBlock>
<TextBox Margin="5" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding p.ModelNumber}" ></TextBox>
</Grid>
Code Behind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
gridProductDetails.DataContext = new ProductViewModel();
}
Please update the following that should solve your problem:
Mark ProductViewModel as public
Convert variable p to auto property. public Product p { get; set; }
Regards
Related
I'm new to WPF and MVVM and i've an applicaton that uses Entity Framework to connect to database and a datagrid to show the users of the application.
The users CRUD operations are made in a separate window and not in the datagrid.
My problems are related with the update of datagrid.
The insert operation is ok but the update is not.
View 1 (Users List):
<DataGrid Grid.Row="1"
ItemsSource="{Binding Users, Mode=TwoWay}"
SelectedItem="{Binding SelectedUser, Mode=TwoWay}"
AutoGenerateColumns="False"
CanUserAddRows="False">
</DataGrid>
ViewModel :
class UserListViewModel: NotificationClass
{
UserDBContext _db = null;
public UserListViewModel()
{
_db = new UserDBContext();
Users = new ObservableCollection<User>(_db.User.ToList());
SelectedUser = Users.FirstOrDefault();
}
private ObservableCollection<User> _users;
public ObservableCollection<User> Users
{
get { return _users; }
set
{
_users = value;
OnProprtyChanged();
}
}
private User _selectedUser;
public User SelectedUser
{
get
{
return _selectedUser;
}
set
{
_selectedUser = value;
OnProprtyChanged();
}
}
public RelayCommand Edit
{
get
{
return new RelayCommand(EditUser, true);
}
}
private void EditUser()
{
try
{
UserView view = new UserView();
view.DataContext = SelectedUser;
view.ShowDialog();
if (view.DialogResult.HasValue && view.DialogResult.Value)
{
if (SelectedUser.Id > 0){
User updatedUser = _db.User.First(p => p.Id == SelectedUser.Id);
updatedUser.Username = SelectedUser.Username; //this doesn't do nothing, object is already with the new username ?!
}
_db.SaveChanges();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
after _db.SaveChanges(), datagrid should not be updated ?
Model:
class UserDBContext: DbContext
{
public UserDBContext() : base("name=DefaultConnection")
{
}
public DbSet<User> User { get; set; }
}
View 2 (User detail)
public partial class UserView : Window
{
public UserView()
{
InitializeComponent();
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
}
User object
class User: NotificationClass
{
public int Id { get; set; }
public string Username { get; set; }
public string CreatedBy { get; set; }
public DateTime? CreatedOn { get; set; }
}
NotificationClass
public class NotificationClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void OnProprtyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
if i close and open view 1, the new username is updated..
could someone help ? thanks
Just implementing INotifyPropertyChanged isn't enough, you have to explicitly invoke PropertyChanged (or in your case OnPropertyChanged) when a property changed.
See also https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-property-change-notification
You can do it like so
class User : NotificationClass
{
private int _id;
private string _username;
private string _createdBy;
private DateTime? _createdOn;
public int Id
{
get => _id;
set
{
if (value == _id) return;
_id = value;
OnPropertyChanged();
}
}
public string Username
{
get => _username;
set
{
if (value == _username) return;
_username = value;
OnPropertyChanged();
}
}
public string CreatedBy
{
get => _createdBy;
set
{
if (value == _createdBy) return;
_createdBy = value;
OnPropertyChanged();
}
}
public DateTime? CreatedOn
{
get => _createdOn;
set
{
if (value.Equals(_createdOn)) return;
_createdOn = value;
OnPropertyChanged();
}
}
}
it worked ! many thanks #nosale !
what about the change made to SelectedUser being reflected in my context ?
if i do this :
SelectedUser.Username = "test";
User updatedUser = _db.User.First(p => p.Id == SelectedUser.Id);
i was thinking that SelectedUser object has the "test" username and updatedUser has the old username, but not .. updatedUser already have "test"
I have a puzzled problem of databinding in WPF.
There is a listbox in XAML which it has linked with ItemSource,
but when it runs, it shows the lists of class names.
so I have applied to DisplayMemberPath, but it doesn't helpful.
and also I'm wondering how I can access inside class from generic class.
Thanks.
result
puzzled.Member
puzzled.Member
puzzled.Member
puzzled.Member
<DockPanel>
<ListBox Name="lbxMbrList" DockPanel.Dock="Left" Width="200" Padding="10"></ListBox>
<ContentControl />
</DockPanel>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
members.Add(new Member("superman", "123-1234567", "address1"));
members.Add(new Member("batman", "111-111111", "address2"));
members.Add(new Member("goodman", "222-222222", "address3"));
members.Add(new Member("badman", "333-333333", "address4"));
lbxMbrList.ItemsSource = members;
lbxMbrList.DisplayMemberPath = members.MemberDetails; //<<it won't helpful
//var i = members.member.Name; //<<how can I access inside class?
//if (i == "superman")
//{
// MessageBox.Show("superman");
//}
}
public class Member
{
private string _name;
private string _phone;
private string _address;
public string Name { get { return _name; } set { _name = value; } }
public string Phone { get { return _phone; } set { _phone = value; } }
public string Address { get { return _address; } set { _address = value; } }
public Member() { }
public Member(string name, string phone, string address)
{
_name = name; _phone = phone; _address = address;
}
public string lbxMember
{
get { return string.Format("{0} - {1}", Name, Phone, Address); }
}
}
class MemberList : IEnumerable<Member>
{
private ObservableCollection<Member> memberList = new ObservableCollection<Member>();
public Member this[int i]
{
get {return memberList[i];}
set {memberList[i] = value;}
}
public void Add(Member member)
{
memberList.Add(member);
}
public void Remove(Member member)
{
memberList.Remove(member);
}
public IEnumerator<Member> GetEnumerator()
{
return memberList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public Member member { get; set; } //<< it think I has misunderstood it
public string MemberDetails
{
get
{ return string.Format("{0} - {1}", member.Name, member.Phone, member.Address); }
}
}
You are assigning the output of your MemberDetails property to the DisplayMemberPath. Instead, you need to assign the name of the property as a string.
lbxMbrList.DisplayMemberPath = "MemberDetails";
For what its worth, this will be easier to work with if you use an ItemTemplate in the ListBox.
[Edit]
Also, as #Blam mentions in his answer, your MemberDetails property is defined in the wrong class, it needs be in the Member class.
lbxMbrList.DisplayMemberPath = "lbxMember";
or
lbxMbrList.DisplayMemberPath = "MemberDetails";
And MemberDetails need to be a property of Member (not MemberList)
I cant figureout why my ICollectionView is not refreshed. Can anyone explain what I'm doing wrong?
I've made a viewmodel like this:
class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<Entity> m_entities = new ObservableCollection<Entity>();
public ICollectionView EntitiesView { get; private set; }
public ICollectionView HighCountView { get; private set; }
public ViewModel()
{
m_entities.Add(new Entity() { Id = 1, Name = "Erik", Description = "The first" });
m_entities.Add(new Entity() { Id = 2, Name = "Olle", Description = "The second" });
m_entities.Add(new Entity() { Id = 3, Name = "Kim", Description = "The last" });
EntitiesView = CollectionViewSource.GetDefaultView(m_entities);
EntitiesView.CurrentChanged += new EventHandler(EntitiesView_CurrentChanged);
HighCountView = new CollectionView(m_entities);
using (HighCountView.DeferRefresh())
{
HighCountView.Filter = e => ((Entity)e).Count > 3;
}
}
private void EntitiesView_CurrentChanged(object sender, EventArgs e)
{
Entity current = EntitiesView.CurrentItem as Entity;
if(current!=null)
{
current.Count++;
HighCountView.Refresh(); // Do I need this line?
OnPropertyChanged("HighCountView"); // or this?
}
}
...and in my window I use it as the datacontext, like this:
public partial class MainWindow : Window
{
private ViewModel vm = new ViewModel();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = vm;
}
}
...and I'm doing my bindings in the XAML-code like this:
<ListBox Grid.Column="0" x:Name="listView1" DisplayMemberPath="Name" ItemsSource="{Binding EntitiesView}" IsSynchronizedWithCurrentItem="True" />
<ListView Grid.Column="1" x:Name="listView2" DisplayMemberPath="Name" ItemsSource="{Binding HighCountView}" IsSynchronizedWithCurrentItem="True" />
The problem is that all three Entities is always shown in listView2 despite that I set the Filter-property. Why?
EDIT
To made the sample complete, here is the Entity-class.
class Entity : INotifyPropertyChanged
{
private int m_id;
public int Id
{
bla bla.....
}
private string m_name;
public string Name
{
bla bla.....
}
private string m_description;
public string Description
{
bla bla.....
}
private int m_count;
public int Count
{
get { return m_count; }
set
{
if (value != m_count)
{
m_count = value;
OnPropertyChanged("Count");
}
}
}
public void Update()
{
Description = "Updated: " + (++Count).ToString() + " times.";
}
At last I found what was wrong.
If I change the line:
HighCountView = new CollectionView(m_entities);
to this
HighCountView = new ListCollectionView(m_entities);
then it works a expected.
I can also remove this line
OnPropertyChanged("HighCountView"); // or this?
I hope this can help somebody!
View:
Playing with a basic calculator using WPF(MVVM).
I've 1 TextBox for the first num, 1 TextBox for the second num, 1 TextBlock for the results and 1 Button to execute the AddCommand and return the result.
What's the right XAML syntax to bind these controls to the right Data.
Model:
public class Operation : INotifyPropertyChanged
{
private double _result;
public Operation()
{
_result = 0;
}
public double Result
{
get { return _result; }
set
{
if (value != _result)
{
_result = value;
RaisePropertyChanged("Result");
}
}
}
public double DoAdd(double first, double second)
{
_result = first + second;
return _result;
}
}
ViewModel:
public class CalcViewModel
{
private Operation _operation;
public RelayCommand AddCommand { get; set; }
public CalcViewModel()
{
_operation = new Operation();
// This is not correct, how to define the AddCommand here so it takes two params
// The first and second nums to work with.
AddCommand = new RelayCommand(first, second => ExecuteAddCommand(first, second));
}
private void ExecuteAddCommand(double first, double second)
{
// How to bind this returned double to the TextBlock in View
_oepration.DoAdd(first, second);
}
}
EDIT new version of code on request of Vlad
Model:
public class Operation
{
private double _result;
public Operation()
{
_result = 0;
}
public double Result
{
get { return _result; }
}
public void PerformAdd(double leftNum, double rightNum)
{
_result = leftNum + rightNum;
}
}
ViewModel:
public class CalcViewModel
{
private Operation _operation;
public double LeftNumber { get; set; }
public double RightNumber { get; set; }
public double Result { get; set; }
public RelayCommand AddCommand { get; set; }
public CalcViewModel()
{
AddCommand = new RelayCommand(a => ExecuteAddCommand());
_operation = new Operation();
}
private void ExecuteAddCommand()
{
_operation.PerformAdd(LeftNumber, RightNumber);
Result = _operation.Result;
}
View XAML:
<TextBox Text="{Binding LeftNumber}" />
<TextBox Text="{Binding RightNumber}" />
<TextBox Text="{Binding Result}" />
<Button Content="Add" Command="{Binding AddCommand}" />
View Code behind:
public partial class CalcUserControl : UserControl
{
CalcViewModel vm;
public CalcUserControl()
{
InitializeComponent();
vm = new CalcViewModel();
this.DataContext = vm;
}
}
I tried all modes of binding without any result. I have here an additional question, what's the default binding mode in such a situation?
I even thought that it has to do with the datatype of the calculation, so I swiched from double to int, but still not working.
Well, let's see what can be done.
1) Model. The model doesn't need anything fancy. I would keep it simple and make it just return the value and not use NotifyPropertyChanged. After all, it's a model.
public class BinaryOperation
{
double _l, _r, _result = 0.0;
public Operation(double l, double r)
{
_l = l; _r = r;
}
public double Result
{
get { return _result; }
}
public PerformAdd()
{
_result = _l + _r;
}
}
2) ViewModel. Here, your RelayCommand doesn't really need any arguments. But you need to store the values of operands in your VM, so that your view can bind to them, instead of sending them in a command. Remember, business logic doesn't belong to view, view just blindly binds to the VM! So you need 3 DPs (left addend, right addend, result) in your VM.
3) When the command arrives, you just take the addends from VM, ask the model to perform the operation, retrieve the result and assign it to your VM's result DP. (Right now, your model operations are fast, so you don't need to do it in asynchronous way. But maybe in the future...)
4) View. You need for your Window/UserControl just to bind to the VM's properties.
Its going to be something as simple as:
<TextBox Text="{Binding LeftAddend}"/>
<TextBox Text="{Binding RightAddend}"/>
<TextBox Text="{Binding Result}"/>
<Button Command="{Binding AddCommand}">Add</Button>
(Don't forget to set the DataContext right.)
Edit:
the VM class has to be a dependency object! And the properties should b defined as dependency properties. Something like this:
public class CalcViewModel : DependencyObject
{
private Operation _operation;
public double LeftNumber
{
get { return (double)GetValue(LeftNumberProperty); }
set { SetValue(LeftNumberProperty, value); }
}
public static readonly DependencyProperty LeftNumberProperty =
DependencyProperty.Register("LeftNumber", typeof(double), typeof(CalcViewModel));
public double RightNumber
{
get { return (double)GetValue(RightNumberProperty); }
set { SetValue(RightNumberProperty, value); }
}
public static readonly DependencyProperty RightNumberProperty =
DependencyProperty.Register("RightNumber", typeof(double), typeof(CalcViewModel));
public double Result
{
get { return (double)GetValue(ResultProperty); }
set { SetValue(ResultProperty, value); }
}
public static readonly DependencyProperty ResultProperty =
DependencyProperty.Register("Result", typeof(double), typeof(CalcViewModel));
public RelayCommand AddCommand { get; set; }
public CalcViewModel()
{
AddCommand = new RelayCommand(a => ExecuteAddCommand());
_operation = new Operation();
}
private void ExecuteAddCommand()
{
_operation.PerformAdd(LeftNumber, RightNumber);
Result = _operation.Result;
}
}
Or, if you want to do it with INotifyPropertyChanged, and you are working with .NET 4.5
public class CalcViewModel : INotifyPropertyChanged
{
private Operation _operation;
public event PropertyChangedEventHandler PropertyChanged;
void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
double _leftNumber;
public double LeftNumber
{
get { return _leftNumber; }
set
{
if (value == _leftNumber) return;
_leftNumber = value;
NotifyPropertyChanged();
}
}
double _rightNumber;
public double RightNumber
{
get { return _rightNumber; }
set
{
if (value == _rightNumber) return;
_rightNumber = value;
NotifyPropertyChanged();
}
}
double _result;
public double Result
{
get { return _result; }
set
{
if (value == _result) return;
_result = value;
NotifyPropertyChanged();
}
}
public RelayCommand AddCommand { get; set; }
public CalcViewModel()
{
AddCommand = new RelayCommand(a => ExecuteAddCommand());
_operation = new Operation();
}
private void ExecuteAddCommand()
{
_operation.PerformAdd(LeftNumber, RightNumber);
Result = _operation.Result;
}
}
The same with older .NET versions:
void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
double _leftNumber;
public double LeftNumber
{
get { return _leftNumber; }
set
{
if (value == _leftNumber) return;
_leftNumber = value;
NotifyPropertyChanged("LeftNumber");
}
}
etc.
Thank you all and especially #Vlad. Just one tiny fault, y've declared the property Result twice on class CalcViewModel : DependencyObject.
It works now fine :)
So I have the following model:
public class Person
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String Address { get; set; }
public String EMail { get; set; }
public String Phone { get; set; }
}
public class Order
{
public Person Pers { get; set;}
public Product Prod { get; set; }
public List<Person> AllPersons { get; set; }
public Order(Person person, Product prod )
{
this.Pers = person;
this.Prod = prod;
AllPersons = database.Persons.GetAll();
}
}
And I have a WPF window used to edit an order.
I set the DataContext to Order.
public SetDisplay(Order ord)
{
DataContext = ord;
}
I have the following XAML:
<ComboBox Name="myComboBox"
SelectedItem = "{Binding Path=Pers, Mode=TwoWay}"
ItemsSource = "{Binding Path=AllPersons, Mode=OneWay}"
DisplayMemberPath = "FirstName"
IsEditable="False" />
<Label Name="lblPersonName" Content = "{Binding Path=Pers.FirstName}" />
<Label Name="lblPersonLastName" Content = "{Binding Path=Pers.LastName}" />
<Label Name="lblPersonEMail" Content = "{Binding Path=Pers.EMail}" />
<Label Name="lblPersonAddress" Content = "{Binding Path=Pers.Address}" />
However, the binding does not seem to work.......When I change the selected item , the labels do not update ....
Regards!!
Any reply is appreciated !!
Your model will need to fire change notifications. See INotifyPropertyChanged and INotifyCollectionChanged.
For INotifyPropertyChanged, you could use a base ViewModel class such as this one. For collections, ObservableCollection<T> does the hard work for you. However, in your case your collection won't change after the UI is bound to it, so you shouldn't need an observable collection. Regardless, I'd generally recommend using observable collections in your view model layer to save head-scratching should the code ever change.
An example of what this would look like is:
public class Person : ViewModel
{
private string firstName;
private string lastName;
private string email;
private string phone;
public string FirstName
{
get
{
return this.firstName;
}
set
{
if (this.firstName != value)
{
this.firstName = value;
OnPropertyChanged(() => this.FirstName);
}
}
}
public string LastName
{
get
{
return this.lastName;
}
set
{
if (this.lastName != value)
{
this.lastName = value;
OnPropertyChanged(() => this.LastName);
}
}
}
// and so on for other properties
}
public class Order : ViewModel
{
private readonly ICollection<Person> allPersons;
private Person pers;
private Product prod;
public Person Pers
{
get
{
return this.pers;
}
set
{
if (this.pers != value)
{
this.pers = value;
OnPropertyChanged(() => this.Pers);
}
}
}
public Product Prod
{
get
{
return this.prod;
}
set
{
if (this.prod != value)
{
this.prod = value;
OnPropertyChanged(() => this.Prod);
}
}
}
// no need for setter
public ICollection<Person> AllPersons
{
get
{
return this.allPersons;
}
}
public Order(Person person, Product prod )
{
this.Pers = person;
this.Prod = prod;
// no need for INotifyCollectionChanged because the collection won't change after the UI is bound to it
this.allPersons = database.Persons.GetAll();
}
}