我正在尝试刷新通过更新数据更新的datagrid。但我无法看到结果。我从更新按钮调用filldatagrid方法但仍然无法在主页中看到更改。
Home.xaml
<DataGrid Margin="10,40,10,10" Name="grdEmployees" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Update">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Button_Click" Tag="{Binding Id}" Content="Update"
Padding="0,0,0,10"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn Header="Check" IsThreeState="True" Binding="{Binding Id}" />
</DataGrid.Columns>
</DataGrid>
Home.Xaml.cs
public partial class Home : UserControl
{
EmployeeContexts em = new EmployeeContexts();
public Home()
{
InitializeComponent();
FillDataGrid();
}
public void FillDataGrid()
{
var emp = em.swap.ToList();
this.DataContext = emp.AsEnumerable();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var code = button.Tag;
if (code != null)
{
var mem = em.swap.Find(code);
ModernDialog1 md = new ModernDialog1();
md.name.Text = mem.Name;
md.address.Text = mem.Addressa;
md.city.Text = mem.City;
md.hide.Text = (mem.Id).ToString();
md.Show();
}
else
{
ModernDialog.ShowMessage("Wrong:Input", "MY App", System.Windows.MessageBoxButton.OK);
}
}
}
ModernDialog1.xaml
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Label FontSize="20">Name</Label>
<TextBox Name="name" Grid.Column="1" Margin="10,10,11,15"></TextBox>
<Label FontSize="20" Grid.Row="1">Address</Label>
<TextBox Name="address" Grid.Column="1" Grid.Row="1" Margin="10,10,11,15"></TextBox>
<Label FontSize="20" Grid.Row="2">City</Label>
<TextBox Name="city" Grid.Column="1" Grid.Row="2" Margin="10,10,11,15"></TextBox>
<Button Foreground="Blue" Click="Button_Click" Grid.Row="3" Grid.Column="1" Margin="10,10,26,10">Update</Button>
<Button Foreground="Red" Click="Button1_Click" Grid.Row="3" Grid.Column="0" Margin="25,10,44,10">Close</Button>
<TextBlock Name="hide" Visibility="Collapsed"/>
</Grid>
ModernDialog1.xaml.cs
公共部分类ModernDialog1:ModernDialog
{
EmployeeContexts em = new EmployeeContexts();
public ModernDialog1()
{
InitializeComponent();
this.Buttons = new Button[] { null };
}
private void Button_Click(object sender, RoutedEventArgs e)
{
testofpage testofpage=new testofpage();
testofpage.Id = int.Parse(hide.Text);
testofpage.Name = name.Text;
testofpage.City = city.Text;
testofpage.Addressa = address.Text;
em.Entry(testofpage).State = EntityState.Modified;
em.SaveChanges();
Home hm = new Home();
hm.FillDataGrid();
this.Hide();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
this.Hide();
}
}
testofpage.cs
public class testofpage:INotifyPropertyChanged
{
public int Id { get; set; }
public override string ToString()
{
return string.Format("{0}n{1}n{2}", Name, Addressa, City);
}
private string name,address,city;
public string Name
{
get { return this.name; }
set
{
if (this.name != value)
{
this.name = value;
this.NotifyPropertyChanged("Name");
}
}
}
public string Addressa
{
get { return this.address; }
set
{
if (this.address != value)
{
this.address = value;
this.NotifyPropertyChanged("Addressa");
}
}
}
public string City
{
get { return this.city; }
set
{
if (this.city != value)
{
this.city = value;
this.NotifyPropertyChanged("City");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}