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`
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 trying to move the datepicker loaded event in xaml.cs to view model. I don't want the code behind in xaml.cs.
xaml code given below,
<DatePicker BorderBrush="LightBlue" BorderThickness="1"
Name="dtTierValidTo" IsEnabled="{Binding CanEdit}"
DisplayDateStart="{Binding TierDealValidFromDate, Mode=OneWay}"
SelectedDate="{x:Static sys:DateTime.Now}"
DisplayDate="{x:Static sys:DateTime.Now}" Width="120" HorizontalAlignment="Left">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded" >
<cmd:EventToCommand Command="{Binding DtTierValidTo_Loaded}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DatePicker>
ViewModel.cs code is below..
public RelayCommand DtTierValidTo_Loaded { get; protected set; }
DtTierValidTo_Loaded = new RelayCommand(dtTierValidTo_Loaded);
private void dtTierValidTo_Loaded()
{
DatePicker dtTierValidTo = new DatePicker();
//DatePicker dtTierValidTo = sender as DatePicker;
dtTierValidTo.BlackoutDates.AddDatesInPast();
}
But the the problem is, event is not getting triggered.
Could I get some help here
Try this approach
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding DtTierValidTo_Loaded}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
hi i have one list box that every row contain one textbox and one button;
with click button that row delete from listbox; this work with mvvm pattern
i use command for this.
this is my xaml:
<DataTemplate x:Key="CategoryTemplate">
<Border Width="400" Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4">
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock Width="300" Margin="5" Text="{Binding Path=Name}"></TextBlock>
<Button Name="btnDeleteCategory" Width="50" Margin="5"
Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
CommandParameter="{Binding}" Content="-" />
</StackPanel>
</Border>
</DataTemplate>
<ListBox Grid.Column="0" Grid.Row="0" Name="lstCategory"
ItemTemplate="{StaticResource CategoryTemplate}"
ItemsSource="{Binding Path=GetAllCategories}">
</ListBox>
and in viewmodel class i have this command :
private ObjectButtonCommand<Category> _deleteCommand;
public ObjectButtonCommand<Category> DeleteCommand
{
get
{
return _deleteCommand
?? (_deleteCommand = new ObjectButtonCommand<Category>(
_category =>
{
GetAllCategories.Remove(_category);
}));
}
}
that GetAllCategories is observecollection propert;
and this is my ObjectButtonCommand code:
public class ObjectButtonCommand<T> : ICommand
where T:class
{
private Action<T> WhatToExecute;
public ObjectButtonCommand(Action<T> What)
{
WhatToExecute = What;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
WhatToExecute((T)parameter);
}
}
now every thing is ok and when click button that row delete;
now i want that this process repeat when i select one row of listbox
i try this code :
<ListBox Grid.Column="0" Grid.Row="0" Name="lstCategory" ItemTemplate="{StaticResource CategoryTemplate}" ItemsSource="{Binding Path=GetAllCategories}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DataContext.DeleteCommand , RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding Path=SelectedItems,ElementName=lstCategory}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
but i get this error at this code : WhatToExecute((T)parameter);
{"Unable to cast object of type 'System.Windows.Controls.SelectedItemCollection' to type 'Sepand.WPFProject.Model.Model.Category'."}
what should i do?
You pass the selection to the delete command which is a list, hence you cannot use the same command for both cases unless you would wrap the individual items (as passed from the DataTemplate) in a list first.
You should probably define a new command which takes an IList as parameter (type of ListBox.SelectedItems), whose items you then cast to Category and remove individually.
If you just want to delete a single selected item you need to change the binding to SelectedItem and need be able to handle the case of SelectedItem being null in your existing command. e.g. change CanExecute to parameter != null if that is respected by InvokeCommandAction.
Hi Mohammad can you try this:
For your Listbox, make sure your SelectionMode is set to Single. Then change that you refer to SelectedItem in the CommandParameter instead of SelectedItems. Like so:
<ListBox Grid.Column="0" Grid.Row="0" Name="lstCategory" ItemTemplate="{StaticResource CategoryTemplate}" ItemsSource="{Binding Path=GetAllCategories}" SelectionMode="Single">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DataContext.DeleteCommand , RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding Path=SelectedItem,ElementName=lstCategory}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</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}"