Silverlight ListBox 控制元件使用介紹

暖楓無敵發表於2014-04-10

1、ListBox的單選和多選

預設情況下只支援單選 
通過設定其SelectionMode可以支援多選

SelectionMode - 選擇模式 [System.Windows.Controls.SelectionMode 列舉]

                    Single - 只允許單選
                    Multiple - 可以多選(不需要任何輔助鍵)
                    Extended - 可以多選(需要 Ctrl 或 Shift 的配合)

 

2、資料模板和資料繫結

                               <ListBox x:Name="lbPerson" ItemsSource="{Binding}" SelectionMode="Multiple"  Height="118" Margin="53,55,8,0" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectionChanged="lbPerson_SelectionChanged">
                                        <ListBox.BorderBrush>
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                <GradientStop Color="#FFA3AEB9" Offset="0.058"/>
                                                <GradientStop Color="#FF617584" Offset="1"/>
                                            </LinearGradientBrush>
                                        </ListBox.BorderBrush>
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock x:Name="content" FontWeight="Bold" Foreground="#0b333c" FontSize="14" TextWrapping="Wrap"  Text="{Binding CNAME}" Width="50" Height="20"/>
                                                    <TextBlock x:Name="content2" FontWeight="Bold" Foreground="#0b333c" FontSize="14" TextWrapping="Wrap"  Text="{Binding CMCODE}" Width="100" Height="20"/>
                                                </StackPanel>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                    </ListBox>

 

        /// <summary>
        /// 從WebService獲取資料,繫結到ListBox的DataContext上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbCounty_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            MapClient.ServiceReference1.Region ws = (sender as ComboBox).SelectedItem as MapClient.ServiceReference1.Region;

            getMapDataSoapClient client = new getMapDataSoapClient();
            client.getRYByADDVCDCompleted += new EventHandler<getRYByADDVCDCompletedEventArgs>(client_getRYByADDVCDCompleted);
            client.getRYByADDVCDAsync(ws.RegionCode);
        }

        void client_getRYByADDVCDCompleted(object sender, getRYByADDVCDCompletedEventArgs e)
        {
            ObservableCollection<FXJGRY> lists = e.Result;
            this.lbPerson.DataContext = lists;
        }

       public T FindFirstVisualChild<T>(DependencyObject obj, string childName) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T && child.GetValue(NameProperty).ToString() == childName)
                {
                    return (T)child;
                }
                else
                {
                    T childOfChild = FindFirstVisualChild<T>(child, childName);
                    if (childOfChild != null)
                    {
                        return childOfChild;
                    }
                }
            }
            return null;
        }

        Dictionary<string, string> dic = new Dictionary<string, string>();
        private void lbPerson_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            ListBoxItem _selectedItem = (ListBoxItem)(lbPerson.ItemContainerGenerator.ContainerFromItem(this.lbPerson.SelectedItem));

            TextBlock myTxt = FindFirstVisualChild<TextBlock>(_selectedItem, "content2");
            if (!dic.ContainsValue(myTxt.Text.ToString().Trim()))
            {
                dic.Add(myTxt.Text.Trim(),myTxt.Text.Trim());
            }
            foreach (KeyValuePair<string, string> kvp in dic)
            {
                MessageBox.Show(kvp.Value.ToString());
            }
        }

 

3、效果如圖

 

 

相關文章