簡體   English   中英

動態菜單項與XAML中的ItemsSource?

[英]Dynamic MenuItems with ItemsSource in xaml?

我在說英語,對不起。

我正在嘗試創建動態菜單項,以顯示基於集合(對象列表)的內容,這是我的xaml,您可以看到“ ListBox”和“ MenuItem”。

如我所說,我將標題綁定到對象的“名稱”:

MainWindow.xaml

<Grid Margin="0,0,-8,-9" Background="Black" >

    <DockPanel x:Name="LayoutRoot">
        <Grid VerticalAlignment="Top">
            <MediaElement x:Name="mediaElement" HorizontalAlignment="Center"  Margin="0,0,60,30" VerticalAlignment="Top" MouseLeftButtonUp="mediaElement_MouseLeftButtonUp">

            </MediaElement>
            <Menu HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="525" IsMainMenu="True">
                <MenuItem Header="Menu">
                    <ListBox x:Name="myList" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="461,-261,-1,106" Background="#FFFFE800" MouseDoubleClick="ListBox_Double_Click" Width="57">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>

                                    <MenuItem Header="{Binding name}"> </MenuItem>

                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </MenuItem>
            </Menu>
        </Grid>

這是我在C#中的代碼, 您可以看到我將“ myList”(這是我的ListBox)與list (包含要在菜單中顯示的內容)的ItemsGetSource綁定

public MainWindow()
{
    var list = new List<History>
    {
        new History() { name = "Guy1"},
        new History() { name = "Guy2"},
        new History() { name = "Guy3"},
        new History() { name = "Guy4"},
    };

    InitializeComponent();

    this.myList.ItemsSource = list;
}

我的課程“歷史記錄”(我要顯示的是“名稱”字段)

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 name;
        }
    }
}

我想我不能將itemSource用於我的menuItems,也許嗎?

這是僅使用MenuItems的示例:

MainWindow.xaml

<Grid Margin="0,0,-8,-9" Background="Black" >
    <DockPanel x:Name="LayoutRoot">
        <Grid VerticalAlignment="Top">
            <MediaElement x:Name="mediaElement" HorizontalAlignment="Center"  Margin="0,0,60,30" VerticalAlignment="Top" MouseLeftButtonUp="mediaElement_MouseLeftButtonUp">

            </MediaElement>
            <Menu HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="525" IsMainMenu="True">
                <MenuItem Header="Menu" x:Name="myList">
                    <MenuItem.ItemContainerStyle>
                        <Style>
                            <Setter Property="MenuItem.Header" Value="{Binding name}"/>
                        </Style>
                    </MenuItem.ItemContainerStyle>
                </MenuItem>
            </Menu>
        </Grid>
    </DockPanel>
</Grid>

結果看起來像這樣:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM