XML bound Combobox Switch error.

Abe

Member
Joined
Mar 16, 2017
Messages
20
Programming Experience
Beginner
I have a combobox and am trying to do a simple switch statement. I keep getting an error.

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 = "">
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
C#:
ComboBoxItem selectedItem = (ComboBoxItem)armorselect.SelectedItem;
but I do not know how to write it to read the XML.

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>
 
I think you want to work with the SelectedValue property rather than SelectedItem

Thanks for the reply. Changed Slecteditem too SelectedValue. also changed the name to choice to reduce confusion. Still getting an error.

C#:
    ComboBoxItem choice = (ComboBoxItem)armorselect.SelectedValue;
    switch (choice.Content.ToString())

System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ComboBoxItem'.'

(selectedIndex does not work either.)
 
Just do your switch on SelectedValue, don't even cast to a comboboxitem

Sent from my SM-G935T using Tapatalk
 
Progress!

So I did as you suggested

C#:
switch [COLOR=#333333](armorselect.SelectedValue) [/COLOR]


No more errors, and the correct item populates in the combobox but none of the cases work. I did a break point and .SelectedValue is getting the whole XML. "Padded" "14gp" etc etc. Need it to only get the value "Padded", which the combobox is showing.

 
I don't work much with ASP.net, however, your combobox is bound to the "Armor" node which is why you get excess XML.

C#:
<Binding Mode="OneTime" Source="{StaticResource Armor}" XPath="Armor"/>

Your XPath here needs to bind directly to the "Type" if you are looking for the "Padding" value. Likely something like this:

C#:
<Binding Mode="OneTime" Source="{StaticResource Armor}" XPath="Armor\Type"/>
 
Thank you very much Tosa, that did it. You rock!

<Binding Mode="OneTime" Source="{StaticResource Armor}" XPath="Armor/Type"/>
 
Hate to bug you again. Making the change from SelectedItem to SelectedValue while it fixed my case statement, it broke all my Textboxes that were bound to it. They all show up as Null now. Here is their binding. Any ideas? I tried both SelectedItem and SelectedValue both are null.

C#:
DataContext="{Binding SelectedItem, ElementName=armorselect}" Text="{Binding [AC].InnerText}"
DataContext="{Binding SelectedValue, ElementName=armorselect}" Text="{Binding [AC].InnerText}"
 
Without seeing all of your code I can only offer you some pointers. Your comboboxes are now ONLY bound to the single xml element, therefore, if you have your textboxes bound to the combobox then any value other than the "Type" property will be null. I'm sure there is a way to dynamically retrieve the value you need for the textbox as long as you are working with a single datasource. It would be something along the lines of (just pseudocode):

C#:
//Armor/[@Type=combobox.SeletedValue]/Cost

In other words, you need to grab the value of the combobox and dynamically use that in your xpath for the textbox. Sorry I can't give specific code, like I said, I seldom work with asp.net
 
Thanks, I will do some research to try and figure it out, hopefully someone will chime in who knows the answer.

Cannot thank you enough for you time!
 
Back
Top Bottom