【问题标题】:bind textblock to current listbox item in pure xaml将文本块绑定到纯 xaml 中的当前列表框项
【发布时间】:2012-02-02 12:10:35
【问题描述】:

我有一些数据保存在一个 xml 文件中。 这些将显示在列表框中。 现在,当我更改列表框 Selectedindex 时,我想根据 selectedindex 更新文本块中的其他信息。

有没有办法在纯 xaml 中做到这一点? 如果没有,我将如何将 Textblock 绑定到列表框中的选定项?

编辑: 我如何在不使用列表框的情况下浏览数据?我的意思是使用一个按钮移动到下一个项目和其他按钮向后移动..!!

非常感谢任何帮助..

<Window x:Class="WpfSingleInstance.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <StackPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Cornsilk">

        <StackPanel.Resources>
            <XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
                <x:XData>
                    <Inventory xmlns="">
                        <Books>
                            <Book ISBN="0-7356-0562-9" Stock="in" Number="9">
                                <Title>XML in Action</Title>
                                <Summary>XML Web Technology</Summary>
                            </Book>
                            <Book ISBN="0-7356-1370-2" Stock="in" Number="8">
                                <Title>Programming Microsoft Windows With C#</Title>
                                <Summary>C# Programming using the .NET Framework</Summary>
                            </Book>
                            <Book ISBN="0-7356-1288-9" Stock="out" Number="7">
                                <Title>Inside C#</Title>
                                <Summary>C# Language Programming</Summary>
                            </Book>
                        </Books>
                    </Inventory>
                </x:XData>
            </XmlDataProvider>
        </StackPanel.Resources>

        <TextBlock FontSize="18" FontWeight="Bold" Margin="10"
HorizontalAlignment="Center">XML Data Source Sample</TextBlock>
        <ListBox
Width="265" Height="98" x:Name="lbox" Background="Honeydew" IsSynchronizedWithCurrentItem="True">
            <ListBox.ItemsSource>
                <Binding Source="{StaticResource InventoryData}"
           XPath="*[@Stock='out'] | *[@Number>=8 or @Number=3]"/>
            </ListBox.ItemsSource>

            <ListBox.ItemTemplate>

                <DataTemplate>
                    <TextBlock FontSize="12" Foreground="Red">
      <TextBlock.Text>
        <Binding XPath="Title"/>
      </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <StackPanel DataContext="{StaticResource InventoryData}">
            <TextBlock Text="{Binding  XPath=Book/Title}"/>
            <TextBox Margin="5,31,98,10" x:Name="textBoxMainDetail" Text="{Binding XPath=Book/Summary}" />
        </StackPanel>
    </StackPanel>



</Grid>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    你可以这样绑定:

    <TextBox Text="{Binding ElementName=lbox, Path=SelectedItem[Title].InnerText}" />
    

    SelectedItem 是一个XmlElement

    编辑:下面是一些示例代码,如何在后面的代码中访问 XmlDataProvider 的数据并将其应用为 TextBox 的 DataContent。

    像这样更改 TextBox.Text 绑定:

    <TextBox x:Name="textBoxMainDetail" Text="{Binding Path=[Title].InnerText}" />
    

    在后面的代码中,从 XmlDataProvider 获取 XML 数据并设置 TextBox 的 DataContext:

    XmlDataProvider dataProvider = (XmlDataProvider)stackPanel.Resources["InventoryData"];
    XmlElement books = (XmlElement)dataProvider.Document.SelectNodes(dataProvider.XPath)[0];
    
    // set DataContext to an item from the child node collection
    textBoxMainDetail.DataContext = books.ChildNodes[0];
    

    请注意,资源字典中带有 XmlDataProvider 的 StackPanel 现在有了一个名称。如果此代码应在应用程序初始化期间运行(例如在 Window 构造函数中),则必须将 XmlDataProvider.IsAsynchronous 属性设置为 false。

    您现在应该能够在按钮单击处理程序中将 DataContext 更改为书籍集合的另一个索引项。

    【讨论】:

    • 您好,谢谢它的工作...我还可以添加一个按钮来浏览数据而不使用列表框吗?我的意思是如果我删除列表框,我将如何浏览 xml 数据?moveforward()、moveback() 等等..
    • 我猜在没有 ListBox 的情况下浏览数据可能会更好地通过后面的代码来完成。一些东西应该跟踪当前/选定的项目。
    【解决方案2】:

    您需要将SelectedValuePath 设置为列表框的“标题”。然后使用 elementName 像这样简单地将你的 textBlock 绑定到你的列表框的 selectedValue -

    <ListBox Width="265" Height="98" x:Name="lbox" Background="Honeydew"
                         IsSynchronizedWithCurrentItem="True" SelectedValuePath="Title">
                </ListBox>
                <StackPanel DataContext="{StaticResource InventoryData}">
                    <TextBlock Text="{Binding Path=SelectedValue, ElementName=lbox}"/>
                    <TextBox Margin="5,31,98,10" x:Name="textBoxMainDetail" Text="{Binding XPath=Book/Summary}" />
                </StackPanel>
            </StackPanel>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-21
      • 2015-01-20
      • 2019-05-28
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      相关资源
      最近更新 更多