上接稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 绑定数据到ListBox

  1. 云栖社区>
  2. 博客>
  3. 正文

上接稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 绑定数据到ListBox

技术小胖子 2017-11-06 01:26:00 浏览610
展开阅读全文

2、DataGrid02.xaml
<UserControl x:Class="Silverlight20.Data.DataGrid02" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"> 
        <StackPanel HorizontalAlignment="Left"> 

                <StackPanel Orientation="Vertical" Margin="5"> 
                 
                        <CheckBox Content="是否只读" Margin="5"    
                                Checked="chkReadOnly_Changed" Unchecked="chkReadOnly_Changed" /> 

                        <CheckBox Content="冻结列" Margin="5" 
                                Checked="chkFreezeColumn_Changed" Unchecked="chkFreezeColumn_Changed"/> 

                        <CheckBox Content="行的选中模式,是否只能单选" Margin="5" 
                                Checked="chkSelectionMode_Changed" Unchecked="chkSelectionMode_Changed" /> 

                        <CheckBox Content="是否允许拖动列" IsChecked="true" Margin="5"    
                                Checked="chkColReorder_Changed" Unchecked="chkColReorder_Changed"/> 

                        <CheckBox Content="是否允许改变列的宽度" IsChecked="true" Margin="5" 
                                Checked="chkColResize_Changed" Unchecked="chkColResize_Changed"/> 
                                 
                        <CheckBox Content="是否允许列的排序" IsChecked="true" Margin="5" 
                                Checked="chkColSort_Changed" Unchecked="chkColSort_Changed"/> 

                        <CheckBox Content="改变表格的垂直分隔线的 Brush" Margin="5"    
                                Checked="chkCustomGridLineVertical_Changed" Unchecked="chkCustomGridLineVertical_Changed"/> 
                                 
                        <CheckBox Content="改变表格的水平分隔线的 Brush" Margin="5" 
                                Checked="chkCustomGridLineHorizontal_Changed" Unchecked="chkCustomGridLineHorizontal_Changed"/> 

                        <ComboBox SelectionChanged="cboHeaders_SelectionChanged" Width="200" HorizontalAlignment="Left"> 
                                <ComboBoxItem Content="列头和行头均显示" Tag="All" /> 
                                <ComboBoxItem Content="只显示列头(默认值)" Tag="Column" IsSelected="True" /> 
                                <ComboBoxItem Content="只显示行头" Tag="Row" /> 
                                <ComboBoxItem Content="列头和行头均不显示" Tag="None" /> 
                        </ComboBox> 

                </StackPanel> 

                <!-- 
                GridLinesVisibility - 表格分隔线的显示方式 [System.Windows.Controls.DataGridGridLinesVisibility枚举] 
                        DataGridGridLinesVisibility.None - 都不显示 
                        DataGridGridLinesVisibility.Horizontal - 只显示水平分隔线 
                        DataGridGridLinesVisibility.Vertical - 只显示垂直分隔线。默认值 
                        DataGridGridLinesVisibility.All - 显示水平和垂直分隔线 
                RowBackground - 奇数数据行背景 
                AlternatingRowBackground - 偶数数据行背景 
                --> 
                <data:DataGrid x:Name="DataGrid1" Margin="5" 
                        Width="400" Height="200" 
                        AutoGenerateColumns="False" 
                        GridLinesVisibility="All" 
                        RowBackground="White" 
                        AlternatingRowBackground="Yellow" 
                        ItemsSource="{Binding}" 
                > 

                        <data:DataGrid.Columns> 
                         
                                <!-- 
                                IsReadOnly - 该列的单元格是否只读 
                                CanUserReorder - 该列是否可以拖动 
                                CanUserResize - 该列是否可以改变列宽 
                                CanUserSort - 该列是否可以排序 
                                SortMemberPath - 该列的排序字段 
                                --> 
                                <data:DataGridTextColumn Header="姓名" Binding="{Binding Name}"    
                                        IsReadOnly="False" 
                                        CanUserReorder="True"    
                                        CanUserResize="True"    
                                        CanUserSort="True"    
                                        SortMemberPath="Name"    
                                /> 
                                 
                                <!-- 
                                Width - 列宽 
                                        Auto - 根据列头内容的宽度和单元格内容的宽度自动设置列宽 
                                        SizeToCells - 根据单元格内容的宽度设置列宽 
                                        SizeToHeader - 根据列头内容的宽度设置列宽 
                                        Pixel - 像素值 
                                --> 
                                <data:DataGridTextColumn Header="生日" Binding="{Binding DayOfBirth}" Width="100" /> 
                                <data:DataGridTextColumn Header="年龄" Binding="{Binding Age}" /> 
                                <data:DataGridCheckBoxColumn Header="性别" Binding="{Binding Male}" /> 
                                <data:DataGridTextColumn Header="姓名" Binding="{Binding Name}" /> 
                                <data:DataGridTextColumn Header="生日" Binding="{Binding DayOfBirth}" /> 
                                <data:DataGridTextColumn Header="年龄" Binding="{Binding Age}" /> 
                                <data:DataGridCheckBoxColumn Header="性别" Binding="{Binding Male}" /> 
                        </data:DataGrid.Columns> 

                </data:DataGrid> 

        </StackPanel> 
</UserControl>
 
DataGrid02.xaml.cs
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Net; 
InBlock.gifusing System.Windows; 
InBlock.gifusing System.Windows.Controls; 
InBlock.gifusing System.Windows.Documents; 
InBlock.gifusing System.Windows.Input; 
InBlock.gifusing System.Windows.Media; 
InBlock.gifusing System.Windows.Media.Animation; 
InBlock.gifusing System.Windows.Shapes; 
InBlock.gif 
InBlock.gifnamespace Silverlight20.Data 
InBlock.gif
InBlock.gif        public partial class DataGrid02 : UserControl 
InBlock.gif        { 
InBlock.gif                public DataGrid02() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                        BindData(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                void BindData() 
InBlock.gif                { 
InBlock.gif                        var source = new Data.SourceData(); 
InBlock.gif 
InBlock.gif                        // 设置 DataGrid 的数据源 
InBlock.gif                        DataGrid1.DataContext = source.GetData(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void chkReadOnly_Changed(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CheckBox chk = sender as CheckBox; 
InBlock.gif 
InBlock.gif                        // IsReadOnly - 单元格是否只读。默认值 false 
InBlock.gif                        DataGrid1.IsReadOnly = (bool)chk.IsChecked; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void chkFreezeColumn_Changed(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CheckBox chk = sender as CheckBox; 
InBlock.gif 
InBlock.gif                        // FrozenColumnCount - 表格所冻结的列的总数(从左边开始数)。默认值 0 
InBlock.gif                        if (chk.IsChecked == true
InBlock.gif                                DataGrid1.FrozenColumnCount = 1; 
InBlock.gif                        else if (chk.IsChecked == false
InBlock.gif                                DataGrid1.FrozenColumnCount = 0; 
InBlock.gif                } 
InBlock.gif                
InBlock.gif                private void chkSelectionMode_Changed(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CheckBox chk = sender as CheckBox; 
InBlock.gif 
InBlock.gif                        // SelectionMode - 行的选中模式 [System.Windows.Controls.DataGridSelectionMode枚举] 
InBlock.gif                        //         DataGridSelectionMode.Single - 只能单选 
InBlock.gif                        //         DataGridSelectionMode.Extended - 可以多选(通过Ctrl或Shift的配合)。默认值 
InBlock.gif                        if (chk.IsChecked == true
InBlock.gif                                DataGrid1.SelectionMode = DataGridSelectionMode.Single; 
InBlock.gif                        else if (chk.IsChecked == false
InBlock.gif                                DataGrid1.SelectionMode = DataGridSelectionMode.Extended; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void chkColReorder_Changed(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CheckBox chk = sender as CheckBox; 
InBlock.gif 
InBlock.gif                        // CanUserReorderColumns - 是否允许拖动列。默认值 true 
InBlock.gif                        if (DataGrid1 != null
InBlock.gif                                DataGrid1.CanUserReorderColumns = (bool)chk.IsChecked; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void chkColResize_Changed(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CheckBox chk = sender as CheckBox; 
InBlock.gif 
InBlock.gif                        // CanUserResizeColumns - 是否允许改变列的宽度。默认值 true 
InBlock.gif                        if (DataGrid1 != null
InBlock.gif                                DataGrid1.CanUserResizeColumns = (bool)chk.IsChecked; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void chkColSort_Changed(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CheckBox chk = sender as CheckBox; 
InBlock.gif 
InBlock.gif                        // CanUserSortColumns - 是否允许列的排序。默认值 true 
InBlock.gif                        if (DataGrid1 != null
InBlock.gif                                DataGrid1.CanUserSortColumns = (bool)chk.IsChecked; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void chkCustomGridLineVertical_Changed(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CheckBox chk = sender as CheckBox; 
InBlock.gif 
InBlock.gif                        if (DataGrid1 != null
InBlock.gif                        { 
InBlock.gif                                // VerticalGridLinesBrush - 改变表格的垂直分隔线的 Brush 
InBlock.gif                                if (chk.IsChecked == true
InBlock.gif                                        DataGrid1.VerticalGridLinesBrush = new SolidColorBrush(Colors.Blue); 
InBlock.gif                                else 
InBlock.gif                                        DataGrid1.VerticalGridLinesBrush = new SolidColorBrush(Color.FromArgb(255, 223, 227, 230)); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void chkCustomGridLineHorizontal_Changed(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CheckBox chk = sender as CheckBox; 
InBlock.gif 
InBlock.gif                        if (DataGrid1 != null
InBlock.gif                        { 
InBlock.gif                                // HorizontalGridLinesBrush - 改变表格的水平分隔线的 Brush 
InBlock.gif                                if (chk.IsChecked == true
InBlock.gif                                        DataGrid1.HorizontalGridLinesBrush = new SolidColorBrush(Colors.Blue); 
InBlock.gif                                else 
InBlock.gif                                        DataGrid1.HorizontalGridLinesBrush = new SolidColorBrush(Color.FromArgb(255, 223, 227, 230)); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void cboHeaders_SelectionChanged(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        ComboBoxItem cbi = ((ComboBox)sender).SelectedItem as ComboBoxItem; 
InBlock.gif 
InBlock.gif                        if (DataGrid1 != null
InBlock.gif                        { 
InBlock.gif                                // HeadersVisibility - 表头(包括列头和行头)的显示方式 [System.Windows.Controls.DataGridHeadersVisibility枚举] 
InBlock.gif                                //         DataGridHeadersVisibility.All - 列头和行头均显示 
InBlock.gif                                //         DataGridHeadersVisibility.Column - 只显示列头。默认值 
InBlock.gif                                //         DataGridHeadersVisibility.Row - 只显示行头 
InBlock.gif                                //         DataGridHeadersVisibility.None - 列头和行头均不显示 
InBlock.gif                                if (cbi.Tag.ToString() == "All"
InBlock.gif                                        DataGrid1.HeadersVisibility = DataGridHeadersVisibility.All; 
InBlock.gif                                else if (cbi.Tag.ToString() == "Column"
InBlock.gif                                        DataGrid1.HeadersVisibility = DataGridHeadersVisibility.Column; 
InBlock.gif                                else if (cbi.Tag.ToString() == "Row"
InBlock.gif                                        DataGrid1.HeadersVisibility = DataGridHeadersVisibility.Row; 
InBlock.gif                                else if (cbi.Tag.ToString() == "None"
InBlock.gif                                        DataGrid1.HeadersVisibility = DataGridHeadersVisibility.None; 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
3、ListBox.xaml
<UserControl x:Class="Silverlight20.Data.ListBox" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <StackPanel HorizontalAlignment="Left"> 

                <!-- 
                ListBox.ItemTemplate - ListBox 的选项模板 
                        DataTemplate - 手工定义 ListBox 的选项数据 
                --> 
                <ListBox x:Name="ListBox1" Margin="5" Width="200" Height="100"> 
                        <ListBox.ItemTemplate> 
                                <DataTemplate> 
                                        <StackPanel Orientation="Horizontal"> 
                                                <TextBlock Text="{Binding Name}" Margin="5" /> 
                                                <TextBlock Text="{Binding Age}" Margin="5" /> 
                                        </StackPanel> 
                                </DataTemplate> 
                        </ListBox.ItemTemplate> 
                </ListBox> 
                 
        </StackPanel> 
</UserControl>
 
ListBox.xaml.cs
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Net; 
InBlock.gifusing System.Windows; 
InBlock.gifusing System.Windows.Controls; 
InBlock.gifusing System.Windows.Documents; 
InBlock.gifusing System.Windows.Input; 
InBlock.gifusing System.Windows.Media; 
InBlock.gifusing System.Windows.Media.Animation; 
InBlock.gifusing System.Windows.Shapes; 
InBlock.gif 
InBlock.gifnamespace Silverlight20.Data 
InBlock.gif
InBlock.gif        public partial class ListBox : UserControl 
InBlock.gif        { 
InBlock.gif                public ListBox() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent();    
InBlock.gif                         
InBlock.gif                        BindData(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                void BindData() 
InBlock.gif                { 
InBlock.gif                        var source = new Data.SourceData(); 
InBlock.gif 
InBlock.gif                        // 设置 ListBox 的数据源 
InBlock.gif                        ListBox1.ItemsSource = source.GetData(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
OK
[源码下载]



     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/343107,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
技术小胖子
+ 关注