Click here to Skip to main content
15,215,192 members
   

C#

 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
Richard MacCutchan28-May-20 3:43
mveRichard MacCutchan28-May-20 3:43 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
Richard Deeming28-May-20 3:53
mveRichard Deeming28-May-20 3:53 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
OriginalGriff28-May-20 3:57
mveOriginalGriff28-May-20 3:57 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
Ertuğrul ÇİÇEK28-May-20 6:32
MemberErtuğrul ÇİÇEK28-May-20 6:32 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
Richard Deeming28-May-20 8:32
mveRichard Deeming28-May-20 8:32 
QuestionCreating and Binding UI objects in C# instead of XAML (WPF) Pin
Member 1484454026-May-20 12:04
MemberMember 1484454026-May-20 12:04 
AnswerRe: Creating and Binding UI objects in C# instead of XAML (WPF) Pin
Mycroft Holmes26-May-20 12:26
professionalMycroft Holmes26-May-20 12:26 
AnswerRe: Creating and Binding UI objects in C# instead of XAML (WPF) Pin
Richard Deeming27-May-20 1:01
mveRichard Deeming27-May-20 1:01 
The XMLDataProvider might be a simpler option:
How to: Bind to XML Data Using an XMLDataProvider and XPath Queries - WPF | Microsoft Docs[^]

If you want to stick with code, you're going to need a class to represent the menu item. For example:
public class MenuHeaderItem
{
    public string HeaderName { get; set; }
    public IReadOnlyList<MenuHeaderItem> Children { get; set; }
}

public static IReadOnlyList<MenuHeaderItem> LoadMenu(XDocument document)
{
    var result = new List<MenuHeaderItem>();
    var allHeaders = document.Descendants("Header");
    foreach (XElement node in allHeaders)
    {
        result.Add(new MenuHeaderItem
        {
            HeaderName = (string)node.Attribute("Value"),
            Children = node.Elements().Select(el => new MenuHeaderItem 
            {
                HeaderName = (string)el,
            }).ToList(),
        });
    }
    
    return result;
}
Bind the loaded values to a read-only dependency property:
private static readonly DependencyPropertyKey MenuHeadersKey = DependencyProperty.RegisterReadOnly(
    "MenuHeaders",
    typeof(IReadOnlyList<MenuHeaderItem>),
    typeof(YourContainingClass),
    new PropertyMetadata(null)
);

public static readonly DependencyProperty MenuHeadersProperty = MenuHeadersKey.DependencyProperty;

public IReadOnlyList<MenuHeaderItem> MenuHeaders
{
    get { return (IReadOnlyList<MenuHeaderItem>)GetValue(MenuHeadersProperty); }
    private set { SetValue(MenuHeadersKey, value); }
}

private void LoadHeaders()
{
    if (File.Exists("MenuHeaders.xml"))
    {
        XDocument document = XDocument.Load("MenuHeaders.xml");
        MenuHeaders = LoadMenu(document);
    }
}
Then bind the MenuHeaders collection to your menu.
<Menu ItemsSource="{Binding Path=MenuHeaders}">
    <Menu.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Header" Value="{Binding Path=HeaderName}" />
            <Setter Property="ItemsSource" Value="{Binding Path=Children}" />
            
            <!-- Handle the menu click event: -->
            <Setter Property="Command" Value="{Binding Path=MenuClickCommand}" />
            <Setter Property="CommandParameter" Value="{Binding Path=HeaderName}" />
        </Style>
    </Menu.ItemContainerStyle>
</Menu>
(I've assumed that you have an ICommand instance on your view-model called MenuClickCommand which will receive the header name as its parameter and handle the menu click event.)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

QuestionHow to mount an ISO file with disc type - CD/DVD Pin
Member 1001484126-May-20 6:43
MemberMember 1001484126-May-20 6:43 
AnswerRe: How to mount an ISO file with disc type - CD/DVD Pin
Richard MacCutchan26-May-20 6:48
mveRichard MacCutchan26-May-20 6:48 
QuestionHow to reverse strings in other processes c# Pin
mcneb1025-May-20 11:06
Membermcneb1025-May-20 11:06 
AnswerRe: How to reverse strings in other processes c# Pin
Dave Kreskowiak25-May-20 18:30
mveDave Kreskowiak25-May-20 18:30 
Questionquestion about a method program Pin
Joanna Moses23-May-20 1:34
MemberJoanna Moses23-May-20 1:34 
AnswerRe: question about a method program Pin
Richard MacCutchan23-May-20 2:00
mveRichard MacCutchan23-May-20 2:00 
AnswerRe: question about a method program Pin
OriginalGriff23-May-20 2:01
mveOriginalGriff23-May-20 2:01 
GeneralRe: question about a method program Pin
Luc Pattyn23-May-20 5:00
sitebuilderLuc Pattyn23-May-20 5:00 
GeneralRe: question about a method program Pin
OriginalGriff23-May-20 5:26
mveOriginalGriff23-May-20 5:26 
GeneralRe: question about a method program Pin
Richard MacCutchan23-May-20 5:52
mveRichard MacCutchan23-May-20 5:52 
GeneralRe: question about a method program Pin
OriginalGriff23-May-20 5:54
mveOriginalGriff23-May-20 5:54 
GeneralRe: question about a method program Pin
Richard MacCutchan23-May-20 6:08
mveRichard MacCutchan23-May-20 6:08 
GeneralRe: question about a method program Pin
OriginalGriff23-May-20 6:11
mveOriginalGriff23-May-20 6:11 
GeneralRe: question about a method program Pin
Member 798912223-May-20 6:50
MemberMember 798912223-May-20 6:50 
GeneralRe: question about a method program Pin
OriginalGriff23-May-20 9:14
mveOriginalGriff23-May-20 9:14 
GeneralRe: question about a method program Pin
Member 798912223-May-20 11:53
MemberMember 798912223-May-20 11:53 
GeneralRe: question about a method program Pin
Richard MacCutchan23-May-20 22:04
mveRichard MacCutchan23-May-20 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.