[英]Silverlight Data Context, datagrid
我有datagrid視圖,其中sql表中的數據也在哪里,每行都有顯示子窗口的按鈕。 在此子窗口中,只有幾個texbox,combobox和datapicker,我需要從我的datagrid中填充此控件數據。 我用下面的方法來填充datagrid。
public ObservableCollection<MyClass> ReadUpdate(int id_update)
{
ObservableCollection<MyClass> result = new ObservableCollection<MyClass>();
string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlDataReader dr;
SqlConnection conn = new SqlConnection(nwConn);
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.CommandText = "Insert_Update";
cmd.Parameters.AddWithValue("@id_update", id_update);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
MyClass lin = new MyClass();
lin.id = dr.GetInt32(1);
if (!dr.IsDBNull(2)) lin.other = dr.GetString(2);
if (!dr.IsDBNull(3)) lin.barkod = dr.GetString(3);
if (!dr.IsDBNull(4)) lin.pw = dr.GetInt32(4);
result.Add(lin);
}
dr.Close();
return result;
}
catch (SqlException e)
{
MyClass lin = new MyClass();
lin.other = e.Message;
result.Add(lin);
return result;
}
finally
{
conn.Close();
};
}
我的課:
public class PIS
{
public int ID { get; set; }
public int PW { get; set;}
public string other { get; set;}
public string barkod { get; set;}
}
我的datagrid和插入數據的方法(正常)
private PIS pis_update;
public Home()
{
InitializeComponent();
webService = new ServiceReference1.Service1Client();
webService.ReadPismaCompleted += WebService_ReadPismaCompleted;
webService.ReadPismaAsync(0);
}
private void WebService_ReadPismaCompleted(object sender, ServiceReference1.ReadPismaCompletedEventArgs e)
{
if (e.Result != null)
{
dataGridPisma.ItemsSource = e.Result;
}
}
我的按鈕:
private void btnUpdate_Click(object sender, System.Windows.RoutedEventArgs e)
{
pismo_update = (PIS)((Button)sender).DataContext;
ChildWindow_Update childWindow_update = new ChildWindow_Update();
childWindow_update.DataContext = ((PIS)((Button)sender).DataContext).Id_Pis;
childWindow_update.Closed += ChildWindow_Update_Closed;
childWindow_update.Show();
}
private void ChildWindow_Update_Closed(object sender, EventArgs e)
{
if (((ChildWindow_Update)sender).DialogResult.Value)
{
webService.ReadPismaAsync(0);
}
}
我的xaml:
<TextBox x:Name="textBox1_other" Text="{Binding Path= other}"/> <TextBox x:Name="textBox2_barkod" Text="{Binding Path= barkod}"/> <ComboBox x:Name="comboBoxPW" HorizontalAlignment="Left" Margin="40,117,0,0" VerticalAlignment="Top" Width="366" SelectedItem="{Binding ReadComboboxPW}" DisplayMemberPath="PW" SelectedValuePath="IDPW" />
我嘗試在按鈕單擊后發送數據上下文我的課程並插入子窗口中的控件,但它不起作用
杜德(Dude),我建議您使用英語,它比我的英語還要糟糕,這可以解釋為什么人們不急於幫助您。
無論如何,我不確定我是否完全理解您的目標,仍然是這一行:
childWindow_update.DataContext = ((PIS)((Button)sender).DataContext).Id_Pis;
很奇怪。 它明確指出您只希望將PIS對象的ID作為dataContext。 真的是您需要的嗎?
更常規的做法是將完整的對象作為DataContext(例如完整的PIS對象)。
希望對您有所幫助,否則請嘗試更明確地解釋您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.