Zhanbo Sun [MS]

Realize .NET & Windows Vista Potential

November 2004 - Posts

  • Raise Routed Event through RaiseEvent

    Filed under: ,

    (Version: Avalon CTP 2004 November)

     

    Both UIElement and ContentElement implement IInputElement interface, which defines RaiseEvent method.  This post gives you an example of using it.

     

    We start with a simple Xaml file:

    <Window x:Class="AvalonApplication1.Window1"

        xmlns="http://schemas.microsoft.com/2003/xaml" xmlns:x="Definition"

        Text="AvalonApplication1">

        <DockPanel>

            <Button ID="btn1" Content="Button 1" Click="ButtonClick1"/>

            <Button ID="btn2" Content="Button 2" Click="ButtonClick2"/>

        </DockPanel>

    </Window>

     

    Two event handlers are defined as follows:

    private void ButtonClick1(object sender, RoutedEventArgs args)

    {

      //Do Nothing

    }

     

    private void ButtonClick2(object sender, RoutedEventArgs args)

    {

      MessageBox.Show("Button2 Just Received Click Event");

    }

     

    So when you click on btn2, you see a MessageBox. But when you click on btn1, you do not see anything in particular.

     

    Now we are going to add code into ButtonClick1 so that it will raise Click event on btn2. As a result, the same MessageBox shows up when you click on btn1. The code below makes use of RaiseEvent:

     

    private void ButtonClick1(object sender, RoutedEventArgs args)

    {

      RoutedEventArgs evtArgs = new RoutedEventArgs();

      evtArgs.SetRoutedEventID(Button.ClickEventID);

      btn2.RaiseEvent(evtArgs);

    }

     

    Note we do not call ButtonClick2 directly. Instead, we raise the Click event on btn2. ButtonClick2 is called to handle that event.

     

    I end this post with a question for my reader: What will happen if we change ButtonClick1 code again? Are you going to see a MessageBox, a ContextMenu , an Exception, or nothing at all?

     

    private void ButtonClick1(object sender, RoutedEventArgs args)

    {

      RoutedEventArgs evtArgs = new RoutedEventArgs();

      evtArgs.SetRoutedEventID(ContextMenu.OpenedEventID);

      btn2.RaiseEvent(evtArgs);

    }

     

    (This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)

    PostTypeIcon
    6,129 Views
  • Show me the Info of Visual Tree

    Filed under:

    (Version: Avalon CTP 2004 November)

     

    What does mainWindow‘s visual tree look like when a simple Xaml file (below) is rendered onto the screen?

     

    <Window x:Class="AvalonApplication1.Window1" xmlns="http://schemas.microsoft.com/2003/xaml" xmlns:x="Definition" Text="AvalonApplication1" ID="mainWindow">

        <Grid>

            <Button Content="Click Me" Click="ButtonClick"/>

        </Grid>

    </Window>

     

    Well, it depends on the style definition for Window, Grid and Button controls.  With the default Windows XP theme, the following printout shows the VisualTree structure for mainWindow.

     

    AvalonApplication1.Window1

        System.Windows.Controls.Border

            System.Windows.Controls.Grid

                System.Windows.Documents.AdornerDecorator

                    System.Windows.Controls.ContentPresenter

                        System.Windows.Controls.Grid

                            System.Windows.Controls.Button

                                System.Windows.Controls.Grid

                                    System.Windows.Shapes.Rectangle

                                    System.Windows.Shapes.Rectangle

                                    System.Windows.Shapes.Rectangle

                                    System.Windows.Shapes.Rectangle

                                    System.Windows.Shapes.Rectangle

                                    System.Windows.Shapes.Rectangle

                                    System.Windows.Shapes.Rectangle

                                    System.Windows.Shapes.Rectangle

                                    System.Windows.Controls.Border

                                        System.Windows.Controls.FlowPanel

                                            System.Windows.Controls.ContentPresenter

                                                System.Windows.Controls.Text

                                    System.Windows.Controls.Grid + GridLinesRenderer

                            System.Windows.Controls.Grid + GridLinesRenderer

                    System.Windows.Documents.AdornerLayer

                System.Windows.Controls.Primitives.StatusBar

                System.Windows.Controls.Primitives.ResizeGrip

                System.Windows.Controls.Grid+GridLinesRenderer

     

    The following code is used to generate the information.

     

    public static class Utility

    {

        public static string GetVisualTreeInfo(Visual element)

        {

            if (element == null)

            {

                throw new ArgumentNullException("element");

            }

            StringBuilder sb1 = new StringBuilder();

            VisualCollection vc = VisualOperations.GetChildren(element);

            if (vc.Count == 0)

            {

                sb1.Append("[No Visual Child] - ");

                sb1.Append(element.GetType());

            }

            else

            {

                sb1.Append(element.GetType());

                sb1.Append(Environment.NewLine);

                WriteToVisualTreeInfo(sb1, vc, 1);

            }

            return sb1.ToString();

        }

     

        private static void WriteToVisualTreeInfo(StringBuilder sb, VisualCollection vc, int level)

        {

            const int indent = 4;

            foreach (Visual v in vc)

            {

                sb.Append(new string(' ', level * indent));

                sb.Append(v.GetType());

                sb.Append(Environment.NewLine);

                VisualCollection vcollection = VisualOperations.GetChildren(v);

                if (vcollection.Count > 0)

                {

                    WriteToVisualTreeInfo(sb, vcollection, level + 1);

                }

            }

        }

    }

     

    I end this post with a question for my reader: what does MessageBox display when the Button is clicked and its event handler is this?

     

    private void ButtonClick(object sender, RoutedEventArgs e)

    {

        Button newButton = new Button();

        MessageBox.Show(Utility.GetVisualTreeInfo(newButton));

    }

     

    (This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)

    PostTypeIcon
    11,124 Views
  • define:Avalon

    Filed under:

    Many years ago when Microsoft.NET is released, the first question people ask is “What is .NET”? Well still different people have different answers, but hopefully you have one!

     

     Here comes Avalon. What is it? I like the explanation in the autorun.htm of November CTP (Community Technology Preview) CD. Here it is:

     

    "Avalon" is the code name for Microsoft’s unified presentation subsystem for Windows, consisting of a display engine and a managed-code framework. It unifies how Windows creates, displays, and manipulates documents, media, and user interface, and enables developers and designers to create visually-stunning, differentiated user experiences.

     

    This blog was created for Avalon and will grow as Avalon developers do.

     

    (This posting is provided "AS IS" with no warranties, and confers no rights.)

    PostTypeIcon
    5,031 Views