Reputation: 140
I have following xaml:
<DataGridTemplateColumn Header="123">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Role}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.Roles,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path = DataContext.UpdateUserCommand}" /> // bind fails there
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
The problem is that my command is not running on change, but if move combobox definition out of datagrid, my command is firing successfully. Seems like my bindging wrong, but i can't figure out what's wrong.
Upvotes: 1
Views: 580
Reputation: 128061
Just bind the ComboBox's SelectedItem
property to a SelectedRole
property in your view model.
In order to run an async action when the view model property changes, just attach an async PropertyChanged event handler in the view model:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ViewModel()
{
PropertyChanged += async (s, e) =>
{
if (e.PropertyName == nameof(SelectedRole))
{
await SomeAsyncAction();
}
};
}
private Role selectedRole;
public Role SelectedRole
{
get { return selectedRole; }
set
{
selectedRole = value;
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(nameof(SelectedRole)));
}
}
private async Task SomeAsyncAction()
{
...
}
}
Upvotes: 1
Reputation: 20461
You could try this (not 100% sure it'll work because cannot see all of your code)
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=DataContext.UpdateUserCommand}" />
Upvotes: 0