试图从XML绑定的组合框中提取不属于DisplayMemberPath等的其他值
我有一个绑定到XML文件的组合框。用户从该组合框中选择一个站点,然后将该站点绑定到另一个组合框,以便用户可以选择该站点的子网 这个很好用。然而,现在我需要从那个xml文件中的子网节点提取额外的信息,我完全丢失了。我处于陀螺仪模式(疯狂地旋转,实际上没有到达任何地方)。我对WPF仍然很陌生,这是我第一次尝试任何类型的数据绑定。(此外,这不是我的“日常工作”——我在系统方面,但喜欢编写脚本或编写任何可以节省我时间的代码。) 以下是我目前掌握的情况: xml文件:试图从XML绑定的组合框中提取不属于DisplayMemberPath等的其他值,xml,wpf,vb.net,xaml,combobox,Xml,Wpf,Vb.net,Xaml,Combobox,我有一个绑定到XML文件的组合框。用户从该组合框中选择一个站点,然后将该站点绑定到另一个组合框,以便用户可以选择该站点的子网 这个很好用。然而,现在我需要从那个xml文件中的子网节点提取额外的信息,我完全丢失了。我处于陀螺仪模式(疯狂地旋转,实际上没有到达任何地方)。我对WPF仍然很陌生,这是我第一次尝试任何类型的数据绑定。(此外,这不是我的“日常工作”——我在系统方面,但喜欢编写脚本或编写任何可以节省我时间的代码。) 以下是我目前掌握的情况: xml文件: <Sites> &l
<Sites>
<Site>
<SiteCode>WDH</SiteCode>
<Name>World Domination Headquarters, Inc.</Name>
<Subnet>
<NetworkAddress>10.10.1.0</NetworkAddress>
<VlanID>1</VlanID>
<VlanName>Default</VlanName>
<DHCPSrv>10.10.1.99</DHCPSrv>
<DHCPSrvType>W2k8</DHCPSrvType>
</Subnet>
<Subnet>
<NetworkAddress>10.10.2.0</NetworkAddress>
<VlanID>2</VlanID>
<VlanName>Mgmt</VlanName>
<DHCPSrv>10.10.2.1</DHCPSrv>
<DHCPSrvType>C2951</DHCPSrvType>
</Subnet>
</Site>
</Sites>
我得到“DHCPSrv不是'System.Windows.Controls.ComboBoxItem'的成员”,如果我翻译的话
正确的意思是“再猜一次…”
我一直在想SelectedItem是否是我应该使用的线程,但我可能会这样做
完全脱离实际。任何人能提供的任何帮助都将不胜感激!!使用以下XAML:
<Window x:Class="WpfAnswer001.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300">
<StackPanel Orientation="Vertical">
<StackPanel.DataContext>
<XmlDataProvider
x:Name="xmlSites"
XPath="Sites/Site"
Source="SiteInfo.xml" />
</StackPanel.DataContext>
<ComboBox x:Name="SiteNameCboBox"
ItemsSource="{Binding}"
DisplayMemberPath="Name"
SelectionChanged="SiteNameCboBox_SelectionChanged"
SelectedIndex="-1">
</ComboBox>
</StackPanel>
</Window>
感谢您花时间整理出如此详细的答案!我认为这让我朝着正确的方向前进。我想将它们(如dhcpServer)直接绑定到var,并跳过文本框(我在代码中使用它们,而不是在UI中显示)。因此,我应该看看如何在代码/XAML外部绑定变量。。。?(我现在正在做一些挖掘,看看我能想出什么。)我已经更新了答案,我希望它现在适合你的需要。干杯!成功了!绝对完美!非常感谢!
<ComboBox x:Name="SiteNameCboBox"
ItemsSource="{Binding}"
DisplayMemberPath="Name"
SelectedIndex="-1"
</ComboBox>
<ComboBox x:Name="DHCPServerCboBox"
DataContext="{Binding SelectedItem, ElementName=SiteNameCboBox}"
ItemsSource="{Binding XPath=Subnet}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding XPath="NetworkAddress"/>
<Binding XPath="VlanName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Dim DHCPSrv As String
Dim cboItem As ComboBoxItem = TryCast(DHCPServerCboBox.SelectedItem, ComboBoxItem)
DHCPSrv = Convert.ToString(cboItem.DHCPSvr)
<Window x:Class="WpfAnswer001.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300">
<StackPanel Orientation="Vertical">
<StackPanel.DataContext>
<XmlDataProvider
x:Name="xmlSites"
XPath="Sites/Site"
Source="SiteInfo.xml" />
</StackPanel.DataContext>
<ComboBox x:Name="SiteNameCboBox"
ItemsSource="{Binding}"
DisplayMemberPath="Name"
SelectionChanged="SiteNameCboBox_SelectionChanged"
SelectedIndex="-1">
</ComboBox>
</StackPanel>
</Window>
private void SiteNameCboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = SiteNameCboBox.SelectedItem as XmlElement;
var networkAddress = selectedItem.SelectSingleNode("Subnet/NetworkAddress").InnerText;
var dhscpServer = selectedItem.SelectSingleNode("Subnet/DHCPSrv").InnerText;
var dhcpServerType = selectedItem.SelectSingleNode("Subnet/DHCPSrvType").InnerText;
}