Monday, April 29, 2013

Create Blank Data Grid According to Main Grid

Add Custom Control

public class BlankDataGrid : DataGrid
    {
        //Parent Data Grid
        private static DataGrid PdataGrid;
        //Blank Data Grid
        private static DataGrid BdataGrid;
        //how many row we will create
        private static int NoRow = 4;
        //if this value is 1 then no delete first recode,this is used for parent and child grid row color matching.
        private static int RemoveFirstRow;
        //Zero to null value converter into blank data grid.
        private static ZeroToEmptyConverter converter = new ZeroToEmptyConverter();
        //when paren data grid column hide then blank data grid column hide by this variable
        private static bool isParenDGColumnHide;
 
        //Constructor
        public BlankDataGrid()
        {
            try
            {
                this.Loaded += new RoutedEventHandler(BlankDataGrid_Loaded);
                this.LoadingRow += new System.EventHandler<DataGridRowEventArgs>(BlankDataGrid_LoadingRow);
            }
            catch (Exception)
            { throw; }
        }
 
        //when grid is loaded then it is called
        private void BlankDataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            //this.HeadersVisibility = DataGridHeadersVisibility.None;
            this.AutoGenerateColumns = false;
            this.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
            this.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
        }
 
        //when every row loaded then this method is called
        private void BlankDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            try
            {
                int dgridrow = e.Row.GetIndex();
                if (dgridrow == 0)
                    if (RemoveFirstRow == 1)
                        e.Row.Visibility = Visibility.Collapsed;
            }
            catch (Exception)
            { throw; }
        }
 
        #region Paren Data Grid Properties
 
        public DataGrid ParentDataGrid
        {
            get { return (DataGrid)GetValue(ParentDataGridProperty); }
            set { SetValue(ParentDataGridProperty, value); }
        }
 
        public static readonly DependencyProperty ParentDataGridProperty =
            DependencyProperty.Register("ParentDataGrid"typeof(DataGrid), typeof(BlankDataGrid),
            new PropertyMetadata(nullnew PropertyChangedCallback(OnParentDataGridChanged)));
 
        private static void OnParentDataGridChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                //clear parent grid temporary list
                PdataGrid = null;
                //get parent data grid
                PdataGrid = ((BlankDataGrid)d).ParentDataGrid as DataGrid;
                if (PdataGrid == nullreturn;
                if (PdataGrid.ItemsSource == nullreturn;
 
                //Parent Grid Sized Changed Event
                PdataGrid.SizeChanged += new SizeChangedEventHandler(dataGrid_SizeChanged);
                //Parent LayoutUpdated Event fire when parent grid column hide
                PdataGrid.LayoutUpdated += new EventHandler(PdataGrid_LayoutUpdated);
 
                //Get Parent Object Type
 
                Type t;
                if (PdataGrid.ItemsSource.GetType() == typeof(PagedCollectionView))
                {
                    var pageCollectionData = PdataGrid.ItemsSource as PagedCollectionView;
                    t = (pageCollectionData.SourceCollection as IEnumerable).AsQueryable().ElementType;
                }
                else
                {
                    t = (PdataGrid.ItemsSource as IEnumerable).AsQueryable().ElementType;
                }
                if (t == nullreturn;
                //Make Blank Data List
                dynamic instant = Activator.CreateInstance(t);//create instance
                var Datalist = new ObservableCollection<dynamic>();
                for (int i = 0; i < NoRow; i++)
                    Datalist.Add(instant);
 
                //add item source
                ((BlankDataGrid)d).ItemsSource = null;
                ((BlankDataGrid)d).ItemsSource = Datalist;
 
                //Clear data grid column
                ((BlankDataGrid)d).Columns.Clear();
                foreach (var columnName in PdataGrid.Columns)
                {
                    ((BlankDataGrid)d).Columns.Add(CreateTextColumn("id"));
                }
                //adjust blank grid according to parent grid.
                AdjustWidth(PdataGrid, ((BlankDataGrid)d));
 
                //Clear blank grid teporary
                BdataGrid = null;
                //set as a further use
                BdataGrid = ((BlankDataGrid)d);
            }
            catch (Exception)
            {
                throw;
            }
        }
 
        //create blank data grid column
        private static DataGridTextColumn CreateTextColumn(string fieldName)
        {
            DataGridTextColumn column = new DataGridTextColumn();
            column.Binding = new Binding(fieldName);
            column.Binding.Converter = converter;
            column.Binding.Mode = BindingMode.OneWay;
            return column;
        }
 
        //adjust blank grid according to parent grid.
        private static void AdjustWidth(DataGrid paren, DataGrid blank)
        {
            for (int i = 0; i < blank.Columns.Count; i++)
            {
                for (int j = 0; j < PdataGrid.Columns.Count; j++)
                {
                    if (i == j)
                    {
                        blank.Columns[i].Width = new DataGridLength(PdataGrid.Columns[j].ActualWidth);
                        blank.Columns[i].Visibility = PdataGrid.Columns[j].Visibility;
                        break;
                    }
                }//end of second for
            }//end of 1st for
        }
 
        //parent grid sized changed
        private static void dataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                if (sender == nullreturn;
                if (BdataGrid == nullreturn;
                isParenDGColumnHide = false;
                var pGrid = sender as DataGrid;
                AdjustWidth(pGrid, BdataGrid);
            }
            catch (Exception)
            { throw; }
        }
 
        //Parent Grid LayoutUpdated Event fire when parent grid column hide
        private static void PdataGrid_LayoutUpdated(object sender, EventArgs e)
        {
            if (isParenDGColumnHide)
            {
                if (BdataGrid == nullreturn;
                if (PdataGrid == nullreturn;
                AdjustWidth(PdataGrid, BdataGrid);
                isParenDGColumnHide = false;
            }
        }
 
        #endregion
 
        #region Properties
 
        public int NumberOfRow
        {
            get { return (int)GetValue(NumberOfRowProperty); }
            set { SetValue(NumberOfRowProperty, value); }
        }
        public static readonly DependencyProperty NumberOfRowProperty =
            DependencyProperty.Register("NumberOfRow"typeof(int), typeof(BlankDataGrid),
            new PropertyMetadata(0, new PropertyChangedCallback(NumberOfRowChanged)));
 
        private static void NumberOfRowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (d != null)
                {
                    NoRow = 0;
                    NoRow = ((BlankDataGrid)d).NumberOfRow;
                }
            }
            catch (Exception)
            { throw; }
        }
 
        public int isRemoveFirstRow
        {
            get { return (int)GetValue(isRemoveFirstRowProperty); }
            set { SetValue(isRemoveFirstRowProperty, value); }
        }
        public static readonly DependencyProperty isRemoveFirstRowProperty =
            DependencyProperty.Register("isRemoveFirstRow"typeof(int), typeof(BlankDataGrid),
            new PropertyMetadata(0, new PropertyChangedCallback(isRemoveFirstRowChanged)));
 
        private static void isRemoveFirstRowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (d != null)
                {
                    RemoveFirstRow = ((BlankDataGrid)d).isRemoveFirstRow;
                }
            }
            catch (Exception)
            { throw; }
        }
 
        public bool isParenDataGridColumnHide
        {
            get { return (bool)GetValue(isParenDataGridColumnHideProperty); }
            set { SetValue(isParenDataGridColumnHideProperty, value); }
        }
        public static readonly DependencyProperty isParenDataGridColumnHideProperty =
            DependencyProperty.Register("isParenDataGridColumnHide"typeof(bool), typeof(BlankDataGrid),
            new PropertyMetadata(falsenew PropertyChangedCallback(isMainGridHideChanged)));
 
        private static void isMainGridHideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (d != null)
                {
                    isParenDGColumnHide = ((BlankDataGrid)d).isParenDataGridColumnHide;
                }
            }
            catch (Exception)
            { throw; }
        }
 
        #endregion
    }

use of custom control in xaml
<lib:BlankDataGrid VerticalAlignment="Top" Grid.Row="1" Grid.Column="0" ParentDataGrid="{Binding ElementName=dgMasterGrid}"/>
                                                <sdk:DataGrid x:Name="dgMasterGrid" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top"/>

Thursday, April 11, 2013

Silverlight Tree View Control

See the link
http://social.msdn.microsoft.com/Forums/en-US/silverlightcontrols/thread/64c166f0-9110-456f-aee0-a33a296f161e

Check Network Connection in Silverlight

Copy and Paste the code below into your main page loading

--Constructor
this.Loaded += new RoutedEventHandler(LoginPage_Loaded);


 #region Get Network Status

        private void LoginPage_Loaded(object sender, RoutedEventArgs e)
        {
            GetNetworkStatus();
            NetworkChange.NetworkAddressChanged += new
                NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
        }

        private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            GetNetworkStatus();
        }

        private void GetNetworkStatus()
        {
            _dataContext.Online = NetworkInterface.GetIsNetworkAvailable();
            if (!_dataContext.Online)
            {
                //no internet connection
                _dataContext.Message = "Internet connection is not available";
            }
            else
            {
                //Load All user Data.
                _dataContext.LoadData();
            }
        }

        #endregion

Monday, April 8, 2013

Use Silverligth Validation Theme

Create new project and add textbox into your main page.Open your project in Blend,Or you can create your project in Blend.See the Following step.



Set validation into button press and Run your application.
Thanks.

Tuesday, April 2, 2013

How can i change WCF Service Binding

It is simple and easy way to change WCF service binding.Here i will discuss step by step following below.
At first open your WCF service project and Follow the image. i think image have to represent many think without say.Now below
 Figure 1
 Figure 2
Figure 3
I think it will be help you.

Monday, April 1, 2013

Drag Drop between datagrid cells


If you’ve been searching the web for a good sample of how to drag and drop data from one DataGrid to another in Silverlight, you’ve probably found some great samples out there, showing “how easy” it can be by just surrounding your.  In fact, if you do that, you can drag and drop data from one grid cell to the other.  The problem is that most of these demos are about the visual aspect of drag and drop.  But in the real world, there is more work to be done.  Most of the samples that I found did not deal with how to save the data once it was dropped in the other grid.
The Firsr XAML Code
<sdk:DataGrid Grid.Row="1" x:Name="dg" ItemsSource="{Binding Data}" AutoGenerateColumns="False" IsReadOnly="True" >
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="Id" Binding="{Binding Id}" />
                <sdk:DataGridTemplateColumn Header="FirstName" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" >
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text ="{Binding FirstName}"  
                                       MouseLeftButtonDown="TextBox_MouseLeftButtonDown" 
                                       MouseLeftButtonUp="TextBox_MouseLeftButtonUp"
                                       MouseMove="TextBlock_MouseMove"/>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" >
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding LastName}" MouseLeftButtonDown="TextBox_MouseLeftButtonDown" MouseLeftButtonUp="TextBox_MouseLeftButtonUp"/>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

The C# Code
private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            dragText = string.Empty;
            TextBlock tb = sender as TextBlock;
            dragTextBox = tb;
            dragText = tb.Text;
        }

        private void TextBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            dragTextBox.Text = tb.Text;
            tb.Text = dragText;
        }

        private void TextBlock_MouseMove(object sender, MouseEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            tb.Cursor = Cursors.Hand;
        }