简体   繁体   中英

c# WPF combobox select item when ItemsSource binded

I trying select programmatically item in combobox where item source are binded to the xml file. The problem is that combobox.SelectedItem or combobox.SelectedValue not work. Anyone have some suggestion?

My SectionComboBox items are dependent on selected TypeCombobox item. I want to set both combobox selection in some case from strings.

XAML:

<ComboBox ItemsSource="{Binding}" DisplayMemberPath="ProfileType.Type"  IsSynchronizedWithCurrentItem="True" 
Name="TypeCombobox" HorizontalAlignment="Left" 
Margin="250,5,0,0" VerticalAlignment="Top" Width="50" Height="20" VerticalContentAlignment="Center" 
SelectionChanged="TypeCombobox_SelectionChanged" 
SelectedValuePath="Content" SelectedItem="{Binding SelectedType}"/>
 
<ComboBox ItemsSource="{Binding SelectedItem.ProfileSection, ElementName=TypeCombobox}" 
DisplayMemberPath="Section" 
IsSynchronizedWithCurrentItem="True"  Name="SectionComboBox" 
HorizontalAlignment="Left" Margin="310,5,0,0" VerticalAlignment="Top" 
Width="140" Height="20" VerticalContentAlignment="Center" SelectionChanged="SectionComboBox_SelectionChanged" 
SelectedValuePath="Content" SelectedItem="{Binding SelectedSection}"/>

XML:

<?xml version="1.0" encoding="utf-8" ?>
<SteelProfiles>
<Profile>
<ProfileType Name="UB" />
<ProfileSection Name="127x76x13" />
<ProfileSection Name="152x89x16" />
<ProfileSection Name="178x102x19" />
<ProfileSection Name="203x102x23" />
<ProfileSection Name="203x133x25" />
<ProfileSection Name="203x133x30" />
<ProfileSection Name="254x102x22" />
<ProfileSection Name="254x102x25" />
<ProfileSection Name="254x102x28" />
<ProfileSection Name="254x146x31" />
</Profile>
<Profile>
<ProfileType Name="PFC" />
<ProfileSection Name="100x50x10" />
<ProfileSection Name="125x65x15" />
<ProfileSection Name="150x75x18" />
<ProfileSection Name="150x90x24" />
<ProfileSection Name="180x75x20" />
<ProfileSection Name="180x90x26" />
<ProfileSection Name="200x75x23" />
<ProfileSection Name="200x90x30" />
<ProfileSection Name="230x75x26" />
<ProfileSection Name="230x90x32" />
<ProfileSection Name="260x75x28" />
<ProfileSection Name="260x90x35" />
<ProfileSection Name="300x90x41" />
<ProfileSection Name="300x100x46" />
<ProfileSection Name="380x100x54" />
<ProfileSection Name="430x100x64" />
</Profile>
</SteelProfiles>

SteelProfile.sc

namespace SteelWork
{
    [XmlRoot(ElementName = "SteelProfiles")]
    public class SteelProfiles
    {
        [XmlElement(ElementName = "Profile")]
        public List<Profile> Profile { get; set; }
    }

    [XmlRoot(ElementName = "Profile")]
    public class Profile
    {
        [XmlElement(ElementName = "ProfileType")]
        public ProfileType ProfileType { get; set; }
        [XmlElement(ElementName = "ProfileSection")]
        public List<ProfileSection> ProfileSection { get; set; }
    }


    [XmlRoot(ElementName = "ProfileType")]
    public class ProfileType
    {
        [XmlAttribute(AttributeName = "Name")]
        public string Type { get; set; }

    }
    [XmlRoot(ElementName = "ProfileSection")]
    public class ProfileSection
    {
        [XmlAttribute(AttributeName = "Name")]
        public string Section { get; set; }

    }
}

Behind Xaml:

public partial class SteelProperties : Window
{
    public static SteelCarpentryTruss SteelTruss { get; set; }
    public string SelectedType { get; set; }
    public string SelectedSection { get; set; }
    public SteelProperties(ObjectId carpentyTrussId)
    {
        InitializeComponent();

        SteelProfiles steelProfiles = null;
        string path = @"C:\Program Files\Autodesk\ApplicationPlugins\SteelWork.bundle\Support\SteelProfiles.xml";
        XmlSerializer serializer = new XmlSerializer(typeof(SteelProfiles));
        StreamReader reader = new StreamReader(path);
        steelProfiles = (SteelProfiles)serializer.Deserialize(reader);
        reader.Close();
        TypeCombobox.ItemsSource = steelProfiles.Profile;

        TypeCombobox.SelectedIndex = 1;
        Document doc = Application.DocumentManager.MdiActiveDocument;
        var tr = doc.TransactionManager.StartTransaction();
        using (tr)
        {
            SteelTruss = new SteelCarpentryTruss(tr, carpentyTrussId);
            ACAStdFunctions.ScheduleData.SetPropertyValueOnEntity(tr, SteelTruss.CarpentryTruss, "Steel", "Name", SteelTruss.TrussName);
            NameTextBox.Text = SteelTruss.TrussName;

            if (SteelTruss.Section != null && SteelTruss.Section != "")
            {

                string sectiontype = SteelTruss.SectionType;
                TypeCombobox.SelectedItem = sectiontype;
                string sectionProfile = SteelTruss.SectionProfile;
                SectionComboBox.SelectedItem = sectionProfile;


            }

            tr.Commit();
        }
        

    }

Ok I resolved this myself. The problem was that I tried set item as string. I modified code to set correct Profile object to the combobox.

if (SteelTruss.Section != null && SteelTruss.Section != "")
   {
   Profile profile = steelProfiles.Profile.SingleOrDefault
          (item => item.ProfileType.Type == SteelTruss.SectionType);
   TypeCombobox.SelectedItem = profile;
   SectionComboBox.SelectedItem = profile.ProfileSection.SingleOrDefault
          (item => item.Section == SteelTruss.SectionProfile);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM