0

I'm in my MainWindowView.xaml. It includes a usercontrol.

I'm trying to set a command with a parameter. This parameter is the selected row of a gridControl (devexpress item).

I have tried two binding, both wrong (they don't find the parameter):

<Button Command="{Binding DeleteCommand}" CommandParameter="{Binding Path=lst1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type uc:ucImpianti}}}" Style="{DynamicResource BtnToolBar}"/>

and

<Button Command="{Binding DeleteCommand}" CommandParameter="{Binding ElementName=lst1, Path=FocusedRow}" Style="{DynamicResource BtnToolBar}"/>

How have I to write the binding to pass the selected row of a gridControl in a UC?

My command defition is:

public ICommand DeleteCommand { get; private set; }
private void DeleteRecord(object parameter)
{
    Debug.WriteLine(parameter);
}
[...]
DeleteCommand = new DelegateCommand<object>(DeleteRecord, CanAlways);
Piero Alberto
  • 3,823
  • 6
  • 56
  • 108
  • 2
    Can't you just Binding the `selecteditem` of your `gridControl` and the `CommandParameter` of your `button` to a property in your ViewModel? Or, you may just binding the `selecteditem` of the `GridControl` to a property, and the command will get it without passing the parameter. – Bolu Sep 25 '14 at 09:01
  • and what type should be this property in my viewModel? – Piero Alberto Sep 25 '14 at 09:04
  • 1
    What type do you use in the `gridControl`? – Bolu Sep 25 '14 at 09:05
  • my gridocntrol is binded (with itemsource) to an observableCollection... should the property be "tabImpianti" type? – Piero Alberto Sep 25 '14 at 09:07
  • 2
    Yes, use the same type – Bolu Sep 25 '14 at 09:12

2 Answers2

1

It is customary in WPF to data bind a collection of a certain type to the ItemsSource property and a property of the type of object in the collection to the SelectedItem property (it makes no difference that this example uses a ListBox):

<ListBox ItemsSource="{Binding YourCollection}" 
    SelectedItem="{Binding YourSelectedItem}" ... />

With this set up, you can data bind directly to the YourSelectedItem property from the CommandParameter property:

<Button Command="{Binding DeleteCommand}" CommandParameter="{Binding YourSelectedItem}"
    Style="{DynamicResource BtnToolBar}" />
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 1
    your code assumes thats all properties are within the same viewmodel, then you dont even need a CommandParameter at all, because the YourSelectedItem property is set anyway :) – blindmeis Sep 25 '14 at 09:34
  • That is entirely true... I'm just going by what information the question author has shared. – Sheridan Sep 25 '14 at 09:45
0

a more general answer would be:

if you wanna access an object/property from a usercontrol, then the UserControl should expose the object/property with a Dependency Property and you can bind to this DP.

another way would be to check if the DataContext of the Usercontrol expose the property then bind to the Datacontext.Property of the usercontrol.

so for your case i would need some more information of your Viewmodel and View bindings

blindmeis
  • 22,175
  • 7
  • 55
  • 74