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>
<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 });
{
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;
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();
{
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;
{
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 });
}
{
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为
{
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;
}
}
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为
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);
}
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;
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 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; }
public class City
{
public string Name { get; set; }
public string ValueName { get; set; }
}