使用实体框架访问代码中的StackPanel

时间:2012-03-30 14:59:14

标签: silverlight silverlight-4.0 datagrid

填充数据网格时。当他们点击datagrid行时,RowDetailsS​​ection就会打开它。但是当我点击保存按钮时,我无法访问DatePicker,Checkbox和TextBox,以进行更新。

感谢

using System.Windows.Controls;
using BusinessApplication1.Web.Services;
using BusinessApplication1.Web;
using System.ServiceModel.DomainServices.Client;
using System.Linq;
using System.Windows;
using System;

namespace BusinessApplication1.Views.UserControls
{
    public partial class ucPersonelTakip : UserControl
    {
        DomainServiceDB db = new DomainServiceDB();
     //   LoadOperation<hr_DayScore> dayscoreLoadOp;
       readonly Error err = new Error("Kaydederken hata oluştu. Lütfen tekrar deneyiniz.");

        public ucPersonelTakip()
        {
            InitializeComponent();

            Loaded += Ayarlar_Loaded;
        }

        void Ayarlar_Loaded(object sender, RoutedEventArgs e)
        {          
            dgPersonelTakip.ItemsSource = db.hr_Employees;
            db.Load(db.GetActiveEmployeeQuery());
        }

        private void btnKaydet_Click(object sender, RoutedEventArgs e)
        {
            try
            {             
                hr_Employee em = (hr_Employee)dgPersonelTakip.SelectedItem;
                StackPanel sp = ((Button)sender).Parent as StackPanel;
                Label lbl = sp.Children[6] as Label;

                EntityQuery<hr_Employee> query = from emp2 in db.GetHr_EmployeeQuery()
                                                 where emp2.EmployeeId == em.EmployeeId
                                                 select emp2;

                LoadOperation<hr_Employee> loadOp = db.Load(query,  new Action<LoadOperation<hr_Employee>>(SetFired), true);

                lbl.Content = "Kayıt edildi.";
            }
            catch (Exception ex)
            {             
                MessageBox.Show(ex.Message);
            }            
        }

        private void SetFired(LoadOperation<hr_Employee> args)
        {
            bool i;

            StackPanel sp = new StackPanel();
            DatePicker dp = sp.Children[1] as DatePicker;
            CheckBox cb = sp.Children[2] as CheckBox;
            TextBox txt = sp.Children[4] as TextBox;

            if (cb.IsChecked == true)
                i = true;
            else
                i = false;

            hr_Employee emp = args.Entities.First();
            emp.StateId = 1;
            emp.LastDayOfWork = dp.SelectedDate;
            emp.ReasonOfFire = txt.Text;
            emp.TakeDocument = i;

            db.SubmitChanges();
        }

        private void dgPersonelTakip_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
        {
            DatePicker dp = e.DetailsElement.FindName("dtTarih") as DatePicker;
            dp.SelectedDate = DateTime.Today;
        }

    }
}

XAML;

<UserControl x:Class="BusinessApplication1.Views.UserControls.ucPersonelTakip"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:dataForm="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="456" d:DesignWidth="850" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices">

    <Grid x:Name="LayoutRoot" Background="White" Height="Auto" Width="Auto" HorizontalAlignment="Left" Margin="10,10,10,10">        

        <sdk:DataGrid x:Name="dgPersonelTakip" IsReadOnly="True" AutoGenerateColumns="False" Height="Auto" Width="Auto" Margin="10,0,10,10" RowDetailsVisibilityChanged="dgPersonelTakip_RowDetailsVisibilityChanged">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Visibility="Collapsed"  Header="Id" Binding="{Binding EmployeeId}" />
                <sdk:DataGridTextColumn Header="Adı" Width="Auto" Binding="{Binding Name}" />
                <sdk:DataGridTextColumn Header="Soyadı" Width="750" Binding="{Binding Surname}" />                
            </sdk:DataGrid.Columns>
            <sdk:DataGrid.RowDetailsTemplate>
                <!-- Begin row details section. -->
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="1" Background="WhiteSmoke">
                        <StackPanel Orientation="Horizontal">                                 
                            <TextBlock Text="Son İş Günü : " VerticalAlignment="Center" Margin="10,10,0,10"/>
                            <sdk:DatePicker Name="dtTarih" VerticalAlignment="Center" Margin="10,10,10,10" SelectedDateFormat="Short" />                                  
                            <CheckBox Name="cbCikisEvraklari" Content="Çıkış evrakları alındı mı?" Margin="10,26,10,10" />
                            <TextBlock Text="Çıkış Sebebi : " VerticalAlignment="Center" Margin="10,10,0,10"/>
                            <TextBox Name="txtCikisSebebi" Width="150" Height="50" Margin="10,10,10,10" AcceptsReturn="True"/>
                            <Button Name="btnKaydet" Content="Kaydet" VerticalAlignment="Center" Click="btnKaydet_Click" Margin="10,10,10,10"/>
                            <sdk:Label Name="lblSonuc" Content="" Margin="10,10,10,10" Foreground="Red" />                     
                        </StackPanel>
                    </Border>
                </DataTemplate>
                <!-- End row details section. -->
            </sdk:DataGrid.RowDetailsTemplate>
        </sdk:DataGrid>

0 个答案:

没有答案