将文本块绑定到纯 xaml 中的当前列表框项 [英] bind textblock to current listbox item in pure xaml

查看:23
本文介绍了将文本块绑定到纯 xaml 中的当前列表框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have some data saved in a xml file. Those will be displayed in a listbox. Now, when i change the listbox Selectedindex, i would like to update the other information in the textblock based on the selectedindex.

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

is there any way to do this in pure xaml? if not, how would i bind the Textblock to the selecteditem in the listbox ?

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

非常感谢任何帮助..

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

推荐答案

你可以这样绑定:

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

SelectedItem 是一个 XmlElement.

SelectedItem is an XmlElement.

这里是一些示例代码,如何在后面的代码中访问 XmlDataProvider 的数据并将其应用为 TextBox 的 DataContent.

Here is a bit of sample code how to access the data of the XmlDataProvider in code behind and apply it as DataContent of a TextBox.

像这样更改 TextBox.Text 绑定:

Change the TextBox.Text binding like this:

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

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

In code behind get the XML data from the XmlDataProvider and set the DataContext of the TextBox:

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.

Note that the StackPanel with the XmlDataProvider in its resource dictionary has now got a name. If this code shall run during application initialization (e.g. in Window constructor), the XmlDataProvider.IsAsynchronous property must be set to false.

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

You should now be able to change the DataContext to another indexed item of the books collection in a button click handler.

这篇关于将文本块绑定到纯 xaml 中的当前列表框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆