【问题标题】:Trying to pull additional values from XML-bound comboBox that are not DisplayMemberPath, etc尝试从非 DisplayMemberPath 等的 XML 绑定组合框中提取其他值
【发布时间】:2014-12-09 06:18:17
【问题描述】:

我有一个绑定到 XML 文件的组合框。用户从该组合框中选择一个站点,然后该组合框又绑定到另一个组合框,以便他们可以在该站点选择一个子网。

这很好用。但是,现在我需要从那个 xml 文件中的子网节点中提取其他信息,并且完全丢失了。我处于陀螺仪模式(疯狂地旋转,实际上没有到达任何地方)。我对 WPF 还是很陌生,这是我第一次尝试任何类型的数据绑定。 (另外,这不是我的“日常工作”——我在系统方面,但喜欢编写脚本或编码任何可以节省我时间的东西。)

这是我目前所拥有的:

xml文件:

<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>

XAML 中的 XML 绑定:

<Grid>
    <Grid.DataContext>
        <XmlDataProvider 
            x:Name="xmlSites"
            XPath="Sites/Site"
            Source="SiteInfo.xml" />
</Grid.DataContext>

选择站点的组合框:

<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>

到目前为止,一切都很好。一切正常。但是....现在在后面的代码中,我需要获取值 来自 SelectedItem 的 NetworkAddress、DHCPSrv 和 DHCPSrvType。

除了很多没用的尴尬事(我就像一只拿着电动工具的猴子),我 尝试了以下变化:

Dim DHCPSrv As String
Dim cboItem As ComboBoxItem = TryCast(DHCPServerCboBox.SelectedItem, ComboBoxItem)
DHCPSrv = Convert.ToString(cboItem.DHCPSvr)

我得到“DHCPSrv 不是‘System.Windows.Controls.ComboBoxItem’的成员。如果我正在翻译的话 正确的意思是“再猜一次……”

我一直在试图弄清楚 SelectedItem 是否是我应该关注的线程,但我可能会 完全脱离基地。任何人都可以提供任何帮助将不胜感激!

【问题讨论】:

    标签: xml wpf vb.net xaml combobox


    【解决方案1】:

    使用以下 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>
    

    您可以在 C# 中直接使用 Xpath,如下所示:

        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;
        }
    

    【讨论】:

    • 感谢您抽出宝贵时间整理出如此详细的答案!我认为这让我朝着正确的方向前进。我想将它们(例如 dhcpServer)直接绑定到 var 并跳过文本框(我在代码中使用这些,而不是在 UI 中显示)。所以,我应该看看如何在代码中/在 XAML 之外绑定变量......? (我现在正在做一些挖掘,看看我能想出什么。)
    • 我已经更新了答案,希望现在能满足您的需求。干杯!
    • 成功了!绝对完美!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多