在XmlDataProvider上使用主-从绑定(Master-Detail Binding)

<Window x:Class="WpfApplication1.MasterDetailBinding"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MasterDetailBinding" Height="300" Width="300">
    <Window.Resources>
        <XmlDataProvider x:Key="xml" XPath="/countries/country">
            <x:XData>
                <countries xmlns="">
                    <country name="中国">
                        <province name="江苏">
                            <city>丹阳</city>
                            <city>常州</city>
                            <city>无锡</city>
                        </province>
                        <province name="安徽">
                            <city>合肥</city>
                            <city>六安</city>
                        </province>
                        <province name="浙江">
                            <city>杭州</city>
                            <city>余杭</city>
                        </province>
                    </country>
                    <country name="美国">
                        <province name="加利福尼亚">
                            <city>洛杉矶</city>
                            <city>圣迭戈</city>
                        </province>
                        <province name="佛罗里达">
                            <city>迈阿密</city>
                        </province>
                    </country>
                </countries>
            </x:XData>
        </XmlDataProvider>
              
        <Style TargetType="HeaderedContentControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="HeaderedContentControl">
                        <DockPanel>
                            <Border DockPanel.Dock="Top" Margin="3" CornerRadius="3" Background="DarkRed">
                                <TextBlock Text="{TemplateBinding Header}" HorizontalAlignment="Center" Foreground="White" />
                            </Border>
                            <ContentPresenter ContentSource="Content" ContentTemplate="{TemplateBinding ContentTemplate}" />
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
              
        <Style TargetType="ListBoxItem">
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="Border" Padding="2" SnapsToDevicePixels="True">
                            <ContentPresenter />
                        </Border>
                              
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="YellowGreen" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
              
    </Window.Resources>
          
    <Grid DataContext="{Binding Source={StaticResource xml}}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <HeaderedContentControl Header="国家">
            <ListBox ItemsSource="{Binding}" DisplayMemberPath="@name" IsSynchronizedWithCurrentItem="True" />
        </HeaderedContentControl>
        <HeaderedContentControl Header="省/州" Grid.Column="1">
            <ListBox Name="lbProvince" ItemsSource="{Binding XPath=province}" DisplayMemberPath="@name" IsSynchronizedWithCurrentItem="True" />
        </HeaderedContentControl>
        <HeaderedContentControl Header="市" Grid.Column="2">
            <ListBox DataContext="{Binding ElementName=lbProvince, Path=SelectedItem}" ItemsSource="{Binding XPath=city}" IsSynchronizedWithCurrentItem="True" />
        </HeaderedContentControl>
    </Grid>
          
</Window>

点击查看原图

知识共享许可协议
《在XmlDataProvider上使用主-从绑定(Master-Detail Binding)》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     163.53ms