silverlight combobox数据绑定

本文介绍如何在Silverlight 2中使用ComboBox进行数据绑定,并通过实例演示了如何实现省份与城市的联动选择功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

silverlight2.0正式版比较之前的beta版本来说又多了几个控件例:ComboBox, ProgressBar, 和 PasswordBox等.以下是个人在运用ComboBox的数据绑定过程中做了个人小结:

 xaml.

ContractedBlock.gif ExpandedBlockStart.gif Code
<UserControl x:Class="CheckBoxAllDataGrid.ComboBoxTest"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width
="400" Height="300" Loaded="UserControl_Loaded">
    
<Grid x:Name="LayoutRoot" Background="White">
        
<Grid.RowDefinitions>
            
<RowDefinition Height="30"/>
            
<RowDefinition Height="Auto"/>
        
</Grid.RowDefinitions>
        
<Grid.ColumnDefinitions>
            
<ColumnDefinition Width="100"/>
            
<ColumnDefinition Width="Auto"/>
        
</Grid.ColumnDefinitions>
        
<ComboBox x:Name="CBox_City" Grid.Row="0" Grid.Column="0" Width="60" HorizontalAlignment="Left"></ComboBox>
        
<ComboBox x:Name="CBox_Channel" Grid.Row="0" Grid.Column="1" Width="60" HorizontalAlignment="Left"></ComboBox>
    
</Grid>
</UserControl>

 

 cs.

 

ContractedBlock.gif ExpandedBlockStart.gif Code
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace CheckBoxAllDataGrid
{
    
public partial class ComboBoxTest : UserControl
    {
        
public ComboBoxTest()
        {
            InitializeComponent();
        }
        
private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            
this.CBox_City.SelectionChanged += new SelectionChangedEventHandler(CBox_City_SelectionChanged);
            
this.CBox_Channel.SelectionChanged += new SelectionChangedEventHandler(CBox_Channel_SelectionChanged);

            
this.CBox_City.ItemsSource = CitySource();
            
this.CBox_City.DisplayMemberPath = "name";
            
this.CBox_City.UpdateLayout();
            
this.CBox_City.SelectedIndex = 0
;
        }
        
//城市
        void CBox_City_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
if (this.CBox_City.SelectedItem != null)
            {
                var CitAlly 
= this.CBox_City.SelectedItem;
                
int CityID = ((city)CitAlly).id;

                
this.CBox_Channel.ItemsSource = (from a in ChannelSource() where a.cityid == CityID select a);
                
this.CBox_Channel.DisplayMemberPath = "channelName";
                
this.CBox_City.UpdateLayout();
                
this.CBox_Channel.SelectedIndex = 0;
            }
        }
        
//频道
        void CBox_Channel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
                        
        }

        
public List<city> CitySource()
        {
            List
<city> cityinfo = new List<city>();
            cityinfo.Add(
new city { id = 1, name = "北京" });
            cityinfo.Add(
new city { id = 2, name = "福建" });
            
return cityinfo;
        }

        
public List<channel> ChannelSource()
        {
            List
<channel> channelinfo = new List<channel>();
            channelinfo.Add(
new channel { id = 11, channelName = "海淀区电视台", cityid = 1 });
            channelinfo.Add(
new channel { id = 22, channelName = "朝阳区电视台", cityid = 1 });
            channelinfo.Add(
new channel { id = 33, channelName = "福州市电视台", cityid = 2 });
            channelinfo.Add(
new channel { id = 44, channelName = "厦门市电视台", cityid = 2 });

            
return channelinfo;
        }

        
public struct city
        {
            
public int id { getset; }
            
public string name { getset; }
        }

        
public struct channel
        {
            
public int id { getset; }
            
public string channelName { getset; }
            
public int cityid { getset; }
        }
    }
}

 

注:如果在绑定的时候就给他指定SelectedIndex属性则在绑定后马上触发SelectionChanged事件

     所以在页面如果有两个或两个以上的连动(例:省份与城市 福建省(Cbox_pro) -- (Cbox_city) 福州,厦门)的控件

     且要求默认选定一个省份时绑定时应注意:

    1.在省份的绑定this.CBox_City.SelectedIndex = 0; 城市的绑定应写在省份的SelectionChanged事件里

    2.设置this.CBox_City.UpdateLayout();确保 UIElement 的所有子对象位置都正确地进行了布局更新

转载于:https://www.cnblogs.com/kaixun001/archive/2009/02/17/1392817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值