View model Property setter not hit when dependency property updated - c#

I have a user control with a dependency property:
public ObservableCollection<Exclusion> SelectedExclusions
{
get
{
return (ObservableCollection<Exclusion>)GetValue(SelectedExclusionsProperty);
}
set
{
SetValue(SelectedExclusionsProperty, value);
}
}
public static readonly DependencyProperty SelectedExclusionsProperty =
DependencyProperty.Register(nameof(TimeSeriesChart.SelectedExclusions),
typeof(ObservableCollection<Exclusion>),
typeof(TimeSeriesChart),
new PropertyMetadata(default(ObservableCollection<Exclusion>)));
I am adding a selected exclusion to this collection on key down:
protected override void OnKeyDown(KeyEventArgs e)
{
if(e.Key == Key.Delete)
{
this.SelectedExclusions.Add(this.ExclusionProviders[0].Exclusions[this.hitTestInfo.DataSeriesIndex]);
}
}
In the view model I have this property & backing variable:
private ObservableCollection<TimeSeriesLibraryInterop.Exclusion> selectedExclusionsToDelete = new ObservableCollection<TimeSeriesLibraryInterop.Exclusion>();
public ObservableCollection<TimeSeriesLibraryInterop.Exclusion> SelectedExclusionsToDelete
{
get
{
return this.selectedExclusionsToDelete;
}
set
{
this.selectedExclusionsToDelete = value;
this.RaisePropertyChanged();
}
}
Finally the binding in the view:
<userControl1 SelectedExclusions="{Binding SelectedExclusionsToDelete, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
The dependency property collection is initialised and populated however the view model property setter is never hit when the dependency property collection changes (Add). I have no binding errors in the output window. Is there something I'm missing here?

Looks like you're adding an item to the collection rather than replacing the collection. You won't hit the vm collection property's setter that way.
If you want to your viewmodel to respond to items being added to the SelectedExclusionsToDelete collection, the viewmodel will need to handle the SelectedExclusionsToDelete.CollectionChanged event. "Properly" handling that event (remove, add, move, clear, etc.) is a real hassle, but if it's not a giant collection you can often get away with something quick and dirty: Treat any change as a whole new collection. I think that's exactly the case you've got, too.
Alternatively, for an even quicker and dirtier approach, I think you could make it a two-way binding by default and have the control assign a new ObservableCollection to this.SelectedExclusions in OnKeyDown. The binding will pass it back to the viewmodel and hit the setter.

Related

Problems Implementing IBindableComponent

I need to bind a property to a ToolStripMenuItem. I've searched around and found it impossible, the best workaround seems to be creating a BindableToolStripMenuItem class and implement it yourself. So I've taken some reasonably well established code from the internet:
public class BindableToolStripMenuItem : ToolStripMenuItem,IBindableComponent
{
private BindingContext bindingContext;
private ControlBindingsCollection dataBindings;
[Browsable(false)]
public BindingContext BindingContext
{
get
{
if (bindingContext == null)
bindingContext = new BindingContext();
return bindingContext;
}
set
{
bindingContext = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ControlBindingsCollection DataBindings
{
get
{
if (dataBindings == null)
dataBindings = new ControlBindingsCollection(this);
return dataBindings;
}
}
}
Now all I need is to bind it right? I set up a form with a toolStripMenu and added a property to bind to: No luck. I can check it, but the changes don't seem to filter down into the binding.
public partial class Form1 : Form
{
private Boolean _BindingChecked;
public Boolean BindingChecked {
get { return _BindingChecked; }
set { _BindingChecked = value; Console.WriteLine(": " + _BindingChecked); }
}
public Form1()
{
InitializeComponent();
BindableToolStripMenuItem btsmi = new BindableToolStripMenuItem();
btsmi.Text = "Checkable";
btsmi.CheckOnClick = true;
btsmi.DataBindings.Add(new Binding("Checked",this,"BindingChecked"));
itemsToolStripMenuItem.DropDownItems.Add(btsmi);
}
}
It's definitely doing some of the binding though! If I change "Binding Checked" or "Checked" strings it throws the appropriate error eg:
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Additional information: Cannot bind to the property or column BindingChecked2 on the DataSource.
It's just any changes (to either, I tested it implementing InotifyChanged going the other way too) don't get applied to the bound property.
Any Idea where I've gone wrong? I've cut things down so much, it's just these two classes, and I've tried starting from scratch with the BdindableToolStripMenuItem, and looked at a fair few other people's implementation (almost all identical) and still can't ever get it to work.
Thanks
You probably want to set the DataSourceUpdateMode of the Binding to OnPropertyChanged, the default is OnValidation but since menu items don't have normal focus behavior I can imagine the default does not work. This allows updates on the menu item to propagate to the BindingChecked property
If you want the reverse, updates to BindingChecked property by code to propagate to the menu item, you must implement INotifyPropertyChanged on the form and raise the event whenever you change the property from code (e.g. in the property set accessor).

Need multiple views of same BindingList with sorting and filtering - WPF

I know this is very similar to other questions that have been asked, and I have looked at them but in one way or another the solutions don't work for this specific scenario.
I have a collection of objects in a BindingList:
private BindingList<PathologyModel> _patientPathologies;
public BindingList<PathologyModel> PatientPathologies { get { return _patientPathologies; } }
This collection needs to be bound to different XamDataGrid controls (Infragistics' version of a DataGrid). I need to use BindingList because I need to know when a user edits a particular item in the collection, which other types of collections don't seem to support.
I need the collection to be sorted, so for now I just have to do this every time an item is added/removed from the list:
private void Pathologies_ListChanged(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemAdded || e.ListChangedType == ListChangedType.ItemDeleted)
{
_pathologies = new BindingList<PathologyModel>(_pathologies.OrderBy(x => x.Tooth.ToToothNumber()).ThenBy(x => x.DiagnosisDate).ToList());
}
}
It would be nice if this could be done automatically without the extra copying. But that's not my biggest problem right now.
I need the different XamDataGrids to have different filtered views of this same collection, which currently I am achieving with this:
public ICollectionView AllPathologies { get { return CollectionViewSource.GetDefaultView(PatientPathologies); } }
public ICollectionView TodaysPathologies { get { return CollectionViewSource.GetDefaultView(PatientPathologies.Where(x => x.DiagnosisDate.Date == DateTime.Today.Date)); } }
This almost works... I say almost because the views are showing the correct data, but the final requirement is that I also need to track the CurrentItemChanged event, so that I can enable/disable certain operations depending on which record the user is on. This works fine with the AllPathologies view, but does not ever get raised with TodaysPathologies, I'm guessing because it gets a different copy of the collection source every time you access the property? Strangely enough, the ListItem_Changed event still works properly against the original collection source.
I have tried making private CollectionViewSource objects to back the ICollectionView properties as I've seen in other articles, such as this:
private CollectionViewSource _todaysPathologies;
public ICollectionView TodaysPathologies { get { return _todaysPathologies.View; } }
...
_todaysPathologies = new CollectionViewSource{Source= _patientPathologies}.View;
But since the source is a BindingList I can't apply a filter predicate:
TodaysPathologies.CanFilter <--- false
So now I'm stuck. I place my fate in your hands, dear StackOverflowers.
"I need to know when a user edits a particular item in the collection, which other types of collections don't seem to support."
This isn't entirely true. A Collection used within a Class that inherits from INotifyPropertyChanged would solve this.
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
I would recommend making an inner class in your window page. With a Property for each value and the editable properties would call the NotifyPropertyChanged event. You would then have a collection of these inner class objects. The inner class would represent a row in the grid.
Another way I've solved this before is to specify text column where users enter information:
In XAML:
<DataGridTextColumn //... Binding="{Binding Path=Value, Mode=TwoWay}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox"/>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
In code:
private string value;
public string Value
{
get
{ return value; }
set
{
this.value = value;
NotifyPropertyChanged("Value");
}
}
The inner class contains this Value Property as well as a reference to an object of the class I used to have a collection of.
Now I have a collection of objects of this inner class, which stores both. I use this to display info of the class and have an extra column to add a value. I then make a new object out of the old class information + this value.
If you've found this helpful and would like me to go into more detail, let me know. I would be more than happy to. That or someone will post :P

Update UI when Entity Model changes in ViewModel

I am trying to come up with a good way of implementing the MVVM pattern using Entity-Framework where my entities are my models. My DataContext is my viewmodel. This is a small reproduction of the problem.
View
<TextBox Text="{Binding MyText}" />
ViewModel:
I have the requirement of needing to navigate record by record from my DB. When a button is clicked in the View a command is sent to the Viewmodel that executes nextRecord(). EF does its magic and _myObject is the next row/record from the database
public class myViewModel: INotifyPropertyChanged
{
private MyEntityObject _myObject;
public string MyText
{
get { return _myObject.MyText; }
set
{
if (_myObject.MyText != value)
{
_myObject.MyText = value;
OnPropertyChanged("MyText");
}
}
}
private void _nextRecord()
{
_myObject = myEntitiesContext.NextRecord() //pseudocode
}
}
Autogenerated Entity Model
public partial class MyEntityObject
{
public string MyText { get; set; }
}
Since the View has no knowledge of _myObject changing, it doesn't update when _myObject changes. A few approaches I have thought of.
I haven't tested wrapping my entities in a INotifyPropertyChanged wrapper class but am wary to do this as I have a lot of entity objects.
I could call OnPropertyChanged("...") for all properties, but some of my entities have a lot of properties to them, which would be ugly. Possible to use reflection to make it cleaner, but I may have properties that aren't databound.
I might be able to defer this to the UI, somehow refreshing the bindings when I click "Next Record", but this breaks MVVM and looks dirty
How can I get the UI to recognize changes from _myObject?
As I've mentioned in the comments, calling OnPropertyChanged("") or OnPropertyChanged(null) invalidates all properties and is equivalent to calling OnPropertyChanged for each and every property. This behavior is also documented here:
The PropertyChanged event can indicate all properties on the object
have changed by using either null or String.Empty as the property name
in the PropertyChangedEventArgs.
This means that you can simply add a call to OnPropertyChanged("") when you update your object to force WPF to reevaluate all bindings to your view model:
private void _nextRecord()
{
_myObject = myEntitiesContext.NextRecord();
OnPropertyChanged("");
}
That being said, I'd still go with #Anand's solution (+1). There's an ongoing debate on whether it's OK or not for the viewmodel to expose the model as a property, and I tend to go with exposing it until you need to introduce some view model specific logic. Most of the time you won't have to and it's not worth the trouble of wrapping model properties.
The problem with your code is that when _myObject changes the MyText property changed event is not fired. A work around would be to create a new property to hold you entity
and make this property as your Grids DataContext in your view as shown below. Now when this line is executed MyObject = myEntitiesObject.NextRecord() your view will be notified about the change.
public class myViewModel : INotifyPropertyChanged
{
private MyEntityObject _myObject;
public MyEntityObject MyObject
{
get { return _myObject; }
set {
if (_myObject != value)
{
_myObject = value;
OnPropertyChanged("MyObject");
}
}
}
private void _nextRecord()
{
MyObject = myEntitiesObject.NextRecord() //pseudocode
}
}
View:
<Grid DataContext="{Binding MyObject}">
<TextBlock Text="{Binding MyText}"/>
</Grid>
An extremely simple but not very elegant solution that I believe would meet needs: upon switching records, set the DataContext to null, then back to the ViewModel.
However, there are arguably more elegant alternatives that require more work to meet all requirements. See Anand's answer for an improvement upon this.
The tag in View should have the mode and UpdateSourceTrigger attribute set with values.

WPF binding not being set to UserControl DependencyProperty

I've got a ListView which is bound to a list of objects. When I select an item in the ListView, I catch a SelectionChanged event and then pass the selected object off to a details view.
protected void list_selectionChanged(object sender, SelectionChangedEventArgs e) {
var myObject = theList.SelectedItem as MyObjectType;
detailsView.DataContext = myObject;
}
detailsView is a UserControl in the same WPF as the ListView. It contains some XAML like so:
<Label Content="{Binding Path=deviceId}"></Label>
<l:MyUc deviceId="{Binding Path=deviceId}" />
Inside MyUC, I've got a DependencyProperty defined:
public static readonly DependencyProperty deviceIdProperty = DependencyProperty.Register("deviceId", typeof(Guid), typeof(MyUC), new FrameworkPropertyMetadata(null));
public Guid deviceId {
get { return (Guid)GetValue(deviceIdProperty); }
set { SetValue(deviceIdProperty, value); }
}
The Label shows the deviceId, but the property inside MyUC never gets set.
Can anyone spot my mistake?
When you use a Dependency Property in XAML, the set method never gets called. If you want to "see" this set, you need to add a property changed callback, as the binding mechanism directly sets the dependency property without your setter.
For details on how to implement this, see the PropertyChanged callbacks section of MSDN.
First, it would be helpful if you could add the actual XAML code where you define the ListView and it's properties.
Second, you should look at the output console (in Visual Studio debug session of course) and see whether there are binding errors regarding the bindings you defined.
It is very probable that the bindings provide values that does not fit the deviceId dependency property type and thus it never changes.

Is there such built-in control in silverlight?

I have a list of items with id and description(i can introduce key-value collection instead if needed). What i need is control that binded to viewmodel id property, but shows description of corresponding item/pair on it. Closest example i know is combobox, where i set DisplayMemberPath and SelectedValue/SelectedValuePath, but i don't need dropdown. So is there any in-built control in Silverlight for this?
(of course i can code one myself, it's easy and I can even just put some logic for viewmodel to get pair i need and bind it's description to simple textblock)
Edit: To illustrate what funcionality i need i coded simple example class. It actually satisfies my needs, but i still want to know if i can use built-in control.
public class CollectionItemDisplayControl:TextBox
{
public CollectionItemDisplayControl()
{
IsReadOnly = true;
}
public string SelectedID
{
get { return (string)GetValue(SelectedIDProperty); }
set { SetValue(SelectedIDProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedID. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedIDProperty =
DependencyProperty.Register("SelectedID", typeof(string), typeof(CollectionItemDisplayControl), new PropertyMetadata(new PropertyChangedCallback(OnSelectedIDChangedStatic)));
private static void OnSelectedIDChangedStatic(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CollectionItemDisplayControl originator = d as CollectionItemDisplayControl;
if (originator != null)
{
originator.OnSelectedIDChanged(e);
}
}
private void OnSelectedIDChanged(DependencyPropertyChangedEventArgs e)
{
string description = String.Empty;
string value = e.NewValue as string;
if (value != null)
{
foreach (var item in _items)
{
if (item.UniqueID == value)
{
description = item.Description;
break;
}
}
}
Text = description;
}
private IDataCollection _viewModel;
public IDataCollection ViewModel
{
get { return _viewModel; }
set
{
_viewModel = value;
if (_viewModel != null)
{
_items = _viewModel.Items;
}
}
}
private ObservableCollection<IUnique> _items = new ObservableCollection<IUnique>();
}
ItemClass contains two properties: ID and Description. I can place this control on the page, bind Items, and one-way bind SelectedID.
Edit 2: well i didn't make SelectedID DependencyProperty so binding won't work, but i will fix it right away
Edit 3: first snippet was sloppy and didn't work properly, so i fixed it.
If I understood properly,
You just need the right binding implemented.
(you do need a list? not just a single item, even if single it's similar just any control)
Bind the list to e.g. ItemsControl.
Set ItemsSource to your list of items
Then override ToString on your Item providing it's 'yours' really. If not you can make your own wrapper.
Within ToString output whatever is presenting your item, e.g. description.
That's a quickest solution, you can also make item template as you want.
EDIT:
well just put everything in the view model and bind to it - the TextBox, i.e.
Text={Binding SelectedText}
e.g.
...in your view model add SelectedText and SelectedID (and Items if needed) - properly do OnPropertyChanged.
Set SelectedID from view model or if 'bound' from another control that may change it.
Within set for SelectedID set the SelectedText.
No need for a control for things like that, it's all data binding really.

Categories

Resources