silverlight DataGrid 内嵌ComboBox 实现加载和保存

<Grid x:Name="LayoutRoot" Background="White" Height="322" Width="502">
        <sdk:DataGrid AutoGenerateColumns="False" Height="176" HorizontalAlignment="Left" Margin="60,57,0,0" Name="dg" VerticalAlignment="Top" Width="246" >
            <sdk:DataGrid.Columns>
                <sdk:DataGridTemplateColumn Width="60" >
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox x:Name="ck" Tag="{Binding Title}" ></CheckBox>
                            </StackPanel>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                     </sdk:DataGridTemplateColumn>
                        <sdk:DataGridTextColumn Header="Title " Binding="{Binding Title }"></sdk:DataGridTextColumn>
                <sdk:DataGridTextColumn Header="Memo" Binding="{Binding Memo }"></sdk:DataGridTextColumn>
                <sdk:DataGridTemplateColumn Header="城市">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="cityData"  Loaded="City_Loaded" Tag="{Binding Kind}" SelectionChanged="cityData_SelectionChanged" />  
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
        <Button Content="保存" Height="23" HorizontalAlignment="Left" Margin="60,11,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" Click="btnSave_Click" />
        <Button Content="加载新值" Height="23" HorizontalAlignment="Left" Margin="154,11,0,0" Name="btnLoadNew" VerticalAlignment="Top" Width="75" Click="btnLoadNew_Click" />
        <Button Content="删除" Height="23" HorizontalAlignment="Left" Margin="250,11,0,0" Name="btnDel" VerticalAlignment="Top" Width="75" Click="btnDel_Click" />
        <CheckBox Content="全选" Height="16" HorizontalAlignment="Left" Margin="60,62,0,0" Name="ckAll" VerticalAlignment="Top"  Click="ckAll_Click" />
    </Grid> 
 
  public partial class BB : UserControl
    {
        List<TestItem> list = new List<TestItem>();
        public List<City> listCity = new List<City>();
        List<TestItem> lstSave = new List<TestItem>();
        public BB()
        {
            InitializeComponent();
           
          
            list.Add(new TestItem() { Title = "第一行", Memo = "Hello", Kind = "0311", Count = 100 });
            list.Add(new TestItem() { Title = "第二行", Memo = "Hello", Kind = "0311", Count = 100 });
            list.Add(new TestItem() { Title = "第三行", Memo = "Hello", Kind = "", Count = 100 });
            listCity.Add(new City() { Name = "石家庄", ValueName = "0311" });
            listCity.Add(new City() { Name = "北京", ValueName = "011" });
            dg.ItemsSource = list;

        }
        private void City_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            comboBox.ItemsSource = listCity;
            comboBox.DisplayMemberPath = "Name";
            comboBox.SelectedItem = (from model in listCity
                                     where model.ValueName == comboBox.Tag.ToString()
                                     select model).FirstOrDefault();

        }
     
        private void cityData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
           
        }
    
        
        private void btnLoadNew_Click(object sender, RoutedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = lstSave;
        }
       
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            lstSave.Clear();
            foreach (object ovj in dg.ItemsSource)
            {
                TestItem newModel = ovj as TestItem;
                ComboBox comb = dg.Columns[3].GetCellContent(ovj).FindName("cityData") as ComboBox;
                lstSave.Add(new TestItem() { Title = "jpl", Memo = newModel.Memo, Kind = (comb.SelectedItem as City).ValueName });
                
            }
        }
      
        private void ckAll_Click(object sender, RoutedEventArgs e)
        {
            if (dg.ItemsSource != null)
            {
                if (this.ckAll.IsChecked.Value)//全选
                {
                    foreach (object ovj in dg.ItemsSource)
                    {
                        CheckBox cb1 = dg.Columns[0].GetCellContent(ovj).FindName("ck") as CheckBox; //cb为
                        cb1.IsChecked = true;
                    }
                }
                else//取消
                {
                    foreach (object obj in dg.ItemsSource)
                    {
                        CheckBox cb2 = dg.Columns[0].GetCellContent(obj).FindName("ck") as CheckBox;
                        cb2.IsChecked = false;
                    }
                }
            }

        }
        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            List<TestItem> delItem = new List<TestItem>();
            delItem.Clear();
            foreach (object ovj in dg.ItemsSource)
            {
                CheckBox cb1 = dg.Columns[0].GetCellContent(ovj).FindName("ck") as CheckBox; //cb为
                TestItem objSel = (from model in list
                                 where model.Title == cb1.Tag.ToString()
                                 select model).FirstOrDefault();
               if (cb1.IsChecked == true)
                {
                    delItem.Add(objSel);
                }
            }
            foreach (TestItem ovj in delItem)
            {
                list.Remove(ovj);
            }
            dg.ItemsSource = null;
            dg.ItemsSource = list;
        }
    }
    public class TestItem
    {
        public String Title { get; set; }
        public String Memo { get; set; }
        public String Kind { get; set; }
        public int Count { get; set; }
    }
    public class City
    {
        public string Name { get; set; }
        public string ValueName { get; set; }
    }

转载于:https://www.cnblogs.com/meimao5211/p/3465264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值