Click here to Skip to main content
15,850,214 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
Richard MacCutchan28-May-20 4:43
mveRichard MacCutchan28-May-20 4:43 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
Richard Deeming28-May-20 4:53
mveRichard Deeming28-May-20 4:53 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
OriginalGriff28-May-20 4:57
mveOriginalGriff28-May-20 4:57 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
Ertuğrul ÇİÇEK28-May-20 7:32
Ertuğrul ÇİÇEK28-May-20 7:32 
GeneralRe: C# How To Get Version Info Of exe in FTP Pin
Richard Deeming28-May-20 9:32
mveRichard Deeming28-May-20 9:32 
QuestionCreating and Binding UI objects in C# instead of XAML (WPF) Pin
Member 1484454026-May-20 13:04
Member 1484454026-May-20 13:04 
AnswerRe: Creating and Binding UI objects in C# instead of XAML (WPF) Pin
Mycroft Holmes26-May-20 13:26
professionalMycroft Holmes26-May-20 13:26 
AnswerRe: Creating and Binding UI objects in C# instead of XAML (WPF) Pin
Richard Deeming27-May-20 2:01
mveRichard Deeming27-May-20 2: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:
C#
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:
C#
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.
XAML
<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 7:43
Member 1001484126-May-20 7:43 
AnswerRe: How to mount an ISO file with disc type - CD/DVD Pin
Richard MacCutchan26-May-20 7:48
mveRichard MacCutchan26-May-20 7:48 
QuestionHow to reverse strings in other processes c# Pin
mcneb1025-May-20 12:06
mcneb1025-May-20 12:06 
AnswerRe: How to reverse strings in other processes c# Pin
Dave Kreskowiak25-May-20 19:30
mveDave Kreskowiak25-May-20 19:30 
Questionquestion about a method program Pin
Joanna Moses23-May-20 2:34
Joanna Moses23-May-20 2:34 
AnswerRe: question about a method program Pin
Richard MacCutchan23-May-20 3:00
mveRichard MacCutchan23-May-20 3:00 
AnswerRe: question about a method program Pin
OriginalGriff23-May-20 3:01
mveOriginalGriff23-May-20 3:01 
GeneralRe: question about a method program Pin
Luc Pattyn23-May-20 6:00
sitebuilderLuc Pattyn23-May-20 6:00 
GeneralRe: question about a method program Pin
OriginalGriff23-May-20 6:26
mveOriginalGriff23-May-20 6:26 
GeneralRe: question about a method program Pin
Richard MacCutchan23-May-20 6:52
mveRichard MacCutchan23-May-20 6:52 
GeneralRe: question about a method program Pin
OriginalGriff23-May-20 6:54
mveOriginalGriff23-May-20 6:54 
GeneralRe: question about a method program Pin
Richard MacCutchan23-May-20 7:08
mveRichard MacCutchan23-May-20 7:08 
GeneralRe: question about a method program Pin
OriginalGriff23-May-20 7:11
mveOriginalGriff23-May-20 7:11 
GeneralRe: question about a method program Pin
kalberts23-May-20 7:50
kalberts23-May-20 7:50 
GeneralRe: question about a method program Pin
OriginalGriff23-May-20 10:14
mveOriginalGriff23-May-20 10:14 
GeneralRe: question about a method program Pin
kalberts23-May-20 12:53
kalberts23-May-20 12:53 
GeneralRe: question about a method program Pin
Richard MacCutchan23-May-20 23:04
mveRichard MacCutchan23-May-20 23: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.