ItemClick event on Silverlight ItemsControl

Published 16.03.2009 05:21 PM by Admin in C# | Silverlight

Recently I have been playing with Rik Robinson’s project called Silverlight 2 Scroller. You can read more about this control on this address :

http://www.wintellect.com/CS/blogs/rrobinson/archive/2009/01/22/yngwie-malmsteen-syndrome-silverlight-2-scroller-revisited.aspx

After looking at ItemsControl I have realize that you probobly want to implement click event when user click on your item in ItemsControl.ItemsControl is widely used in Silverlight.I have little modify project above to implement that click event.

Proccess is simple.I have created delegate that reference to method witch have ItemControlEventArgs parameter.I have also created ItemControlEventArgs class that inherits from EventArgs class and inside this class I have add property that represents Item data that is shown inside my  user control.

 public delegate void ItemClickDelegate(ItemControlEventArgs e);

        public class ItemControlEventArgs:EventArgs

        {

            public Employee ItemEmployee{get;set;}   

        }

        public event ItemClickDelegate ItemClicked;

One problem is that PresentationFrameworkCollection class does not have string indexer implemented, so we can’t write something like this :

(sender as Grid).Children["NameOfControl"]

If you have your own user control that use ItemsControl you probably have grid or stackPanel as your container control.I have implement on Grid_MouseLeftButtonDown event some code that iterate trough collection controls and find controls with particular names.

        private void Grid_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)

        {

            //clicked Employee

            Employee emp = new Employee();

            //iterate trough collection of children controls in grid to get emp info

            foreach (UIElement element in

            (sender as Grid).Children)

            {

                if (element != null)

                {

                    if (element.GetType() == typeof(System.Windows.Controls.StackPanel))

                    {

                        foreach (UIElement childrenElement in (element as StackPanel).Children)

                        {

                            Type childType = childrenElement.GetType();

                            if (childType == typeof(System.Windows.Controls.TextBlock))

                            {

                                if ((childrenElement as TextBlock).Name == "FullName")

                                {

                                    emp.FullName = (childrenElement as TextBlock).Text;

                                }

                                else if ((childrenElement as TextBlock).Name == "Title")

                                {

                                    emp.Title = (childrenElement as TextBlock).Text;

                                }

                                else if ((childrenElement as TextBlock).Name == "Age")

                                {

                                    emp.Age = Convert.ToInt32((childrenElement as TextBlock).Text);

                                }

                            }                 

                        }

                    }

 

                    //image property we don't use for anything

                        emp.ImagePath = "";

                }

 

            }

            ItemControlEventArgs args=new ItemControlEventArgs();

            args.ItemEmployee=emp;

            ItemClicked(args);

        }

After that on page where I use my user control I have event ItemClicked

 SilverlightScroller:ScrollerControl x:Name="theScrollerControl" 

            Margin="0,50,0,0" HorizontalAlignment="Center" VerticalAlignment="Top"

            ScrollTime="250" Width="940" Height="620" ItemClicked="item_Clicked"

and on codebehind I can get my itemData:

 private void item_Clicked(ScrollerControl.ItemControlEventArgs e)

        {

            MessageBox.Show(

            e.ItemEmployee.FullName + " " + e.ItemEmployee.Title + " " +

            e.ItemEmployee.Age.ToString() + " " + e.ItemEmployee.ImagePath);

        }

You can look picture bellow.

Proba1

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Recently I have been playing with silverlight and I came into a situation where MouseLeftButtonDown event is not triggered every time.It is only triggered when I click on child control inside container.

One way to fix this is to set background color of container to something.In my example I set my background color of grid control to transparent.

<Grid Height="100" Width="Auto" MouseLeftButtonDown="Grid_MouseLeftButtonDown" Background="Transparent">

Now if you click anywhere inside grid control MouseLeftButtonDown event is triggered.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

SqlException class does'nt have public constructor

Published 08.03.2009 07:43 AM by Admin in C# | Sql | Testing
Tags: , ,

I am currently preparing ado.net 3.5 course so I try to throw SqlException in one of my examples and realize that SqlException class is sealed and has no public constructor.

I have found solution on Rido blog http://blogs.msdn.com/rido how to create SqlException using reflection.

        public static SqlException CreateSqlException(string errorMessage, int errorNumber)

        {

 

            SqlErrorCollection collection = GetErrorCollection();

            SqlError error = GetError(errorNumber, errorMessage);

            MethodInfo addMethod = collection.GetType().

            GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance);

            addMethod.Invoke(collection, new object[] { error });

            Type[] types = new Type[] { typeof(string), typeof(SqlErrorCollection) };

            object[] parameters = new object[] { errorMessage, collection };

            ConstructorInfo constructor = typeof(SqlException).

            GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);

            SqlException exception = (SqlException)constructor.Invoke(parameters);

            return exception;

        }

        private static SqlError GetError(int errorCode, string message)

        {

            object[] parameters = new object[] {

            errorCode, (byte)0, (byte)10, "server", message, "procedure", 0 };

            Type[] types = new Type[] {

            typeof(int), typeof(byte), typeof(byte), typeof(string), typeof(string),

            typeof(string), typeof(int) };

            ConstructorInfo constructor = typeof(SqlError).

             GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);

            SqlError error = (SqlError)constructor.Invoke(parameters);

            return error;

        }

 

        private static SqlErrorCollection GetErrorCollection()

        {

            ConstructorInfo constructor = typeof(SqlErrorCollection).

            GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { }, null);

            SqlErrorCollection collection = (SqlErrorCollection)constructor.Invoke(new object[] { });

            return collection;

        }

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Radenko Zec Blog

Silverlight, C#, WCF, WPF,NET...