logo
down
shadow

Adding click event to generated MenuItems with datatemplate in WPF


Adding click event to generated MenuItems with datatemplate in WPF

By : user2949103
Date : November 16 2020, 06:23 AM
hope this fix your issue You're only handling the click on the "header" MenuItem here. Put the click handler into a Style inside the MenuItem's ItemContainerStyle.
code :
<MenuItem>
    <MenuItem.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <EventSetter Event="Click" Handler="PolygonShapesMenu_OnClick"/>
        </Style>
    </MenuItem.Resources>
</MenuItem>


Share : facebook icon twitter icon
Adding a style to HierarchicalDataTemplate generated MenuItems

Adding a style to HierarchicalDataTemplate generated MenuItems


By : John Buell
Date : March 29 2020, 07:55 AM
it should still fix some issue I'm adding my hierarchical data to a Menu-Control using the HierarchicalDataTemplate. , Like this:
code :
<Menu>
    <Menu.ItemContainerStyle>
        <Style TargetType="MenuItem">
           ....
        </Style>
    </Menu.ItemContainerStyle>
</Menu>
<Menu>  
    <MenuItem Header="Text" ItemsSource="{Binding Data}" ItemContainerStyle="{SomeStyle}"/>  
</Menu>  
adding button to datatemplate and calling its click event

adding button to datatemplate and calling its click event


By : Ridvan BEAU
Date : March 29 2020, 07:55 AM
should help you out Nilesh,
you should be using binding the button to a command. For example if your data item is defined like this:
code :
public class MyItem : ViewModelBase
{
    public MyItem()
    {
        ClickMeCommand = new RelayCommand(ClickMe);
    }

    private void ClickMe()
    {
        Debug.WriteLine("I was clicked");
    }

    public string ISBN { get; set; }
    public string BookName { get; set; }
    public string PublisherName { get; set; }

    public ICommand ClickMeCommand { get; set; }
}
<DockPanel>
    <Button Content="Click Me" Command="{Binding ClickMeCommand}" />
</DockPanel>
public class MainViewModel : ViewModelBase
{
    public IEnumerable<MyItem> Items { get; private set; }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        Items = new List<MyItem>
                    {
                        new MyItem{ ISBN = "ISBN", BookName = "Book", PublisherName = "Publisher"}
                    };
        ClickMeCommand = new RelayCommand<MyItem>(ClickMe);
    }

    private void ClickMe(MyItem item)
    {
        Debug.WriteLine(string.Format("This book was clicked: {0}", item.BookName));
    }

    public ICommand ClickMeCommand { get; set; }
<DockPanel>
    <Button Content="Click Me" CommandParameter="{Binding}" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}, Path=DataContext.ClickMeCommand}" />
</DockPanel>
  <ListBox ItemTemplate="{StaticResource MyTemplate}" ItemsSource="{Binding Items}"/>
ContextMenu MenuItems adding continuously on Click event

ContextMenu MenuItems adding continuously on Click event


By : Bruce fxq
Date : March 29 2020, 07:55 AM
hop of those help? add a boolean data member which will check if the sub menu's were already added
code :
private void OnddBtnMobileClick(object sender, RoutedEventArgs e)
{
   if(alreadyAdded == true) 
      return;

   alreadyAdded = true;
   mnItem = new MenuItem();
   mnItem.Header ="B1"; 
   cMenu.Items.Add(mnItem);
   mnItem = new MenuItem();
   mnItem.Header ="A1";
   cMenu.Items.Add(mnItem);
   mnItem = new MenuItem();
   mnItem.Header="B 2";
   cMenu.Items.Add(mnItem);
   cMenu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
}
Appended anchor tag click event does not adding active class to it. (appended tag is generated by click event of another

Appended anchor tag click event does not adding active class to it. (appended tag is generated by click event of another


By : Josh Ettwein
Date : March 29 2020, 07:55 AM
like below fixes the issue At the moment you bind the click event to $('.newLink'), there is no link with that class in the page yet. Use this to bind on any current or future elements matching the selector: $(document).on('click', '.newLink', yourcallbackfunction);
c# wpf Click event for header of MenuItems

c# wpf Click event for header of MenuItems


By : srikanth
Date : March 29 2020, 07:55 AM
Any of those help You could handle the PreviewMouseLeftButtonDown event instead of Click:
code :
item.PreviewMouseLeftButtonDown += (s, e) => ShowMainWindow();
Related Posts Related Posts :
  • Can't set a date time in one of my entity framework objects, says NullReferenceException
  • GETJob() Win32 Print spooler api 64bit The parameter is incorrect, how to fix?
  • Switch Case Causing Trouble In Going To A particular Function
  • Switch Case Calling A Function Based On The User's Choice
  • Accessing WebBrowser on different Threads
  • CPU Usage Avoid 100% With Environment.ProcessorCount?
  • Select distinct categories from database
  • C# app getting Watson dialog when calling TerminateProcess
  • The underlying provider failed on Open in entity framework connection
  • Compile a library without checking references
  • Can we create and access a registry key in HKEY_LOCAL_MACHINE without running application with admin permissions in C#
  • Control light intensity with a UI slider
  • Threading (Does these threads exits or aborts itself?)
  • Change the function of a button if Logged in
  • how to accept value in date format in textbox in C# windows form?
  • remove the lines from RichtextBox?
  • How to call String.Split that takes string as separator?
  • Specify task timeout in parallel linq to objects
  • comparing two custom objects to prevent duplicates
  • How to Print Text from ComboBox into a MessageBox
  • How can I do a Func<object[],Expression<Func<T,bool>>> dynamic?
  • C# SqlDataReader No data exists for the row/column
  • My code is not giving the desired output
  • Is it possible to have multiple MVC routes point to the same controller/view?
  • Updating Listbox results in " Invalid cross-thread access."
  • Finding Elbow Angle with Kinect
  • Host a mvc web api application in the sub-folder of a website
  • How to generate a sequential unique id in a thread-safe way
  • How to add a separator to a WPF combobox that is databound?
  • optimize linq query with related entities
  • Optimize this code for large input
  • Is there any way to clear all list box short way?
  • ACR122 Device Programming sample does not find reader
  • Parallel Library: does a delay on one degree of parallelism delay all of them?
  • How to convert a var which contains Long to an Long[]
  • Quickly prune and create valid data combinations
  • Get 3 parameters out of a Dictionary<string, Dictionary<string Action>>
  • Regex IsMatch taking too long to execute
  • Shuffling divs using C# behindcode
  • ASP.NET Project and IE10 Compatibility Mode issues in Windows 7
  • Why can't I loop through this dictionary?
  • RavenDB Stream for Unbounded Results - Connection Resilience
  • How to remove the focus border of a CheckBox in C# Visual Studio?
  • Exception while using String.Format "Index (zero based) must be greater than or equal to zero and less than the siz
  • Caching an LDAP connection
  • C# How to detect kill event sender
  • How to Display Splashscreen and login form at same time using c#
  • Read .sql script with parameters
  • Why would I need to compile/generate methods/code at runtime?
  • Unable to create a constant value of type 'Project.Models.Subcategory'. Only primitive types or enumeration types are su
  • How to run possibly synchronous or possibly asynchronous code?
  • Drop SQLite tables using list of tables names
  • Can I organize Git submodules in a flat hierarchy?
  • How to make a server reply after receiving a request/data
  • Stored Procedure works in DB but not in C# code
  • MVVM WPF Programmatically add Child Elements
  • NCrunch and PostSharp Logging clash
  • Increasing memory usage in socket client
  • Multiple DropDownList but same action
  • How to get application memory usage as shown in Task Manager?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org