控制项的资料系结

时间:2021-08-29 06:55:59

1、前台代码

<Window x:Class="控制项的资料系结_Master_Details.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:控制项的资料系结_Master_Details" 
        Title="MainWindow" Width="400" Height="500">

    <Grid >       
        <Grid.Resources>
            <ObjectDataProvider x:Key="myData" ObjectType="{x:Type src:LeagueList}" />
            <DataTemplate x:Key="myTemplate">
                <StackPanel>               
                 <Image Source="{Binding Path=Image}" VerticalAlignment="Center" Width="80"/>
                  <TextBlock Text="{Binding Path=Name}" FontSize="20" HorizontalAlignment="Center" Foreground="Blue" />
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>

        <DockPanel Name="dockPaneMain"  DataContext="{Binding Source={StaticResource myData}}">
            
            <TextBlock HorizontalAlignment="Center" DockPanel.Dock="Top" FontSize="25" FontWeight="Bold"> NBA联盟</TextBlock>
        
            <DockPanel Name="dockPanelLeft" Margin="10,0,10,0" DockPanel.Dock="Left">
                <TextBlock DockPanel.Dock="Top" FontSize="20" HorizontalAlignment="Center">联盟分区</TextBlock>  
             <ListBox DockPanel.Dock="Top"  Name="listBox1" IsSynchronizedWithCurrentItem="True"  ItemsSource="{Binding}" ItemTemplate="{DynamicResource myTemplate}"/>
            </DockPanel>

            <DockPanel Name="dockPanelRight" Margin="10,0,10,0" DockPanel.Dock="Left">
                <TextBlock DockPanel.Dock="Top" FontSize="20" HorizontalAlignment="Center" Text="{Binding Path=Name}"></TextBlock>
                <ListBox Name="listBox2" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Teams}" ItemTemplate="{DynamicResource myTemplate}"/>
            </DockPanel>
            
        </DockPanel>
    </Grid>
</Window>

2、数据源

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 控制项的资料系结_Master_Details
{
    /// <summary>
    /// 球队
    /// </summary>
    public class Team
    {
        private string name = "";
        private string image = "";
        public Team(string name,string image)
        {
            this.name = name;
            this.image = image;
        }
        public string Name
        {
            get { return name; }
        }
        public string Image
        {
            get { return image; }
        }
    }
    public class TeamList : List<Team>
    {
        public TeamList()
            : base()
        { }
    }
    /// <summary>
    /// 联盟
    /// </summary>
    public class League
    {
        string name = "";
        TeamList teams = null;
        public League(string name)
        {
            this.name = name;
            teams = new TeamList(); 
        }
        public string Name
        {
            get { return name; }
        }
        public TeamList Teams
        {
            get { return teams; }
        }
    }
    public class LeagueList : List<League>
    {
        public LeagueList()
            : base()
        {
            League east = new League("东部联盟");
            Add(east);
            east.Teams.Add(new Team("公牛","./image/gn.jpg"));
            east.Teams.Add(new Team("热火", "./image/rh.jpg"));
            east.Teams.Add(new Team("步行者", "./image/bxz.jpg"));
            east.Teams.Add(new Team("凯尔特人", "./image/ketr.jpg"));
            east.Teams.Add(new Team("老鹰", "./image/ly.jpg"));

            League weast = new League("西部联盟");
            Add(weast);
            weast.Teams.Add(new Team("马刺", "./image/mc.jpg"));
            weast.Teams.Add(new Team("雷霆", "./image/lt.jpg"));
            weast.Teams.Add(new Team("湖人", "./image/hr.jpg"));
            weast.Teams.Add(new Team("灰熊", "./image/hx.jpg"));
            weast.Teams.Add(new Team("快船", "./image/kc.jpg"));
        }
    }   
}

控制项的资料系结

3、源代码下载:点击打开链接