XAML/Blend dependent data binding

我的梦境 提交于 2019-12-08 04:11:08

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/3579866/xaml-blend-dependent-data-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!