Silverlight 3.0: How to bind DataGridColumn to an ICommand wherein the DataGrid is bound to an ItemsSource - datagridcolumn

I have a SL3 DataGrid bound to a collection. One column of the datagrid is a HyperlinkButton column and I want to bind the click event of the column to an ICommand present in the VM.
pseudo code:
DataGrid ItemsSource="{Binding someCollection}"
DataGridHyperLinkColumn Commands.Command="{Binding myClickCommand}"
Now in this scenario the Commands.Command is trying to locate the myClickCommand within the someCollection instead of getting it from the VM myClickCommand property.
I have also tried the fix Commands.Command="{Binding Path=DataContext.myClickCommand, ElementName=nameOfUserControl}" but that fails as well.
What is the way out...? I don't want to use BindingHelper since SL3 already supports ElementBinding...

voila...there is a solution already available from Dan Wahlin out here: http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx

Related

Q: Update viewmodel property from a listview binded textbox

i've got a textbox which text is binded to a listview selecteditem as follows:
<TextBox x:Name="txtAdditional" Width="300" Text="{Binding ElementName=lstPersons, Path=SelectedItem.Additional, Mode=OneWay}" />
Now i like to implement a mechanism to add new items to the listview using this textbox to get the actual data. So i would like to bind the text of the textbox to a property of the viewmodel so it can be processed by a command.
So it the textboxes text has to be binded to the listviews selecteditem and additionally to a property of my viewmodel.
I've searched around and found some approaches but i can't help to think that there should be some simpler mechanism to archive this goal.
What I found by now:
Using MultiBindung with some kind of ValueConverter? So it seems to me that this is primary for displaying and not for updating a viewmodels property.
The use of some selfdefined custom control?
The use of an BindingProxy with in- and out-dependencyproperties like in Impossible WPF Part 1: Binding Properties?
Is there another, simpler solution to this or would i have to use one of those above?
And if one should use one of those approaches, which one whould you choose?
I can't help but thinking that this issue hasn't been given some thoughts already, using an mvvm pattern?! ;-)
yes thanks to the hint from BionicCode.
We, or better I should have to think the MVVM concept out. Of course no - or at least as little code behind as possible.
I added the property "selectedPerson" to my viewmodel and bound the SelectedItem of the listview to this property. So the object related properties were at hand directly through the "selected" object in my viewmodel and there was no need anymore to access the textbox content at all.
Thanks to BionicCode for the hint!

datarowview binding to combobox datatemplate

I have a wpf application using mvvm. I am trying to bind my datatable to a combobox that has a datatemplate of 2 textblocks. How do I specify the binding on these text blocks to bind to datarowview.item["columnname"]
In short, what I am trying to achieve is to bind 2 columns of my datatable to a multicolumn combo box
It turns our that you can simply bind to the column names just like how you blind to an object's public property. WPF is smart enough to figure it out.

How do i access a combobox text in a usercontrol from the wpf forms viewmodel?

I have a UserControl with 4 combobox bound to collections in viewmodel for that usercontrol.
I have used this control in a wpf form. This wpf form has its own viewmodel.
How do i access the text from the 4 comboboxes within the wpf form's viewmodel?
EDIT: i saw that you have different viewmodels. now it depends of the use of your usercontrol and the use of mvvm:)
you can use messenger or eventaggregator to comunicate the seleteditems from usercontrolviewmodel to mainviewmodel.
you can also use RelativeSource binding in your usercontrol to bind the selecteditem to your mainviewmodel directly (usercontrol then is just a composition of controls).
you can can rid of the usercontrol viewmodel and put all in the mainviewmodel and take my old example
you can create DependencyProperties for the SelectedItems in your usercontrol!(not usercontrol viewmodel!) and bind these to the properties in your mainviewmodel. i think thats the cleanest way if the usercontrol should be a real usercontrol.
old example:
in your viewmodel: //the real code should of course implement INotifyPropertyChanged and raise it properly
public ObservableCollection<string> MyFirstCollection {get; set;}//init once, add,remove,clear to alter
public string MySelectedCombobox1Value {get;set;}
in your usercontrol:
<ComboBox ItemsSource="{MyFirstCollection }" SelectedItem="{Binding MySelectedCombobox1Value, Mode=TwoWay}" />
thats all relating to your question. be sure that you set the DataContext right. you can check this with tools like snoop. the code i posted expected that the dataconext for the combobox is the viewmodel.
The UserControl should inherit the data context of the form you're adding it to which would be the view model. Any bindings in the UserControl would then be relative to the inherited data context. Have you tried binding to a view model property to ComboBox.Text?
UPDATE
Sorry, misread your question. Didn't see that the user control already has its own view model.
While it seems like there's a better approach, you could expose dependency properties on the user control that exposé the text of each combobox. Just thinking out loud.
The only clean way to do this is with binding, and the only way that would be recommended is if the user control exposes a DependencyProperty for the ViewModel or the individual text properties (as was suggested by sellmeadog) for consumption. Then you can have a property in the parent ViewModel that binds directly to that Dependency Property.

Wpf Datagrid Delete Row issue

I am new to WPF. I want to delete row from datagrid runtime.When I try to delete row like this
Datagrid.Items.Remove(eRow);
It gives me an error " The error is: Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
I read online that you could use ObservationCollection and InotifyPropertyChangedEvent but I dont know how to implement it.
I have remove button like this
This is datagrid
<ctrls:RhinoDataGrid x:Name="dataGrid" Grid.Row="1" Margin="5" ItemsSource="{Binding Model.CurrentDataTable}"
Style="{StaticResource RhinoDataGridBaseStyle}" IsReadOnly="{Binding Model.IsLinkFile}"
SelectedValue="{Binding Model.CurrentDataRow}" SelectedValuePath="Row"
>
</ctrls:RhinoDataGrid>
Pls help me. Thanks.
Your DataGrid's ItemsSource has a binding on Model.CurrentDataTable. If you want to delete a row, you will have to delete the item in that collection. But the DataGrid won't notice that modification, if the collection does not implement INotifyCollectionChanged.
.NET has a builtin ObservableCollection, which implements INotifyCollectionChanged. If you use this collection type for CurrentDataTable, the DataGrid will update itself, when the collection is modified.
WPF Data binding means that you very rarely manipulate the user interface directly. What you're trying to do is directly remove the row from the grid control on the UI, which is how you might have approached things in Winforms.
With WPF data binding the user interface reacts to the underlying data. So in your case the grid is bound onto (or "watching") the contents of the ItemsSource specified by the binding : Binding Model.CurrentDataTable
To delete a row, you need to remove it from the underlying data and the UI will automatically reflect the change.
This is what ObservableCollection and INotifyPropertyChanged are all about - you really need to read up on them if you're doing WPF development!

Binding DataGrid control selected items to Command in View Model?

I am trying to take the currently selected objects (rows) in a DataGrid control and send them directly to the View Model (MVVM pattern) without any code in the XAML code behind, using ICommand.
I have done this with a Button since ButtonBase implements the Command property - the DataGrid does not. It appears that using Commands between the View and the View Model when using buttons is the convention, but what about other controls?
Is there a way to take the currently selected items and send this in a Command to the View Model?
Here's an article on the subject. It's about Silverlight, but the guidance should be useful for WPF as well. Basically it says to use something like EventToCommand (from MVVMLight Toolkit) to translate the SelectionChanged event into an ICommand.
I like to use the code found here. It is some AttachedProperties which let you hook up Commands to just about any Event
<DataGrid
local:CommandBehavior.Event="MouseDown"
local:CommandBehavior.Command="{Binding SomeCommand}"
local:CommandBehavior.CommandParameter="{Binding SelectedItem}"/>

Resources