Listview ContextMenu Unclickable - c#

So I have a contextmenu in my xaml code which is
<ListView x:Name="listView" Grid.Row="1" SelectionChanged="listView_SelectionChanged" IsEnabled="True">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Command="Delete" IsEnabled="True" Click="MenuItem_Click"></MenuItem>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
and then in my c# code I have
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
listView.Items.Remove(listView.SelectedItems[0]);
showList.RemoveAt(listView.Items.IndexOf(listView.SelectedItems[0]));
}
but unfortunatelly when I debug my program It won't let me click contextmenu delete button :( How can I solve this?

Related

How to bind command click of MenuContextItem from one User Control to Other in WPF

In my code I have following User Controls
MyListUserControl
MyListItemUserControl
MyListUserControl loads list with items loaded from MyListItemUserControl,
on MyListItemUserControl I have a button on click of that I'm showing ContextMenu with multiple options.
Now on click of that MenuItem I have to call method from MyListUserControl, is it possible to do that?
In short following is the structure that I have
MyListUserControl -> MyListItemUserControl -> ContextMenu Item Click -> Command from MyListUserControl
If I write ListItem code in same ListUserControl its working fine. But I would prefer the item and list code in separate classes.
MyListUserControl xaml Code
<ListView ItemsSource="{Binding Data}">
<ListView.ItemTemplate>
<DataTemplate>
<local1:MyListItemUserControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
CommentData="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I have DelegateCommand Defined in this class, which I would like to call from MyListItemUserControl
MyListItemUserControl xaml
<ContextMenu>
<MenuItem Command="{Binding Source={StaticResource Proxy}, Path=Data.MenuItem_Clicked}"
Header="{x:Static propertyRes:Resources.Txt_Copy}"
Style="{StaticResource menuItems}" />
<MenuItem Header="{x:Static propertyRes:Resources.Txt_Edit}" Style="{StaticResource menuItems}" />
<MenuItem Header="{x:Static propertyRes:Resources.Txt_Delete}" Style="{StaticResource menuItems}" />
</ContextMenu>
this is pseudo code, but should be enough for you to get the idea
<MyListItemUserControl>
<MyListItemUserControl.ItemTemplate>
<Grid>
<Grid.ContextMenu>
<ContextMenu>
<ContextMenuItem
Header="Do stuff"
Command="{Binding ElementName=DetailsView, Path=DataContext.DoStuffCommand"
CommandParameter="{Binding}"/>
</ContextMenu>
</Grid.ContextMenu>
<!-- your content -->
</Grid>
</<MyListItemUserControl.ItemTemplate>
</MyListItemUserControl>
<MyListItemUserControl x:Name="DetailsView">
<!-- I assume that data context is MyListItemViewModel or something similar
it has the command: DataContext.DoStuff that takes items from list as parameter -->
</MyListItemUserControl>
EDIT:
If your controls are defined in seperate files you just need to pass the commnad. I usually do this by adding a dependency property in code behind that holds the command:
public static readonly DependencyProperty DoStuffCommandProperty = DependencyProperty.Register(
"DoStuffCommand", typeof(ICommand), typeof(MainWindow), new PropertyMetadata(default(ICommand)));
public ICommand DoStuffCommand
{
get { return (ICommand) GetValue(DoStuffCommandProperty); }
set { SetValue(DoStuffCommandProperty, value); }
}
so you can do:
<ListView ItemsSource="{Binding Data}">
<ListView.ItemTemplate>
<DataTemplate>
<local1:MyListItemUserControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
CommentData="{Binding}" DoStuffCommand="{Binding DoStuffCommandInSomeViewModel}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

Popup not working on MenuItem

I have to display a Popup on click of a MenuItem. I have written the below code for that:
<ContextMenu>
<Border>
<MenuItem Name="ack" Header="ACK" HorizontalAlignment="Center" Command="{Binding AcknowledgeCommand}" Visibility="{Binding IsAcked, Converter={StaticResource showOnFalse}}" Click="MenuItem_Click_1"/>
</Border>
<Separator/>
<Border>
<MenuItem Header="Info" HorizontalAlignment="Center" Click="MenuItem_Click"/>
</Border>
<Separator/>
<Border>
<MenuItem Header="Goto" HorizontalAlignment="Center"/>
</Border>
<Popup Name="infoPopup" Placement="Mouse" HorizontalOffset="-100" VerticalOffset="-100" AllowsTransparency="True" StaysOpen="True">
<Alarms:UserControl_MYUC DataContext="{Binding TopMost}" Background="{DynamicResource DetailPanelCompartmentTitleBg}" BorderBrush="{DynamicResource SecWindowBtnBorder}"/>
</Popup>
</ContextMenu>
OnClick of the MenuItem, I have written the following code:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
infoPopup.Visibility = System.Windows.Visibility.Visible;
infoPopup.IsOpen = true;
}
I am able to see the popup on click of the Info button, but the Popup goes within a second. I want it to be visible till the user clicks some other area or the popup loses focus.
Please suggest what to do.
Apply StaysOpenOnClick="True" to your MenuItem.
Please use the StaysOpen Boolean to define what you wanted.
myPopup.StaysOpen = true;
For further information, please refer to the following link : https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.staysopen(v=vs.110).aspx

WPF ContextMenu + MenuItem memory leak

Can anybody help with WPF and how-to free ContextMenu + MenuItem. As I’m using it, it keeps hold a WeakReference.
I have created a demo application, where it adds 10000 UserControl to a StackPanel and then removes it again. If the UserControl ContextMenu has no MenuItem, everything is fine. But if it contains a
<UserControl.ContextMenu>
<ContextMenu>
<MenuItem Header="Properties" />
</ContextMenu>
</UserControl.ContextMenu>
Then Teleriks profiler reports 180.050 instances of WeakReference. How can I free this up?
I have googled without luck - I have read the following pages, but couldn't see any solution, else I have missed some of the informations.
Strange wpf memory leak
WPF Combobox "leaks" memory
https://social.msdn.microsoft.com/Forums/vstudio/en-US/aa987282-6c43-4652-b712-2397d4044ff5/memoryleak-with-contextmenu-and-menuitem?forum=wpf
Demo application:
MainWindow.xaml has added:
<StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="Add Empties" Click="AddClick" Margin="3" />
<Button Content="Add With ContextMenu" Click="AddWithContextMenuClick" Margin="3" />
<Button Content="Remove" Click="RemoveClick" Margin="3" />
</StackPanel>
<StackPanel x:Name="PagesStackPanel">
</StackPanel>
</StackPanel>
MainWindow.xaml.cs has added:
private void AddClick(object sender, RoutedEventArgs e)
{
for (var i = 0; i < 10000; i++)
{
PagesStackPanel.Children.Add(new ChildViewEmpty());
}
}
private void RemoveClick(object sender, RoutedEventArgs e)
{
PagesStackPanel.Children.Clear();
}
private void AddWithContextMenuClick(object sender, RoutedEventArgs e)
{
for (var i = 0; i < 10000; i++)
{
PagesStackPanel.Children.Add(new ChildViewWithMenu());
}
}
ChildViewEmpty.xaml has added:
<Grid />
ChildViewWithMenu.xaml has added:
<UserControl.ContextMenu>
<ContextMenu>
<MenuItem Header="Properties" />
</ContextMenu>
</UserControl.ContextMenu>
Telerik profiler dump, where we can see the memory I can't free with GC, when ContextMenu has a MenuItem.
Telerik profiler dump, where ContextMenu has no MenuItem.
I’m running this on Windows 8.1 with .NET 4.5

Access button in radgridview during RowLoaded

I have Simple radgridview.
In Last column I have radbutton
I want to access this radbutton during grid loaded
<telerik:RadGridView x:Name="gvViewer" ItemsSource="{Binding Items}">
<telerik:GridViewDataColumn UniqueName="StatusName" Width="200">
<telerik:GridViewDataColumn.Header>
<TextBlock Text="StatusName" />
</telerik:GridViewDataColumn.Header>
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<telerik:RadButton Name="btnSend" Content="Send" />
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView>
I have this event rowLoaded
void gvViewer_RowLoaded(object sender, RowLoadedEventArgs e)
{
var button= row.ChildrenOfType<RadButton>();
}
row.ChildrenOfType() returns null to "var button"
How do i access this button?
Please try checking this way Row.FindControl("btnSend"); Or
e.Row.FindControl("btnSend"); and cast as RadButton for using it

ContextMenuOpening event not trigered

I have a button with a context menu, but I just can get the ContextMenuOpening event to triger.
<Button Name="a_button"
ContextMenu="{StaticResource MyContextMenu}"
ContextMenuOpening="MyContextMenu_Opening" >
</Button>
private void MyContextMenu_Opening(object sender, ContextMenuEventArgs e)
{
// doesnt get here.
}
Any ideas how to make this work ?
Thanks.
ContextMenuOpening event must be handled on an ancestor of the ContextMenu not on the ContextMenu itself. If you try handling it on the ContextMenu the event only fires when you right click once ContextMenu is already open.
I've just checked your code and it works perfectly.
Here is my full xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<ContextMenu x:Key="MyContextMenu">
<MenuItem Header="Send" />
</ContextMenu>
</Window.Resources>
<Grid>
<Button Name="a_button"
ContextMenu="{StaticResource MyContextMenu}"
ContextMenuOpening="MyContextMenu_Opening" >
</Button>
</Grid>
</Window>
And *.cs:
private void MyContextMenu_Opening(object sender, ContextMenuEventArgs e)
{
}

Categories