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}"
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 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
I am following the MVVM pattern. I have a grid with few columns one column having Button in it . Clicking on button i want to open dialog box which is expected to display data related to that particular row in which button was clicked. But the problem is with binding , as i am unable to bind the control with viewmodel.
<Button Command="{Binding Path=ParentRow.DataContext,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
UpdateSourceTrigger=PropertyChanged, Mode=Default}"
lib:Event.Binding="Click.[**NameOfViewModelMethod**]" >
</Button>
First things first, if your Button is inside a Grid with a defined DataContext you don't need to set the Path like Path=ParentRow.DataContext.
Your Command binding should be like this:
<Button Command="{Binding YourVMICommand"} />
You have to define a public ICommand in your VM and then bind it to the button.
you don't show all of your code and context but it should work like this
i assume you are in a usercontrol caling parent datacontext ...
(exemple with listview):
<ListView ItemsSource="{Binding listFromDataContext, IsAsync=True}" Margin="3,51,0,10" >
<ListView.ItemTemplate >
<DataTemplate>
<grid>
<Button Command="{Binding DataContext.MyMethode, RelativeSource={RelativeSource AncestorType={x:Type controls:thisUserControl}}}" CommandParameter="{Binding}" />
</grid>
</DataTemplate>
</ListView.ItemTemplate >
</ListView>
then in model
private ICommand _MyMethode;
public ICommand MyMethode
{
get
{
return _MyMethode ?? (_MyMethode = new CommandHandler<MyModel.item>(x => showMessage(x), _canExecute));
}
}
public void showMessage(MyModel.item x)
{
MessageBox.Show(x.Info);
}
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>