【问题标题】:WPF Databinding to WindowWPF 数据绑定到窗口
【发布时间】:2014-04-03 13:45:44
【问题描述】:

我想将列表框项的 DataBinding 链接到新窗口

“名称”按钮应打开一个带有列表框项的 DataBinding 的新窗口。

绑定是一个 .xml 文件:

<People>
   <Person image="Test.jpg" company="" position="" website="" chat="" notes="">
      <Name first_name="Max" second_name="" last_name="Mustermann" salutation="" />
      <Email email1="" email2="" email3="" />
      <Phone_Numbers business="" private="" mobile="" />
      <Business_Adress street="" place="" state="" postalcode="" country="" />
      <Private_Adress street="" place="" state="" postalcode="" country="" />
   </Person>
</People>

并且新窗口应该链接到 Person 的 Name 元素。

虽然会有不止一个人,但窗口应该链接到正确的人。

这是 XmlDataProvider:

<XmlDataProvider x:Name="XmlData" Source="People.xml" XPath="/People/Person" />

列表框的绑定如下:

<ListBox 
   Grid.Row="0" 
   IsSynchronizedWithCurrentItem="True" 
   ItemsSource="{Binding UpdateSourceTrigger=Explicit}" 
   x:Name="PeopleListBox" 
   SelectionMode="Single">
   <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
         <Setter Property="HorizontalContentAlignment" Value="Stretch" />
      </Style>
   </ListBox.ItemContainerStyle>
   <ListBox.ItemTemplate>
      <DataTemplate>
         <TextBlock>
            <TextBlock.Text>
               <MultiBinding StringFormat="{}{0}, {1} {2}">
                  <Binding XPath="Name/@last_name" />
                  <Binding XPath="Name/@first_name" />
                  <Binding XPath="Name/@second_name" />
               </MultiBinding>
            </TextBlock.Text>
         </TextBlock>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

【问题讨论】:

  • 你有什么问题?
  • 我该怎么做,因为它只打开第一个人
  • 请显示一些代码。
  • 我添加了一些代码

标签: c# wpf data-binding listbox


【解决方案1】:

除非将DataContext 设置为XmlData,否则您的ItemsSource 绑定将不起作用。你没有提到你在哪里定义XmlDataProvider,因为如果在Resources,那么你需要指定x:Key而不是x:Name,然后你必须将它指定为Binding.Source。您也没有说People.xml 是什么,因为它必须作为资源添加到您的解决方案中

<Window ...>
    <Window.Resources>
        <XmlDataProvider x:Key="XmlData" XPath="/People/Person" Source="People.xml"/>
    </Window.Resources>
    <ListBox ItemsSource="{Binding Source={StaticResource XmlData}}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0}, {1} {2}">
                            <Binding XPath="Name/@last_name" />
                            <Binding XPath="Name/@first_name" />
                            <Binding XPath="Name/@second_name" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

或者你可以通过在代码中手动设置XmlDataProvider.Source来手动加载

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2016-03-23
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多