BindableCollection changes when other ObservableCollection changes - wpf

Is there a way to update an ObservableCollection with the items which are added/deleted in other ObservableCollection?
How can I update my ViewModel's BindableCollection when items are added, removed in FullyObservableCollection?
It is important to note I am trying to use MVVM pattern with Caliburn.Micro.
VieModel
private BindableCollection<Employees> _employees = new BindableCollection(OracleConnector.GetEmployeeRepositorys());
public BindableCollection<Employees> Employees
{
get
{
return _employees;
}
set
{
OracleConnector.List.CollectionChanged += (sender, args) =>
{
_employees = OracleConnector.List;
};
NotifyOfPropertyChange(() => Employees);
}
}
OracleConnector
public class OracleConnector
{
public static FullyObservableCollection<Employees> List = new FullyObservableCollection<Employees>();
public static FullyObservableCollection<Employees> GetEmployeeRepositorys()
{
using (IDbConnection cnn = GetDBConnection("localhost", 1521, "ORCL", "hr", "hr"))
{
var dyParam = new OracleDynamicParameters();
try
{
var output = cnn.Query<Employees>(OracleDynamicParameters.sqlSelect, param: dyParam).AsList();
foreach (Employees employees in output)
{
List.Add(employees);
}
}
catch (OracleException ex)
{
MessageBox.Show("Connection to database is not available.\n" + ex.Message, "Database not available", MessageBoxButton.OK, MessageBoxImage.Error);
}
return List;
}
}
}
I am able to detect if changes are made in the FullyObservableCollection but I don't know how to pass them to the ViewModel.

Use the IEventAggregator in the OracleConnector class when you add a new employee. Publish a EmployeeAddedMessage which contains the new employee. Make sure you publish on the correct thread too. You are likely to need to use PublishOnUiThread method. The ShellViewModel is then able to implement the IHandle<EmployeeAddedMessage> as a method probably called Handle(EmployeeAddedMessage msg). Inside the Handle method you can then add the Employee to the appropriate Employee collection.
You may need to add the OracleConnector to your application bootstrapper as well as the EventAggregator class that Caliburn Micro provide. Your ShellViewModel will also need to call the Subscribe(this) method on the event aggregator. Both the OracleConnector and ShellViewModel need to be using the same instance of the event notifier. So make sure you register the event aggregator as a singleton.
See here for more details about using the event notification. Also, my application here uses event notification for application events.

Related

Reactive UI with Datagrids

Currently my RadDatagrid1 has a Cell_click action for the RadDatagrid1, and when a ClientName is selected, that client info is projected in DataGrid2.
code within Mouse Double Click:
private void Cell_click(object sender, GridViewSelectedCellsChangedEventArgs e)
{
Var Details = (from info in contacts
where info.ClientName = sender.CurrentCell.ToString()
select new {info.ClientName, info.ClientAddress, Info.ClientNumber});
DataGrid2.ItemsSource = Details.ToList();
}
This is currently what i have but, it should be a reactive UI.
An example of reactitve UI i was told to look at was this in the GridViewModel:
this.WhenAny(x => x.Forename, x => x.Surname, x => x.City, (p1, p2, p3) => Unit.Default).Subscribe(x => Filter());
But that doesn't quite make sense to me. If I could get guidance and tips how to convert this to reactive UI please.
I am a newbie to Reactive UI and my experience so far has been through trial and error, due to the lack of documentation. So my method below might not be correct.
Make sure you have a ViewModel backing your WPF control (see this page)
Your ViewModel should look something like:
public class ViewModel : ReactiveObject {
// ClientInfo type is the type of object you want to bind to the DataGrid
private ClientInfo clientInfo;
public ClientInfo ClientInfo {
set { this.RaiseAndSetIfChanged(ref clientInfo, value); }
get { return clientInfo; }
}
// Move contacts IEnumerable/IQueryable to your ViewModel
private IQueryable<ClientInfo> contacts;
public LoadInfo(string clientName) {
ClientInfo = (from info in contacts
where info.ClientName = clientName
select new {info.ClientName, info.ClientAddress, Info.ClientNumber})
}
}
Make sure your View (the control class) implements IViewFor<T> where T is the type of your View Model. Bind views according to the documentation here.
Do something like this for your View:
// Implement the methods on that interface, which I will not include below
public partial class View : IViewFor<ViewModel> {
private ICommand loadClientInfo;
public View() { // constructor
InitializeComponent(); // Don't forget this
// Binds the data in your ViewModel with the ItemSource of your DataGrid
this.OneWayBind(ViewModel, vm => vm.ClientInfo, x => x.DataGrid2.ItemSource);
}
private void Cell_click(object sender, GridViewSelectedCellsChangedEventArgs e)
{
ViewModel.LoadInfo(sender.CurrentCell.ToString());
}
}

How to send notification about the change of composite collection

I've got a composite collection. After modifications on its items from code behind I want the View to get updated. But I don't know how to notify the view. I tried the INotifyCollectionChanged, but it didn't work for me.
protected ObservableCollection<ScriptParameterComboItem> cItems
public virtual CompositeCollection CItems
{
get
{
return new CompositeCollection {new CollectionContainer {Collection = cItems}};
}
}
public void ConvertValue(params object[] parameters)
{
string newAverageOption = DisplayValueConverter.Convert(1, parameters);
var enumItem = cItems[1];
enumItem.Value = newAverageOption;
RaiseCollectionChanged("CItems");
}
protected void RaiseCollectionChanged(string property)
{
if(CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add));
}
Your ScriptParameterComboItem class must implement INotifyPropertyChanged. So when changing it's properties, listeners will be notified. Using ObservableCollection helps listeners to be notified when something is added to the collection or removed from. Not changing the actual data within every single item.

Notifying my UI to refresh when a collection in it's VM changes

I have a usercontrol bound to a VM. This VM contains a collection property, lets call it "MyCollection" and several regular properties lets call one of them "SomeProperty". As you can see, the get and set logic for this property references the collection in the VM.
The problem is, when I make a change to "MyCollection", this obviously has an impact on the values that are shown in the UI (as they are calculated based on it). However, my UI doesn't appear to be smart enough to update itself whenever "MyCollection" changes.
Here is the VM my usercontrol is bound to:
public class MyVM
{
private ObservableCollection<SomeOtherVM> _myCollection = new ObservableCollection<SomeOtherVM>();
public MyVM()
{
}
public ObservableCollection<SomeOtherVM> MyCollection
{
get { return _myCollection; }
[Notify]
set
{
_myCollection = value;
}
}
public virtual string SomeProperty
{
get
{
if (_myCollection.Count == 1)
return _myCollection[0].SomeProperty;
else
return "More than one "SomeOtherVM" has been selected";
}
[Notify]
set
{
foreach (SomeOtherVM s in _myCollection)
{
s.SomeProperty = value;
}
}
}
}
}
Note that nothing in my usercontrol is directly bound to the collection, it is only bound to other properties that reference the collection in its get; set; methods.
Is there anything I could do in the VM to force the UI to update whenever "MyCollection" is changed? I want to avoid having to put anything in the code behind for the user control.
Subscribe to CollectionChanged of MyCollection and fire PropertyChanged-notifications for the other properties there (no fancy attribute usage for you).

Validating data on the UI with WCF RIA and the MVVM pattern

Is there a best practice or widely accepted way of structuring and validating data using MVVM in conjunction with RIA services in Silverlight?
Here's the crux of my problem. Let's say I have an EmployeeView, EmployeeViewModel and some Employee entity. In regular RIA applications I will expose that Employee entity on the view and I get validation "for free", because Entities implement INotifyDataErrorInfo and IDataErrorInfo (correct?).
Now if I want to expose some Employee properties through a ViewModel instead of directly through an Entity then it becomes more complicated. I could expose the bits that I need directly and hook them into the entity on the backend, like this:
private Employee _employee;
public EmployeeViewModel()
{
_employee = new Employee();
}
public string Name
{
get { return _employee.Name; }
set
{
_employee.Name = value;
// fire property change, etc.
}
}
... but I lose the tasty "free" validation of entities. Otherwise, I could expose the entity directly in the view model, like so
private Employee _employee;
public Employee Employee
{
get { return _employee; }
}
public EmployeeViewModel()
{
_employee = new Employee();
}
In this case, the view will bind directly to the Employee entity and find its properties in there, like so:
<StackPanel DataContext="{Binding Employee}">
<TextBox Text="{Binding Name}" />
</StackPanel>
Using this method we get "free" validation, but it's not exactly a clean implementation of MVVM.
A third option would be to implement INotifyDataErrorInfo and IDataErrorInfo myself in the VMs, but this seems like an awful lot of plumbing code, considering how easy it would be for me to use the above solution and have something slightly less "clean" but a heck of a lot easier at the end of the day.
So I guess my question is, which of these approaches are appropriate in which situation? Is there a better approach I am missing?
In case it's relevant I'm looking at the Caliburn.Micro MVVM framework, but I would be keen to see answers that apply generically.
I am using RIA with Caliburn.Micro and am pretty happy with my solution for client side validation.
What I have done is put a ValidationBaseViewModel between Screen (provided by Caliburn.Micro) and my actual application VMs (EmployeeViewModel in your case). ValidationBaseViewModel implements INotifyDataErrorInfo so that plumbing code your talking about is only written once. I then add/remove/notify of errors via ValidationBaseViewModel from an override of the (Caliburn.Micro) PropertyChangedBase.NotifyOfPropertyChange with the following code:
public override void NotifyOfPropertyChange(string property)
{
if (_editing == null)
return;
if (HasErrors)
RemoveErrorFromPropertyAndNotifyErrorChanges(property, 100);
if (_editing.HasValidationErrors)
{
foreach (var validationError in
_editing.ValidationErrors
.Where(error => error.MemberNames.Contains(property)))
{
AddErrorToPropertyAndNotifyErrorChanges(property, new ValidationErrorInfo() { ErrorCode = 100, ErrorMessage = validationError.ErrorMessage });
}
}
base.NotifyOfPropertyChange(property);
}
This is actually in another VM (between ValidationBaseViewModel and EmployeeViewModel) with the following definition:
public abstract class BaseEditViewModel<TEdit> :
ValidationBaseViewModel where TEdit : Entity
where Entity is RIAs System.ServiceModel.DomainServices.Client.Entity and the _editing class member is an instance of this type TEdit which is being edited by the current VM.
In combination with Caliburn coroutines this allows me to do some cool stuff like the following:
[Rescue]
public IEnumerable<IResult> Save()
{
if (HasErrors)
{
yield return new GiveFocusByName(PropertyInError);
yield break;
}
...
}
If you don't want to use external resources or frameworks, then I you could have a ViewModelBase that implement INotifyDataErrorInfo.
That class will have ValidateProperty(string propertyName, object value) to validate a speciic property, and Validate() method to validate the entire object. Internally use the Validator class to return the ValidationResults.
If you use reflector, it can be pretty easy to achieve by mimicking the validation process in the Entity class itself to the ViewModelBase.
Although it's no "free", is still relatively cheap tho.
Here is a sample implementation of IDataErrorInfo. Although not tested, will give you the idea.
public class ViewModelBase : INotifyPropertyChanged, INotifyDataErrorInfo
{
/*
* InotifyPropertyChanged implementation
* Consider using Linq expressions instead of string names
*/
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public IEnumerable GetErrors(string propertyName)
{
if (implValidationErrors == null) return null;
return ImplValidationErros.Where(ve =>
ve.MemberNames.Any(mn => mn == propertyName));
}
public bool HasErrors
{
get
{
return implValidationErrors == null || ImplValidationErros.Any();
}
}
private List<ValidationResult> implValidationErrors;
private List<ValidationResult> ImplValidationErros
{
get
{
return implValidationErrors ??
(implValidationErrors = new List<ValidationResult>());
}
}
private ReadOnlyCollection<ValidationResult> validationErrors;
[Display(AutoGenerateField = false)]
protected ICollection<ValidationResult> ValidationErrors
{
get
{
return validationErrors ??
(validationErrors =
new ReadOnlyCollection<ValidationResult>(ImplValidationErros));
}
}
protected void ValidateProperty(string propertyName, object value)
{
ValidationContext validationContext =
new ValidationContext(this, null, null);
validationContext.MemberName = propertyName;
List<ValidationResult> validationResults =
new List<ValidationResult>();
Validator.TryValidateProperty(
value,
validationContext,
validationResults);
if (!validationResults.Any()) return;
validationResults
.AddRange(ValidationErrors
.Where(ve =>
!ve.MemberNames.All(mn =>
mn == propertyName)));
implValidationErrors = validationResults;
if (ErrorsChanged != null)
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
}
you can use a partial class to extend your entitty and add data validation there via idataerrorinfo.

Silverlight DataBinding cross thread issue

I have an Image control with it's source bound to a property on an object(string url to an image). After making a service call, i update the data object with a new URL. The exception is thrown after it leaves my code, after invoking the PropertyChanged event.
The data structure and the service logic are all done in a core dll that has no knowledge of the UI. How do I sync up with the UI thread when i cant access a Dispatcher?
PS: Accessing Application.Current.RootVisual in order to get at a Dispatcher is not a solution because the root visual is on a different thread(causing the exact exception i need to prevent).
PPS: This only is a problem with the image control, binding to any other ui element, the cross thread issue is handled for you.
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {...});
Also look here.
Have you tried implementing INotifyPropertyChanged?
The property getter for RootVisual on the Application class has a thread check which causes that exception. I got around this by storing the root visual's dispatcher in my own property in my App.xaml.cs:
public static Dispatcher RootVisualDispatcher { get; set; }
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page();
RootVisualDispatcher = RootVisual.Dispatcher;
}
If you then call BeginInvoke on App.RootVisualDispatcher rather than Application.Current.RootVisual.Dispatcher you shouldn't get this exception.
I ran into a similar issue to this, but this was in windows forms:
I have a class that has it's own thread, updating statistics about another process, there is a control in my UI that is databound to this object. I was running into cross-thread call issues, here is how I resolved it:
Form m_MainWindow; //Reference to the main window of my application
protected virtual void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
if(m_MainWindow.InvokeRequired)
m_MainWindow.Invoke(
PropertyChanged, this, new PropertyChangedEventArgs(propertyName);
else
PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
}
This seems to work great, if anyone has suggestions, please let me know.
When ever we want to update UI related items that action should happen in the UI thread else you will get an invalid cross thread access exception
Deployment.Current.Dispatcher.BeginInvoke( () =>
{
UpdateUI(); // DO the actions in the function Update UI
});
public void UpdateUI()
{
//to do :Update UI elements here
}
The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.
For example, consider a Person object with a property called FirstName. To provide generic property-change notification, the Person type implements the INotifyPropertyChanged interface and raises a PropertyChanged event when FirstName is changed.
For change notification to occur in a binding between a bound client and a data source, your bound type should either:
Implement the INotifyPropertyChanged interface (preferred).
Provide a change event for each property of the bound type.
Do not do both.
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
// Change the namespace to the project name.
namespace TestNotifyPropertyChangedCS
{
// This form demonstrates using a BindingSource to bind
// a list to a DataGridView control. The list does not
// raise change notifications. However the DemoCustomer type
// in the list does.
public partial class Form1 : Form
{
// This button causes the value of a list element to be changed.
private Button changeItemBtn = new Button();
// This DataGridView control displays the contents of the list.
private DataGridView customersDataGridView = new DataGridView();
// This BindingSource binds the list to the DataGridView control.
private BindingSource customersBindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
// Set up the "Change Item" button.
this.changeItemBtn.Text = "Change Item";
this.changeItemBtn.Dock = DockStyle.Bottom;
this.changeItemBtn.Click +=
new EventHandler(changeItemBtn_Click);
this.Controls.Add(this.changeItemBtn);
// Set up the DataGridView.
customersDataGridView.Dock = DockStyle.Top;
this.Controls.Add(customersDataGridView);
this.Size = new Size(400, 200);
}
private void Form1_Load(object sender, EventArgs e)
{
// Create and populate the list of DemoCustomer objects
// which will supply data to the DataGridView.
BindingList<DemoCustomer> customerList = new BindingList<DemoCustomer>();
customerList.Add(DemoCustomer.CreateNewCustomer());
customerList.Add(DemoCustomer.CreateNewCustomer());
customerList.Add(DemoCustomer.CreateNewCustomer());
// Bind the list to the BindingSource.
this.customersBindingSource.DataSource = customerList;
// Attach the BindingSource to the DataGridView.
this.customersDataGridView.DataSource =
this.customersBindingSource;
}
// Change the value of the CompanyName property for the first
// item in the list when the "Change Item" button is clicked.
void changeItemBtn_Click(object sender, EventArgs e)
{
// Get a reference to the list from the BindingSource.
BindingList<DemoCustomer> customerList =
this.customersBindingSource.DataSource as BindingList<DemoCustomer>;
// Change the value of the CompanyName property for the
// first item in the list.
customerList[0].CustomerName = "Tailspin Toys";
customerList[0].PhoneNumber = "(708)555-0150";
}
}
// This is a simple customer class that
// implements the IPropertyChange interface.
public class DemoCustomer : INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// The constructor is private to enforce the factory pattern.
private DemoCustomer()
{
customerNameValue = "Customer";
phoneNumberValue = "(312)555-0100";
}
// This is the public factory method.
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
}
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged();
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
NotifyPropertyChanged();
}
}
}
}
}

Resources