You should start to read about XmlDataProvider and how to use bindings to XML-documents with XPath. A good starting point can be How to: Bind to XML Data Using an XMLDataProvider and XPath Queries | Microsoft Docs[^] In the example a ListBox is populated but you should be able to replace it with a Menu bound to an XmlDataProvider as ItemsSource with a customized ItemContainerStyle that sets the Header of the MenuItem to the title attribute. Good luck.
Henrik Jonsson
Posts
-
Generate a WPF-Menu based on a XML-document -
Reference Material for creating/documenting Software architectureAs already pointed out learning UML is a good starting Point for a object-oriented language like Java. I can recommend the book "Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development" by Craig Larman [^] to learn about how to actually apply modelling in real scenarios.
-
ListBox Style ProblemHello, the problem is that the default control template for ListBoxItem contains a border element which controls the background color. To fully control the appearance you could replace the template using the template from ListBoxItem ControlTemplate Example[^] with your own definition. See below for an example.
<Style x:Key="listBoxItemContainerStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="Orange"/>
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Gray"/>
<Setter TargetName="Border" Property="BorderThickness" Value="2"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="Orange"/>
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> -
code dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() doesnt workHi, when you get "Object reference not set to an instance of an object" exceptions some of the properties you invoke have returned
null
, in this case most likelyValue
for an empty grid cell. Check the Value for null before using it:var gridValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value;
if( gridValue != null )
{
string gridStringValue = gridValue.ToString();
...
} -
The Web can make you lose your religion, study saysThe Internet certainly helps us to widen our view of the world, so the religions may have to adapt, but to what point? During the service yesterday the priest started her preachment by refeering to Goooooogle when explaining "reconciliation". Is the search enginge our new god, having all the answers and knowing more about us than ourselves? Maybe we have to reconcile us with that fact too?
-
Shortest Way to Shuffle ArrayHi again, using the Fisher-Yates shuffling algorithm [^] seems to be superior in performance:
public static void Shuffle(T[] array)
{
Random rnd = new Random();
for (int i = array.Length - 1; i >= 0; i--)
{
int index = rnd.Next(i);
T temp = array[index];
array[index] = array[i];
array[i] = temp;
}
} -
Shortest Way to Shuffle ArrayThis generic function provides a quick way of randomly shuffling (reordering) the items in-place in an existing array:
public static void ShuffleArray(T[] array)
{
Random rnd = new Random();
int[] order = new int[array.Length];
for (int i = 0; i < array.Length; i++)
{
order[i] = rnd.Next();
}
Array.Sort(order, array);
}It can we rewritten as
Random rnd = new Random();
Array.Sort(array.Select(r => rnd.Next()).ToArray(), array);giving just slightly worse performance. According to my tests the use of Array.Sort is about three times faster than the OrderBy solution for a million integer array.