2014-09-25 31 views
0

我在我的MainWindowView.xaml中。它包含一個用戶控件。WPF命令:來自用戶控件的參數

我想用參數設置一個命令。該參數是gridControl(devexpress項目)的選定行。

我已經試過兩個結合,兩個都錯了(他們沒有找到參數):

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

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

如何有我寫的結合來傳遞的選定行UC中的gridControl?

我命令defition是:

public ICommand DeleteCommand { get; private set; } 
private void DeleteRecord(object parameter) 
{ 
    Debug.WriteLine(parameter); 
} 
[...] 
DeleteCommand = new DelegateCommand<object>(DeleteRecord, CanAlways); 
+2

不能你只綁定你的'gridControl'的selecteditem''和'你要button'一個的'CommandParameter'一些更多的信息你的ViewModel屬性?或者,您可以將'GridControl'的'selecteditem'綁定到一個屬性,並且該命令會在不傳遞參數的情況下得到它。 – Bolu 2014-09-25 09:01:56

+0

什麼類型應該是我的viewModel中的這個屬性? – 2014-09-25 09:04:08

+1

你在'gridControl'中使用了什麼類型? – Bolu 2014-09-25 09:05:42

回答

1

習慣上在WPF將數據綁定某種類型的ItemsSource屬性的集合和對象的類型的集合中的SelectedItem屬性的屬性(它使這個例子是使用一個ListBox沒有區別):

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

通過這種設置,可以將數據直接從CommandParameter道具綁定到YourSelectedItem財產erty:

<Button Command="{Binding DeleteCommand}" CommandParameter="{Binding YourSelectedItem}" 
    Style="{DynamicResource BtnToolBar}" /> 
+1

你的代碼假設所有的屬性都在相同的viewmodel內,那麼你根本就不需要CommandParameter,因爲YourSelectedItem屬性設置爲無論如何:) – blindmeis 2014-09-25 09:34:05

+0

這完全是真的......我是隻是通過問題作者分享的信息。 – Sheridan 2014-09-25 09:45:41

0

更一般的答案是:

,如果你想訪問從用戶控件對象/屬性,則用戶控件應公開與依賴項屬性的對象/屬性,你可以綁定到這個DP。

另一種方法是檢查UserControl的DataContext是否暴露該屬性,然後綁定到usercontrol的Datacontext.Property。

那麼對你的情況,我會需要你的視圖模型和視圖綁定