简体   繁体   中英

I have a dynamic menuItem but I can't get an object field from this menu

I asked a question a bit earlier about the same thing, but I have an other problem.

I need help about a dynamic menu I have. I want to get the "path" of objects in my menu.

在此处输入图片说明

Here is my wpf, you can see I binded Value="{Binding name}"/> because I want to display the names in my menu, not the path:

<Menu HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="525" IsMainMenu="True">
            <MenuItem Header="Menu" x:Name="myList" Click="myList_Click">
                <MenuItem.ItemContainerStyle>
                    <Style>

                        <Setter Property="MenuItem.Header" Value="{Binding name}"/>

                    </Style>
                </MenuItem.ItemContainerStyle>
            </MenuItem>
 </Menu>

I binded a collection with "this.myList.ItemsSource = list;", it's what displays my dynamic menu,so here's my mainwindow.cs:

 public MainWindow()
{

  var list = new List<History>
        {
                new History() { name = "Guy1", path = "C:/F1/}"},
                new History() { name = "Guy2", path = "C:/F2/"},
                new History() { name = "Guy3", path = "C:/F3/"},
                new History() { name = "Guy4", path = "C:/F4"},
        };

    InitializeComponent();

    this.myList.ItemsSource = list;
}

private void myList_Click(object sender, RoutedEventArgs e)
{
    DependencyObject obj = (DependencyObject)e.OriginalSource;
    while (obj != null && obj != myList)
    {

        if (obj.GetType() == typeof(MenuItem))
        {
            MessageBox.Show((e.OriginalSource as MenuItem).Header.ToString());
            break;
        }
        obj = VisualTreeHelper.GetParent(obj);
    }
}

And here is a simple class that I named "History":

namespace MediaPlayer
{
public class History
{
    // "prop" puis "tab"
    public String name { get; set; }
    public String path { get; set; }
    public int time { get; set; }

    public override string ToString()
    {
        return path;
    }
}
}

What I want to do is when I click on, for example, Guy1, I want my software to display a message that says "C:/F1/" because it's Guy1, for GUY2 I want "C:/F2/".

Do you have any idea? Do you know how can I acess the path of my object by using something like this? : MessageBox.Show((e.OriginalSource as MenuItem).Header.ToString());

Thanks.

EDIT :

<Menu HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="525" IsMainMenu="True">
                <MenuItem Header="Menu" x:Name="myList" Click="myList_Click">
                    <MenuItem.ItemContainerStyle>

                        <Style TargetType="{x:Type MenuItem}">
                            <EventSetter  Event="Click" Handler="MyItemClick"/>
                        </Style>
                    </MenuItem.ItemContainerStyle>
                </MenuItem>
            </Menu>

(easy solution)

You can use an EventSetter inside the Style for your MenuItem . Syntax as such:

<EventSetter Event="Click" Handler="MyItemClick" />

Inside the event handler code you can find out which item was clicked:

public void MyItemClick(object sender, EventArgs e)
{
    var clickedMenuItem = (MenuItem)sender;
    var clickedHistoryItem = (History)clickedMenuItem.DataContext;
    //do stuff with whatever "guy" was clicked
}

(more advanced option)

Note that using the MVVM Pattern , you can create a Command object inside the history item, and handle whatever you need to handle from there. The syntax would be:

<Setter Property="Command" Value="{Binding ClickCommand}" />

You can look into the MVVM pattern, but the learning curve is quite steep. It will definetely pay off once your program grows through.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM