|
|
he application that I'm creating has a menu comprised of labels/headers. When you click on a menu item (a xaml Label) a drop down appears with subheaders related to that item. However, I need to make this menu customizable via XML. The idea is this, the XML outlines the header names as well as the subheader names for each menu item.
<MenuHeaders>
<Header Value="Something">
<NestedHeader1>NestedHeader1</NestedHeader1>
<NestedHeader2>NestedHeader2</NestedHeader2>
</Header>
<Header Value="Something Else">
<NestedHeader1>NestedHeader1</NestedHeader1>
<NestedHeader2>NestedHeader2</NestedHeader2>
</Header>
</MenuHeaders>
I am able to read the XML using XDocument and bind a Dependency Property to the label context. The problem is that I do not know how many menu items might be in the XML file. I wanted to avoid creating a ton of labels and dependency properties for the label content to bind to. Instead of creating labels in xaml and binding to a property in C#, can I create UI items in C# and populate them with elements from a list at run time? There's some code below of how I'm getting and binding the data to a preexisting label.
C#:
<pre>
public string HeaderName
{
get => (string)GetValue(HeaderNameProperty);
set => SetValue(HeaderNameProperty, value);
}
public static readonly DependencyProperty HeaderNameProperty =
DependencyProperty.Register("HeaderName", typeof(string), typeof(MainWindow));
private void LoadHeaders()
{
var msg = new List<string>(0);
XDocument doc = null;
try
{
doc = XDocument.Load("MenuHeaders.xml");
}
catch (Exception e)
{
msg.Add(string.Format("Unable to open file:{0}", ""));
msg.Add(e.Message);
}
if(doc != null)
{
var allHeaders = doc.Descendants("Header").ToList();
List<string> headers = new List<string>();
foreach(XNode node in allHeaders)
{
XElement element = node as XElement;
foreach(XAttribute attribute in element.Attributes())
{
headers.Add(attribute.Value);
}
}
HeaderName = headers[0];
}
}
XAML:
<stackpanel x:name="Stack">
(As you may have noticed, I'm only adding the Header attributes to the List and none of the subheaders. This code is not complete but it's what I currently have) I will appreciate any help.
|
|
|
|
|
You need to put a secondary loop into the Header loop to get the children of the header.
How do you intend to bind the actions to the menu item?
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
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}" />
<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
|
|
|
|
|
I am creating an application using c# winform to create and mount ISO files like wincdemu. I am able to create the ISO file and mount it like a local drive. I want to mount the image with disc type CD-ROM/DVD-ROM. I am using AttachVirtualDisk() to mount the image.
Is there any API/library support to mount the image with disc type CD-ROM/CD-R/CD-RW/DVD-ROM/DVD-RAM etc. ?
I appreciate any advice.
|
|
|
|
|
|
How can I reverse strings in other processes in c#? Can this be done with SendMessage or WriteProcessMemory?
|
|
|
|
|
It would have to be WriteProcessMemory. You're going to need admin permissions to do that.
This is akin to writing a debugger. It's not so straight foreword to just read a string, what you THINK is a string anyway, and just write one back. There are a multitude of considerations involved, like PageProtection and race conditions.
No, I'm not going to tell you how to do this because it can be used for malicious purposes.
|
|
|
|
|
Hi guys trying to run a program in c# which needs to return the number of times the char occurs in string. the main is fine but the method name i created gives me an error which i don't know why.(in bold) your help will be appriciated here is my program:
static void Main(string[] args)
{
string n = "Banana";
int num=CountOccurrences(n,'a');
Console.WriteLine(num);
}
static int CountOccurrences(string text, char digit)
{
for (int i=0 ; i < text.Length; i++)
{
if (text[i] == digit)
{
i++;
}
return i;
}
|
|
|
|
|
Your CountOccurrences method has its return statement in the wrong place. Also you are incrementing the loop count in two places so your result will most likely be wrong. It should be:
static int CountOccurrences(string text, char digit)
int count = 0;
for (int i=0 ; i < text.Length; i++)
{
if (text[i] == digit)
{
count++;
}
}
return count;
}
|
|
|
|
|
We don't know what error you are getting, but I suspect it's not the method name that is causing the problem.
Check the error itself - it should give you a good clue as to what is going wrong.
I suspect that the error message is likely to be "not all code paths return a value" - and that means that the compiler can see a route through your code which doesn't execute a return value statement.
Try moving the return i; line outside your for loop, so it's the last instruction in the method!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You prefer a scope error?
|
|
|
|
|
That's his next problem!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
And the fact that he's using the same variable for two different counts. But see my answer ...
|
|
|
|
|
Which starts him off using the debugger to find out what's going on ... with any luck!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
What? I'm awake? Oh damn ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Small sidetrack: You all refer to Joanna as he/him. Is Joanna also used a the name of a male (it certainly is a female name, at least in some parts of the world), or did you just overlook the name of the original poster?
|
|
|
|
|
This is the internet, where men are men, women are men, and 12 year old girls are FBI agents ...
The internet convention is not to assume a gender: male pronouns are generally used unless gender preference is explicitly stated: the OP is from Israel, and I for one have no idea if "Joanna" is a male or female name there (I can't even order a beer in Hebrew, because I've never been there).
Even in English, assuming gender from a name is fraught with difficulties: "Leslie" is generally given to boys, and "Lesley" to baby girls - but sometimes the parents spell the other way round (my wife's father was "Lesley" for example). And some of the "modern names" they give kids these days ... no chance!
And then there is the "I identify sexually as ..." fun and games which throws the whole load in the bin and starts you all over again just for the fun of it. Stick to "he", "him", "his" unless otherwise stated.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Yeah, if you don't know (or are not sure). But I find it difficult to refer to a "Mary" or "Rosalyn" as a "he" in a reply - in situations where I know for sure (at least 99.9%) that the reply goes to a girl. When a poster identifies herself as "Mary", I consider the gender explicitly stated.
The reason why I asked is that "Joanna" as I know it is just as feminine as Mary or Rosalyn. So my question was not primarily about Internet gender conventions, but about this specific name in particular: Is the name Joanna given to boys - in Israel (like the Joanna starting this thread), or in other cultures?
I have argued myself for accepting role designations that for historical reasons are gender specific, as being gender neutral today. E.g. I have no problems accepting male midwives or cleaning ladies. But those are roles, not specific individuals. When I refer to individual persons where the gender is known, I find it strange to refer to the opposite gender. If I were to refer to one specific male midwife, I would never refer to him as "she".
Finish language has a traditional neutral gender reference merging "him" and "her". It is slowly being accepted in Swedish, and I have seen it in Norwegian as well: "hen" as as common term for "han" and "hun". I applaud that development.
Yet, I think it is OK that people call me a "coffee hag" (kaffekjerring) - a dialect expression for a person addicted to coffee. At one national congress, people from the south of Norway refused to believe that we have male hags in mid Norway. I had to get support from others from my district to confirm it. (This was in a discussion whether we should replace the gender specific term "formann" with the gender neutral "leder", where I argued that this is a role description, not a person description.)
|
|
|
|
|
Member 7989122 wrote: "hen" as as common term for "han" and "hun". Try calling a girl "hen" in Glasgow. 
|
|
|
|
|
Ah, an incoming Gorbals Kiss!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
... or a kick in the Gorbals...
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Seen it done once or twice. 
|
|
|
|
|
Richard MacCutchan wrote: Try calling a girl "hen" in Glasgow.
Eh? They wouldn't blink an eye, it's part of the common vernacular.
|
|
|
|