I have menu with XML like this:
<MenuItem x:Name="MenuItemCameras" Header="Cameras" ItemsSource="{Binding LocalCameras}" >
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding DisplayName}" IsCheckable="True" IsChecked="{Binding IsStreamingVideo}" IsEnabled="{Binding CanStreamVideo}"
Command="{Binding DataContext.CommandSelectLocalCamera, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
Click="MenuItemCameras_OnClick"/>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
I want to iterate like this:
foreach (MenuItem mMenuItem in MenuItemCameras) {
//some code
}
How can I do this ?
To retrieve the corresponding MenuItem, you can do this
foreach (var item in MenuItemCameras.Items) {
MenuItem menuItem = (MenuItem)MenuItemCameras.ItemContainerGenerator.ContainerFromItem(item);
//some code
}
But there's a downside, as long as the corresponding MenuItem isn't created ItemContainerGenerator.ContainerFromItem will return null
Related
I need to get selected item from the combo box as command parameter.
but for the first time selected item is received as old value in the command parameter.
XAML:
<DataTemplate x:Uid="DataTemplate_2">
<control:ComboBox x:Uid="ComboBoxType"
x:Name="AstmComboBox"
HorizontalAlignment="Stretch"
Height="20"
FlowDirection="LeftToRight"
HorizontalContentAlignment="Right"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type control:UserControl}}, Path=DataContext.InclusionTypes}"
SelectedItem="{Binding Path=ClassName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding DataContext.IsReclassificationAllowed, RelativeSource={RelativeSource AncestorType=control:DataGrid}}"
Width="Auto">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type control:UserControl}},Path=DataContext.ClassChangedCommand, Mode=OneWay}"
CommandParameter="{Binding ElementName=AstmComboBox, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</control:ComboBox>
</DataTemplate>
in viewmodel i have relaycommand
public RelayCommand ClassChangedCommand
{
get
{
return _classChangedCommand ?? (_classChangedCommand = new RelayCommand(ClassChanged));
}
}
private void ClassChanged(object obj)
{
{
SetSelectedItemsType(obj as string);
}
}
Here for the first time obj received has the old value of the combo box item.
I have a WPF code like this:
<ListBox SelectedItem="{Binding SelectedItem}">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete" Click="MenuItem_Delete_Click" />
<MenuItem Header="Replace" Click="MenuItem_Replace_Click">
<ListBox SelectionMode="Single" SelectedItem="{Binding ReplaceItem}" />
</MenuItem>
<MenuItem Header="Insert" Click="MenuItem_Insert_Click">
<ListBox SelectionMode="Single" SelectedItem="{Binding InsertItem}}" />
</MenuItem>
</ListBox>
But this goes like follows:
When Mouse leave
When Mouse On
So how should I fix this?Thanks in advance!
Instead of using a listbox on the MenuItem, why don't you try to add menuitems to the existing menuitem (for your case is "Insert" and "Replace")
Let"s see my example.
MenuItem mi = new MenuItem();
mi.Header = "PX1-20T-D-B";
NameOfYourMenuItem.Items.Add(mi);
mi.Click += new RoutedEventHandler(MenuItemClick);
You can also add the eventHandler for each one of them.
Instead of using a ListBox in the MenuItem or adding sub menu items in code behind, try using DataTemplates. I assume that you have a collection containing your list box items.
<MenuItem Header="Replace" Click="MenuItem_Replace_Click" ItemsSource="{Binding ReplaceItemsCollection}">
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding}" Click="replaceSubMenuItem_Clicked"/>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
<MenuItem Header="Replace" Click="MenuItem_Replace_Click" ItemsSource="{Binding InsertItemsCollection}">
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding}" Click="insertSubMenuItem_Clicked"/>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
In your code behind add:
private void replaceSubMenuItem_Clicked(object sender, EventArgs e)
{
// sender is the MenuItem. Just parse it.
}
private void insertSubMenuItem_Clicked(object sender, EventArgs e)
{
// ...
}
i have a devexpress ListBoxEdit control with a context menu.
hear is my design`
<dxe:ListBoxEdit x:Name="lstEventsList" Grid.Column="1" Grid.Row="3" Tag="{Binding DataContext}" >
<mvvm:Interaction.Triggers>
<mvvm:EventToCommand EventName="SelectedIndexChanged" Command="{Binding GetReferenceCodeListOnEventSelect}" PassEventArgsToCommand="True" />
</mvvm:Interaction.Triggers>
<dxe:ListBoxEdit.ContextMenu>
<ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Command="{Binding SyncToMediaDevice}" Header="SYNC"
CommandParameter="{Binding}"/>
</ContextMenu>
</dxe:ListBoxEdit.ContextMenu>
</dxe:ListBoxEdit>
now problem is menu context command are not working..binding property is
public ICommand SyncToMediaDevice
{
get
{
return _syncToMediaDevice;
}
set
{
_syncToMediaDevice = value;
}
}`
Please help me thanks`
I have a problem regarding the state of a menuitem in ContextMenu. I have a ObversableCollection of Cars. The cars are visualized in a ListBox, for each ListBox Item I want a ContextMenu. In that ContextMenu there is an option ReserveCar.
They problem I'm having is that the CanExecute of the Car is only executed once when I right click any car. They CanExecute will not be called anymore after that when I right click an other Car.
This causes that when I rightclick a Car which can be Reserved, the MenuItem is active, but when I then rightclick another which I should not be able to reserve the MenuItem remains active (because CanExecute is not called again).
<ListBox
ItemsSource="{Binding Cars}"
SelectedItem="{Binding SelectedCar}">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Reserve Car"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"
Command="{Binding ReserveCarCommand}">
<MenuItem.Icon>
<Image Source="{StaticResource ReserveCarIcon}" Width="24" Height="24"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
My ViewModel:
private RelayCommand<Car> _reserveCarCommand;
public ICommand ReserveCarCommand
{
get { return _reserveCarCommand ?? (_reserveCarCommand = new RelayCommanReserveCar, CanReserveCar)); }
}
public bool CanReserveCar(Car car)
{
return !car.IsReserved && ReservationsAreOpen;
}
public void ReserveCar(Car car)
{
car.IsReserved = true;
}
Also when I manually refresh the Command when doing something, the CanExecute is called with a null as parameter, so thats not working either.
if (_reserveCarCommand != null) _reserveCarCommand .RaiseCanExecuteChanged();
Try binding the context menu on ListBoxItem instead of ListBox. As binding of context menu for ListBox happen only at the fist right click so CanExectute will not fire after first right click.
<ListBox Name="simpleListBox"
ItemsSource="{Binding Cars}"
SelectedItem="{Binding SelectedCar}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
...
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
I have a UserControl with a ListView in it.
In addition I have a class listViewItems.cs with a DisplayMemberBinding to a GridView in listView.
Each ListView-Item has a context-menu.
Now I'm trying to enable/disable the context-menu-items depending if a value in class ListViewItems is null
I've tried a binding to the IsEnabled property to the boolean value ShowResItemEn in class ListViewItems.cs but it does not work.
DataOutput.xaml
<ListView.Resources>
<ContextMenu x:Name="cmListView" x:Key="ItemContextMenu" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
<MenuItem x:Name="itmRes"
Header="Reservierungen anzeigen"
IsEnabled="{Binding PlacementTarget.SelectedItem.ShowResItemEn, RelativeSource={RelativeSource FindAncestor,AncestorType=ContextMenu}}"
Command="{Binding ShowResItemCmd}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" >
</MenuItem>
</ContextMenu>
</ListView.Resources>
class ListViewItems.cs
public Boolean ShowResItemEn
{
get
{
return (auftrNr[0] == null) ? false : true;
}
}
Ok it works now. I've setted the AncestorType wrong
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},Path=SelectedItem.ShowBesItemEn}"