/// <summary>
        /// Emulates a keydown event, as if "key" were pressed when the "target" control has focus.
        /// </summary>
        /// <param name="target">Control to send keyDown event to.</param>
        /// <param name="key">Key press to emulate in KeyDown event.</param>
        private void SendKeyDownEvent(Control target, Key key)
        {
            var routedEvent = Keyboard.KeyDownEvent; // Event to send

            target.RaiseEvent(
               new KeyEventArgs(
                 Keyboard.PrimaryDevice,
                 PresentationSource.FromVisual(target),
                 0,
                 key) { RoutedEvent = routedEvent }
             );
        }
        public void TemplateApplyTest()
        {
            string text = @"
            <ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='root'>
                <FrameworkElement x:Name='child' Height='400'/>
                <ControlTemplate.Triggers>
                    <Trigger Property='FrameworkElement.Width' Value='100'>
                        <Setter Property='FrameworkElement.Height' Value='100'/>
                    </Trigger>
                    <Trigger Property='FrameworkElement.Width' Value='200'>
                        <Setter Property='FrameworkElement.Height' Value='200'/>
                        <Setter TargetName='child' Property='FrameworkElement.Height' Value='200'/>
                    </Trigger>
                    <EventTrigger RoutedEvent='FrameworkElement.Initialized'>
                        <Setter Property='FrameworkElement.Height' Value='300'/>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>";

            ControlTemplate controlTemplate = XamlLoader.Load(XamlParser.Parse(text)) as ControlTemplate;

            Control control = new Control();
            control.Width = 100;
            control.Template = controlTemplate;

            control.ApplyTemplate();

            Assert.AreEqual(1, control.VisualChildren.Count());

            FrameworkElement child1 = control.TemplateChild as FrameworkElement;
            Assert.IsNotNull(child1);

            FrameworkElement child2 = control.Template.FindName("child", control) as FrameworkElement;
            Assert.AreEqual(child1, child2);

            Assert.AreEqual(control, child1.TemplatedParent);
            Assert.AreEqual(400, child1.Height);
            Assert.AreEqual(BaseValueSource.ParentTemplate, child1.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource);
            Assert.AreEqual(BaseValueSource.ParentTemplate, child1.GetBaseValueSource(FrameworkElement.HeightProperty));

            Assert.AreEqual(100, control.Height);
            Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource);
            Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetBaseValueSource(FrameworkElement.HeightProperty));

            control.Width = 200;
            Assert.AreEqual(200, control.Height);
            Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource);
            Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetBaseValueSource(FrameworkElement.HeightProperty));

            Assert.AreEqual(200, child1.Height);
            Assert.AreEqual(BaseValueSource.ParentTemplateTrigger, child1.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource);
            Assert.AreEqual(BaseValueSource.ParentTemplateTrigger, child1.GetBaseValueSource(FrameworkElement.HeightProperty));

            control.RaiseEvent(new RoutedEventArgs(FrameworkElement.InitializedEvent, control));
            Assert.AreEqual(300, control.Height);
            Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetValueSource(FrameworkElement.HeightProperty).BaseValueSource);
            Assert.AreEqual(BaseValueSource.TemplateTrigger, control.GetBaseValueSource(FrameworkElement.HeightProperty));

            control.Template = null;
            control.ApplyTemplate();

            Assert.AreEqual(Double.NaN, control.Height);
        }
Example #3
0
        public void StyleEventTriggerTest()
        {
            string text = @"
            <Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:test='clr-namespace:Granular.Presentation.Tests;assembly=Granular.Presentation.Tests'>
                <Setter Property='FrameworkElement.Width' Value='100'/>
                <EventSetter Event='FrameworkElement.Initialized' Handler='InitializedHandler'/>
                <Style.Triggers>
                    <EventTrigger RoutedEvent='FrameworkElement.Initialized'>
                        <Setter Property='FrameworkElement.Width' Value='200'/>
                    </EventTrigger>
                </Style.Triggers>
            </Style>";

            TestStyle style = new TestStyle();
            XamlLoader.Load(style, XamlParser.Parse(text));

            Control control = new Control();

            control.Style = style;
            Assert.AreEqual(100, control.Width);
            Assert.AreEqual(BaseValueSource.Style, control.GetValueSource(FrameworkElement.WidthProperty).BaseValueSource);
            Assert.AreEqual(0, style.InitializedHandlerCallsCount);

            control.RaiseEvent(new RoutedEventArgs(FrameworkElement.InitializedEvent, control));
            Assert.AreEqual(200, control.Width);
            Assert.AreEqual(BaseValueSource.StyleTrigger, control.GetValueSource(FrameworkElement.WidthProperty).BaseValueSource);
            Assert.AreEqual(1, style.InitializedHandlerCallsCount);

            control.Style = null;
            Assert.AreEqual(Double.NaN, control.Width);
            Assert.AreEqual(BaseValueSource.Default, control.GetValueSource(FrameworkElement.WidthProperty).BaseValueSource);

            control.RaiseEvent(new RoutedEventArgs(FrameworkElement.InitializedEvent, control));
            Assert.AreEqual(Double.NaN, control.Width);
            Assert.AreEqual(BaseValueSource.Default, control.GetValueSource(FrameworkElement.WidthProperty).BaseValueSource);
            Assert.AreEqual(1, style.InitializedHandlerCallsCount);
        }