Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Binding to IsEnabled Property in ListBox' ContextMenu's MenuItem
    primarykey
    data
    text
    <p>i was looking at the other threads regarding databinding to contextmenus but i couldnt figure out how to get it to work since the suggestions/answers wouldnt work for me.</p> <p>i have a listbox, which is bound to an ObversableCollection - that works fine.</p> <p>now i have a contextmenu inside that listbox. that contextmenu has 4 items to activate, deactivate etc the selected task (which is the item that is represented in the listbox).</p> <p>due to permissions, i need to control, wether the items in the contextmenu are enabled or disabled, so i have to set the IsEnabled-Property of the ContextMenuItem by binding it to the same Collection that the Listbox is bound to.</p> <p>but for some reason, the contextmenu items are not getting disabled - the property seems to be ignored.</p> <hr> <p><strong>EDIT:</strong> i have now implemented your suggestion:</p> <p>WPF</p> <pre><code>&lt;ListView Margin="10,10,10,55" Name="listviewCurrentJobs" ItemsSource="{Binding JobCollection}"&gt; &lt;ListView.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="Starten" Command="{Binding Path=startCommand}"/&gt; &lt;MenuItem Header="Stoppen" Command="{Binding Path=stopCommand}"/&gt; &lt;MenuItem Header="Aktivieren" Command="{Binding Path=enableCommand}"/&gt; &lt;MenuItem Header="Deaktivieren" Command="{Binding Path=disableCommand}"/&gt; &lt;MenuItem Header="Löschen" Command="{Binding Path=deleteCommand}"/&gt; &lt;/ContextMenu&gt; &lt;/ListView.ContextMenu&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="" Width="32"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Image Source="{Binding State}" Width="16"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Description}" /&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>C#</p> <pre><code>public class currentJob : MonitoringWindow { public string State { get; set; } public string Description { get; set; } public bool startPermitted { get; set; } public bool stopPermitted { get; set; } public bool enablePermitted { get; set; } public bool disablePermitted { get; set; } public bool deletePermitted { get; set; } public ICommand StartCommand { get; private set; } public ICommand StopCommand { get; private set; } public ICommand EnableCommand { get; private set; } public ICommand DisableCommand { get; private set; } public ICommand DeleteCommand { get; private set; } public currentJob() { StartCommand = new DelegateCommand(ExecuteStart, CanExecuteStart); StopCommand = new DelegateCommand(ExecuteStop, CanExecuteStop); EnableCommand = new DelegateCommand(ExecuteEnable, CanExecuteEnable); DisableCommand = new DelegateCommand(ExecuteDisable, CanExecuteDisable); DeleteCommand = new DelegateCommand(ExecuteDelete, CanExecuteDelete); } public bool CanExecuteStart() { return startPermitted; } public bool CanExecuteStop() { return stopPermitted; } public bool CanExecuteEnable() { return enablePermitted; } public bool CanExecuteDisable() { return disablePermitted; } public bool CanExecuteDelete() { return deletePermitted; } public void ExecuteStart() { currentJob curJob = ((currentJob)listviewCurrentJobs.SelectedItem); string curJobName = curJob.Name; if (new TaskService().GetFolder("DocuWare Notifications").Tasks[curJobName].Enabled == false) new TaskService().GetFolder("DocuWare Notifications").Tasks[curJobName].Enabled = true; new TaskService().GetFolder("DocuWare Notifications").Tasks[curJobName].Run(); loadJobs(); } public void ExecuteStop() { if (new TaskService().GetFolder("DocuWare Notifications").Tasks[((currentJob)listviewCurrentJobs.SelectedItem).Name].Enabled == true) { new TaskService().GetFolder("DocuWare Notifications").Tasks[((currentJob)listviewCurrentJobs.SelectedItem).Name].Stop(); loadJobs(); } } public void ExecuteEnable() { new TaskService().GetFolder("DocuWare Notifications").Tasks[((currentJob)listviewCurrentJobs.SelectedItem).Name].Enabled = true; loadJobs(); } public void ExecuteDisable() { new TaskService().GetFolder("DocuWare Notifications").Tasks[((currentJob)listviewCurrentJobs.SelectedItem).Name].Enabled = false; loadJobs(); } public void ExecuteDelete() { new TaskService().GetFolder("DocuWare Notifications").DeleteTask(((currentJob)listviewCurrentJobs.SelectedItem).Name); if (isMsSql) { mssqlconn.Open(); new SqlCommand("DELETE FROM dbo.DocuWareNotifications WHERE NAME = '" + ((currentJob)listviewCurrentJobs.SelectedItem).Name + "'", mssqlconn).ExecuteNonQuery(); mssqlconn.Close(); } else { mysqlconn.Open(); new MySqlCommand("DELETE FROM DocuWareNotifications WHERE NAME = '" + ((currentJob)listviewCurrentJobs.SelectedItem).Name + "'", mysqlconn).ExecuteNonQuery(); mysqlconn.Close(); } loadJobs(); } } public partial class MonitoringWindow : MetroWindow { [...] foreach (Task task in new TaskService().GetFolder("DocuWare Notifications").Tasks) { if (task != null) { currentJob item = new currentJob(); switch (task.State) { case TaskState.Disabled: item.State = "/DWNotDesigner;component/images/disabled.png"; item.Description = task.Name; break; case TaskState.Ready: item.State = "/DWNotDesigner;component/images/active.png"; item.Description = task.Name; break; case TaskState.Running: item.State = "/DWNotDesigner;component/images/working.png"; item.Description = task.Name; break; } item.startPermitted = startPermitted; item.stopPermitted = stopPermitted; item.enablePermitted = enablePermitted; item.disablePermitted = disablePermitted; item.deletePermitted = deletePermitted; _jobCollection.Add(item); } } } </code></pre> <p>for some reason there is no change!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload