![](/img/trans.png)
[英]how to select an item in a combobox in another form when combobox is binded to a table in c#
[英]c# WPF combobox select item when ItemsSource binded
我在 combobox 中以編程方式嘗試 select 項目,其中項目源綁定到 xml 文件。 問題是 combobox.SelectedItem 或 combobox.SelectedValue 不起作用。 有人有什么建議嗎?
我的 SectionComboBox 項目依賴於選定的 TypeCombobox 項目。 在某些情況下,我想從字符串中同時設置 combobox 選擇。
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; }
}
}
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();
}
}
好的,我自己解決了這個問題。 問題是我嘗試將項目設置為字符串。 我修改了代碼以將正確的配置文件 object 設置為 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.