XAML/Blend 相关数据绑定 [英] XAML/Blend dependent data binding

查看:26
本文介绍了XAML/Blend 相关数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个绑定到 XPath 的列表框,相对于另一个列表框当前选中的项目.

I want to create a listbox that'll be bound to XPath, relative to the other listbox's currently selected item.

它使用 XmlDataProvider 作为数据,XML 文件如下所示:

It's using XmlDataProvider for the data, and the XML file looks like this:

<Programs>
    <Program name="...">
        <Step name="..."/>
        <Step name="..."/>
    </Program>
    <Program name="another">

    ...

</Programs

因此,父"列表框列出了所有程序,而子"仅显示当前程序中的步骤.这种类型的绑定叫什么?

So, the "parent" listbox is listing out all the programs, while "child" shows only steps from the current program. What's such a type of binding called?

推荐答案

给你.希望这能回答您的问题.

Here you go. Hope this answers your question.

<Window x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:StackOverflow"
        xmlns:uc="clr-namespace:StackOverflow.UserControls"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <XmlDataProvider x:Key="xml">
            <x:XData>
                <Programs xmlns="">
                    <Program name="Program">
                        <Step name="Step1"/>
                        <Step name="Step2"/>
                    </Program>
                    <Program name="Program2">
                        <Step name="Step3"/>
                        <Step name="Step4"/>
                    </Program>
                </Programs>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>

    <Grid>
        <StackPanel>
            <ListBox x:Name="parent" ItemsSource="{Binding Source={StaticResource xml}, XPath=Programs/Program}" 
                     Height="100">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding XPath=@name}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

            <ListBox DataContext="{Binding ElementName=parent, Path=SelectedItem}" ItemsSource="{Binding XPath=Step}" 
                     Height="100">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding XPath=@name}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>

    </Grid>
</Window>

这篇关于XAML/Blend 相关数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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