XAML/Blend 相关数据绑定

发布于 2024-09-16 08:08:26 字数 363 浏览 2 评论 0原文

我想创建一个相对于其他列表框当前所选项目绑定到 XPath 的列表框。

它使用 XmlDataProvider 获取数据,XML 文件如下所示:

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

    ...

</Programs

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-09-23 08:08:26

干得好。希望这能回答您的问题。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文