Silverlight数据上下文,datagrid

时间:2016-02-22 15:55:59

标签: c# xaml silverlight datagrid

我有数据网格视图,其中来自sql表的数据也是每行都有显示childwindow的按钮。在这个子窗口中有几个texbox,组合框和数据采集器,我需要从我的数据网格填充这个控件数据。我使用方法填充下面的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;}
 }

我的数据网格和插入数据的方法(工作正常)

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:

&#13;
&#13;
<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"  />
&#13;
&#13;
&#13;

我尝试按下按钮后发送datacontext我的类并插入到childwindow中的控件中,它不起作用

1 个答案:

答案 0 :(得分:0)

老兄,我建议你研究你的英语,它比我的更糟糕,这可以解释为什么人们不急于帮助你。

无论如何,我不确定我完全理解你的目标,仍然是这一行:

childWindow_update.DataContext = ((PIS)((Button)sender).DataContext).Id_Pis;

很奇怪。它清楚地表明你想要dataContext只是你的PIS对象的Id。这真的是你需要的吗?

更常规的是将完整对象作为DataContext(就像一个完整的PIS对象)。

希望它有所帮助,否则试着更明确地解释你的问题。

相关问题