CommandBinding refresh on parameter changed
up vote
7
down vote
favorite
I need the command I am executing on a datagrids context menu to know which column was clicked on.
XAML
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}" ContextMenuOpening="DataGrid_ContextMenuOpening">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Column" Command="{Binding AddColumn}"/>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Code behind:
private void DataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var current = e.OriginalSource as DependencyObject;
while(current != null && !(current is DataGridCell))
{
current = VisualTreeHelper.GetParent(current);
}
var cell = current as DataGridCell;
foreach (var item in dataGrid.ContextMenu.Items.OfType<MenuItem>())
{
item.CommandParameter = cell.Column.DisplayIndex;
(item.Command as DelegateCommand<object>)?.RaiseCanExecuteChanged();
}
}
I'm having to break MVVM by having the view tell the VM command that its execution has changed because the command binding doesn't seem to refresh on the parameter changing. Is there a better way to do this?
c# wpf mvvm
bumped to the homepage by Community♦ 41 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
7
down vote
favorite
I need the command I am executing on a datagrids context menu to know which column was clicked on.
XAML
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}" ContextMenuOpening="DataGrid_ContextMenuOpening">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Column" Command="{Binding AddColumn}"/>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Code behind:
private void DataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var current = e.OriginalSource as DependencyObject;
while(current != null && !(current is DataGridCell))
{
current = VisualTreeHelper.GetParent(current);
}
var cell = current as DataGridCell;
foreach (var item in dataGrid.ContextMenu.Items.OfType<MenuItem>())
{
item.CommandParameter = cell.Column.DisplayIndex;
(item.Command as DelegateCommand<object>)?.RaiseCanExecuteChanged();
}
}
I'm having to break MVVM by having the view tell the VM command that its execution has changed because the command binding doesn't seem to refresh on the parameter changing. Is there a better way to do this?
c# wpf mvvm
bumped to the homepage by Community♦ 41 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
there is no CommandParameter binding as i'm not aware of a way to bind to bind a contextMenu to the sub element its called on, but the issue is that there is no link between the Commnd parameter and the Cimmand CanExecute call even though the parameter has a baring on it the command can execute or not
– MikeT
May 15 '17 at 9:46
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I need the command I am executing on a datagrids context menu to know which column was clicked on.
XAML
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}" ContextMenuOpening="DataGrid_ContextMenuOpening">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Column" Command="{Binding AddColumn}"/>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Code behind:
private void DataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var current = e.OriginalSource as DependencyObject;
while(current != null && !(current is DataGridCell))
{
current = VisualTreeHelper.GetParent(current);
}
var cell = current as DataGridCell;
foreach (var item in dataGrid.ContextMenu.Items.OfType<MenuItem>())
{
item.CommandParameter = cell.Column.DisplayIndex;
(item.Command as DelegateCommand<object>)?.RaiseCanExecuteChanged();
}
}
I'm having to break MVVM by having the view tell the VM command that its execution has changed because the command binding doesn't seem to refresh on the parameter changing. Is there a better way to do this?
c# wpf mvvm
I need the command I am executing on a datagrids context menu to know which column was clicked on.
XAML
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}" ContextMenuOpening="DataGrid_ContextMenuOpening">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Column" Command="{Binding AddColumn}"/>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Code behind:
private void DataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var current = e.OriginalSource as DependencyObject;
while(current != null && !(current is DataGridCell))
{
current = VisualTreeHelper.GetParent(current);
}
var cell = current as DataGridCell;
foreach (var item in dataGrid.ContextMenu.Items.OfType<MenuItem>())
{
item.CommandParameter = cell.Column.DisplayIndex;
(item.Command as DelegateCommand<object>)?.RaiseCanExecuteChanged();
}
}
I'm having to break MVVM by having the view tell the VM command that its execution has changed because the command binding doesn't seem to refresh on the parameter changing. Is there a better way to do this?
c# wpf mvvm
c# wpf mvvm
edited Apr 16 '17 at 18:41
Jamal♦
30.2k11115226
30.2k11115226
asked Apr 10 '17 at 13:11
MikeT
17616
17616
bumped to the homepage by Community♦ 41 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 41 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
there is no CommandParameter binding as i'm not aware of a way to bind to bind a contextMenu to the sub element its called on, but the issue is that there is no link between the Commnd parameter and the Cimmand CanExecute call even though the parameter has a baring on it the command can execute or not
– MikeT
May 15 '17 at 9:46
add a comment |
there is no CommandParameter binding as i'm not aware of a way to bind to bind a contextMenu to the sub element its called on, but the issue is that there is no link between the Commnd parameter and the Cimmand CanExecute call even though the parameter has a baring on it the command can execute or not
– MikeT
May 15 '17 at 9:46
there is no CommandParameter binding as i'm not aware of a way to bind to bind a contextMenu to the sub element its called on, but the issue is that there is no link between the Commnd parameter and the Cimmand CanExecute call even though the parameter has a baring on it the command can execute or not
– MikeT
May 15 '17 at 9:46
there is no CommandParameter binding as i'm not aware of a way to bind to bind a contextMenu to the sub element its called on, but the issue is that there is no link between the Commnd parameter and the Cimmand CanExecute call even though the parameter has a baring on it the command can execute or not
– MikeT
May 15 '17 at 9:46
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Here's an example using the CommandParameter. It's a little verbose, but ContextMenu bindings often are.
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.CurrentColumn.DisplayIndex}" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Does that refresh the commands CanExecute when you move over a new column?
– MikeT
Aug 31 '17 at 16:01
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Here's an example using the CommandParameter. It's a little verbose, but ContextMenu bindings often are.
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.CurrentColumn.DisplayIndex}" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Does that refresh the commands CanExecute when you move over a new column?
– MikeT
Aug 31 '17 at 16:01
add a comment |
up vote
0
down vote
Here's an example using the CommandParameter. It's a little verbose, but ContextMenu bindings often are.
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.CurrentColumn.DisplayIndex}" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Does that refresh the commands CanExecute when you move over a new column?
– MikeT
Aug 31 '17 at 16:01
add a comment |
up vote
0
down vote
up vote
0
down vote
Here's an example using the CommandParameter. It's a little verbose, but ContextMenu bindings often are.
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.CurrentColumn.DisplayIndex}" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
Here's an example using the CommandParameter. It's a little verbose, but ContextMenu bindings often are.
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Table}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove Column" Command="{Binding RemoveColumn}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.CurrentColumn.DisplayIndex}" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
edited Dec 29 '17 at 19:15
Nkosi
2,108619
2,108619
answered Aug 31 '17 at 15:49
Tyler Daniels
20529
20529
Does that refresh the commands CanExecute when you move over a new column?
– MikeT
Aug 31 '17 at 16:01
add a comment |
Does that refresh the commands CanExecute when you move over a new column?
– MikeT
Aug 31 '17 at 16:01
Does that refresh the commands CanExecute when you move over a new column?
– MikeT
Aug 31 '17 at 16:01
Does that refresh the commands CanExecute when you move over a new column?
– MikeT
Aug 31 '17 at 16:01
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f160312%2fcommandbinding-refresh-on-parameter-changed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
there is no CommandParameter binding as i'm not aware of a way to bind to bind a contextMenu to the sub element its called on, but the issue is that there is no link between the Commnd parameter and the Cimmand CanExecute call even though the parameter has a baring on it the command can execute or not
– MikeT
May 15 '17 at 9:46