I have a combobox and am trying to do a simple switch statement. I keep getting an error.
If I switch the content of the combo too
It works fine. But I do not want to manually input 1000 items. I really want to use the XML. I have tried tons of different options and either I get the error or the selected Items value is "armor" every time. Any ideas?
I am pretty sure the issue is
but I do not know how to write it to read the XML.
Combobox XAML
Switch
XML SAMPLE
C#:
System.InvalidCastException: 'Unable to cast object of type 'System.Xml.XmlElement' to type 'System.Windows.Controls.ComboBoxItem'.'
If I switch the content of the combo too
C#:
</ComboboxItem Content = "">
I am pretty sure the issue is
C#:
ComboBoxItem selectedItem = (ComboBoxItem)armorselect.SelectedItem;
Combobox XAML
C#:
<ComboBox x:Name="armorselect" HorizontalAlignment="Left" VerticalAlignment="Top" Width="142" FontFamily="/Player Workbench;component/Fonts/#UnZialish" Background="#FF212121" Foreground="Black" DisplayMemberPath="Type" Margin="118,400,0,0" SelectionChanged="armorselect_SelectionChanged">
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FFADA5A5" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF212121" />
</ComboBox.Resources>
<ComboBox.ItemsSource>
<Binding Mode="OneTime" Source="{StaticResource Armor}" XPath="Armor"/>
</ComboBox.ItemsSource>
</ComboBox>
Switch
C#:
private void armorselect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
armorimage.Visibility = Visibility.Visible;
int ac = (Convert.ToInt32(tac.Text));
ComboBoxItem selectedItem = (ComboBoxItem)armorselect.SelectedItem;
switch (selectedItem.Content.ToString())
{
case "Padded":
armorimage.Source = new BitmapImage(new Uri("/Images/i_padded.png", UriKind.Relative));
ac = 11;
break;
case "Leather":
armorimage.Source = new BitmapImage(new Uri("/Images/i_leather.png", UriKind.Relative));
ac = 11;
break;
case "Studded Leather":
armorimage.Source = new BitmapImage(new Uri("/Images/i_studded_leather.png", UriKind.Relative));
ac = 12;
break;
case "Hide":
armorimage.Source = new BitmapImage(new Uri("/Images/i_hide.png", UriKind.Relative));
ac = 12;
break;
XML SAMPLE
C#:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ArmorInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Armor>
<Type>Padded</Type>
<Cost>5gp</Cost>
<AC>11 + Dex modifier</AC>
<Strength>N/A</Strength>
<Stealth>Disadvantage</Stealth>
<Weight>8lbs</Weight>
</Armor>
<Armor>
<Type>Leather </Type>
<Cost>10gp</Cost>
<AC>11 + Dex modifier</AC>
<Strength>N/A</Strength>
<Stealth>N/A</Stealth>
<Weight>10lbs</Weight>
</Armor>
<Armor>
<Type>Studded Leather</Type>
<Cost>45gp</Cost>
<AC>12 + Dex modifier</AC>
<Strength>N/A</Strength>
<Stealth>N/A</Stealth>
<Weight>13lbs</Weight>
</Armor>
<Armor>
<Type>Hide</Type>
<Cost>10gp</Cost>
<AC>12 + Dex modifier (Max 2)</AC>
<Strength>N/A</Strength>
<Stealth>N/A</Stealth>
<Weight>12lbs</Weight>
</Armor>
<Armor>