Multiple Binding - wpf

In my WPF Caliburn.Micro application, I have a datagrid and a checkbox with a corresponding ModelView bool property. I need to bind the checkbox to one of the datagrid's fields OneWay (which is easy). But also I want to bind the same checkbox to the property OneWayToSource. Could you please tell me how I can do that? I don't see how Multibinding can help here.
Thanks.

I don't know if this is a checkbox per row of the DataGrid, or a checkbox for a row with a particular id or index. Either way, you can use TwoWay binding, which will be the default anyway if your view model property has a getter and setter.
Your view model property should point to the instance of the record that the DataGrid is binding to.
E.g.
View Model
public ObservableCollection<Item> MyGridItems { get; set; }
public MyViewModel()
{
this.MyGridItems = ...
this.MySpecialItem = this.MyGridItems[0];
}
public Item MySpecialItem
{
get { return this.mySpecialItem; }
set { this.mySpecialItem = value; // notify of property change here }
}
View
<CheckBox IsChecked="{Binding MySpecialItem.MyBooleanProperty}" />

Related

MVVM Light View not refreshing binding property

I use MVVM Light.
My Model inherits from MVVM Light's ObservableObject.
The Model has a property X
public float X
{
get
{
return x_;
}
set
{
Set<float>(() => this.X, ref x_, value);
}
}
In my ViewModel I have a property X
public float X
{
get
{
myModel.X;
}
}
My View has a label where the content is binding to the ViewModel property X.
(DataContext is set to ViewModel)
When the value of property X in Model is updated from code, the label in the view never gets updated.
Question what is the recommended way to make sure that the View correctly reflects the updated value?
(If I make a property in my ViewModel that returns the Model, I could bind direclty to Model.X in my View. But I want my View to bind to the ViewModel not directly down to the Model)
When myModel.X fires the PropertyChanged event, that does not automatically fire the ViewModel's PropertyChanged.
Moreover, if your Model class already implements INotifyPropertyChanged, there is no need to duplicate the X property in the ViewModel class.
Just turn the myModel member into a public property
public class ViewModel
{
public Model MyModel { get; set; }
}
and bind to it by {Binding MyModel.X}.

Selection changed event of combobox in wpf mvvm

I am new to wpf and MVVM, and I've spent all day trying to get the value of a ComboBox to my ViewModel on SelectionChanged. I want to call a function in the selection changed process. In mvvm, what is the solution for it?
In MVVM, we generally don't handle events, as it is not so good using UI code in view models. Instead of using events such as SelectionChanged, we often use a property to bind to the ComboBox.SelectedItem:
View model:
public ObservableCollection<SomeType> Items { get; set; } // Implement
public SomeType Item { get; set; } // INotifyPropertyChanged here
View:
<ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding Item}" />
Now whenever the selected item in the ComboBox is changed, so is the Item property. Of course, you have to ensure that you have set the DataContext of the view to an instance of the view model to make this work. If you want to do something when the selected item is changed, you can do that in the property setter:
public SomeType Item
{
get { return item; }
set
{
if (item != value)
{
item = value;
NotifyPropertyChanged("Item");
// New item has been selected. Do something here
}
}
}

how to populate a datagrid after selecting an item from combobox?

i have an combobox and datagrid when user select a data from combobox the grid will be populated according to that using MVVM and entity framework
Advance thanks
In your ViewModel, create a SelectedItem property which notifies on change as so:
private object _selectedItem
public object SelectedItem
{
get
{
return _selectedItem;
}
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem")
}
}
Bind your SelectedItem property of your combo box to this property.
Then watch for a change on SelectedItem and change your datagrid's source property accordingly.

Caliburn Micro: how to set binding UpdateSourceTrigger?

I've been exploring the Caliburn Micro MVVM Framework just to get a feel for it, but I've run into a bit of a problem. I have a TextBox bound to a string property on my ViewModel and I would like the property to be updated when the TextBox loses focus.
Normally I would achieve this by setting the UpdateSourceTrigger to LostFocus on the binding, but I don't see any way to do this within Caliburn, as it has setup the property binding for me automatically. Currently the property is updated every time the content of the TextBox changes.
My code is very simple, for instance here is my VM:
public class ShellViewModel : PropertyChangeBase
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyOfPropertyChange(() => Name);
}
}
}
And inside my view I have a simple TextBox.
<TextBox x:Name="Name" />
How to I change it so the Name property is only updated when the TextBox loses focus, instead of each time the property changes?
Just set the binding explictly for that instance of the TextBox and Caliburn.Micro won't touch it:
<TextBox Text="{Binding Name, UpdateSourceTrigger=LostFocus}" />
Alternatively, if you want to change the default behaviour for all instances of TextBox, then you can change the implementation of ConventionManager.ApplyUpdateSourceTrigger in your bootstrapper's Configure method.
Something like:
protected override void Configure()
{
ConventionManager.ApplyUpdateSourceTrigger = (bindableProperty, element, binding) =>{
#if SILVERLIGHT
ApplySilverlightTriggers(
element,
bindableProperty,
x => x.GetBindingExpression(bindableProperty),
info,
binding
);
#else
if (element is TextBox)
{
return;
}
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
#endif
};
}

Implementing a "Search as you type" in Silverlight

I am trying to implement a search as you type screen in my Silverlight application. The idea is that I have a screen with a textedit control and a listbox. The listbox is filled with all my data.
When the user types something in the textbox the following happens:
All the items that are not containing all the letters from the user input are hidden.
The matching letters of the visible list items are highlighted with a different color.
I am not sure how to start with this, so all pointers, samples and hints are welcome!
I would suggest using a CollectionViewSource. A CollectionViewSource has the ability to filter items. You can bind your ListBox to the CollectionViewSource and handle Filter event to do the filtering. Bind your "Search Box" to a Text property which you can use in your Filter event. You can handle the "KeyUp" event of the TextBox control to kick off your filtering, by calling the Refresh method on the CollectionViewSource View.
Filtering Data using CollectionViewSource: http://xamlcoder.com/blog/2010/10/27/filtering-data-using-collectionviewsource/
http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.filter.aspx
http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.aspx
http://timheuer.com/blog/archive/2009/11/04/updated-silverlight-3-datagrid-grouping-data-pagedcollectionview.aspx
http://bea.stollnitz.com/blog/?p=392
Sudo code:
// ViewModel - properties should fire NotifyPropertyChanged
public class ViewModel : INotifyPropertyChanged
{
public ViewModel
{
this.Data = new CollectionViewSource();
this.Data.Source = this.GenerateObjects();
this.Data.Filter += (s,e) =>
{
// TODO: add filter logic
DataObject item = e.Item as DataObject;
return item.Name.Contains(this.SearchText);
};
}
public string SearchText{get;set;}
public CollectionViewSource Data {get;set;}
private List<DataObject> GenerateObjects(){ // generate list of data objects }
}
// View XAML
<StackPanel>
<TextBox Text="{Binding SearchText, Mode=TwoWay}" KeyUp="OnKeyUp"/>
<ListBox ItemsSource="{Binding Data.View}"/>
</StackPanel>
// View Code Behind
public class View : UserControl
{
public View() { this.DataContext = new ViewModel(); }
private ViewModel ViewModel { get { return this.DataContext as ViewModel; } }
private OnKeyUp()
{
this.ViewModel.Data.View.Refresh();
}
}
You may want to start with the AutocompleteBox from the Silverlight Toolkit.
It has a number of handy points where you would be able to extend it's functionality, for example in the instance searching your pool of values.

Resources