CommandBinding refresh on parameter changed











up vote
7
down vote

favorite
1












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?










share|improve this question
















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















up vote
7
down vote

favorite
1












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?










share|improve this question
















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













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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










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>





share|improve this answer























  • Does that refresh the commands CanExecute when you move over a new column?
    – MikeT
    Aug 31 '17 at 16:01













Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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>





share|improve this answer























  • Does that refresh the commands CanExecute when you move over a new column?
    – MikeT
    Aug 31 '17 at 16:01

















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>





share|improve this answer























  • Does that refresh the commands CanExecute when you move over a new column?
    – MikeT
    Aug 31 '17 at 16:01















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>





share|improve this answer














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>






share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)