Beispiel #1
0
        public void ResourceInheritanceTest()
        {
            object value;

            FrameworkElement root = new FrameworkElement();

            root.Resources = new ResourceDictionary();
            root.Resources.Add("key1", "value1");

            Assert.IsTrue(root.TryGetResource("key1", out value));
            Assert.AreEqual("value1", value);

            FrameworkElement child1 = new FrameworkElement();

            root.AddVisualChild(child1);
            root.AddLogicalChild(child1);

            Assert.IsTrue(child1.TryGetResource("key1", out value));
            Assert.AreEqual("value1", value);

            child1.Resources = new ResourceDictionary();
            child1.Resources.Add("key1", "value2");

            FrameworkElement child2 = new FrameworkElement();

            child1.AddVisualChild(child2);

            Assert.IsTrue(child2.TryGetResource("key1", out value));
            Assert.AreEqual("value2", value);

            root.AddLogicalChild(child2);

            Assert.IsTrue(child2.TryGetResource("key1", out value));
            Assert.AreEqual("value1", value);
        }
Beispiel #2
0
        public void ContainingNameScopeTest()
        {
            FrameworkElement logicalParent = new FrameworkElement();
            FrameworkElement visualParent  = new FrameworkElement();
            FrameworkElement child         = new FrameworkElement();

            NameScope logicalNameScope = new NameScope();

            NameScope.SetNameScope(logicalParent, logicalNameScope);

            NameScope visualNameScope = new NameScope();

            NameScope.SetNameScope(visualParent, visualNameScope);

            Freezable value    = new Freezable();
            Freezable subValue = new Freezable();

            // visual / context tree: logicalParent -> visualParent -> child -> value -> subValue
            logicalParent.AddVisualChild(visualParent);
            visualParent.AddVisualChild(child);
            child.SetValue(ValueProperty, value);
            value.SetValue(ValueProperty, subValue);

            // logical tree: logicalParent -> child
            logicalParent.AddLogicalChild(child);

            Assert.AreEqual(logicalNameScope, NameScope.GetContainingNameScope(subValue));
        }
        public void FindAncestorSourceObserverTest()
        {
            FrameworkElement parent = new FrameworkElement();
            FrameworkElement child  = new FrameworkElement();

            parent.AddVisualChild(child);

            Freezable value = new Freezable();

            FindAncestorSourceObserver observer1 = new FindAncestorSourceObserver(value, typeof(FrameworkElement), 0);
            FindAncestorSourceObserver observer2 = new FindAncestorSourceObserver(value, typeof(FrameworkElement), 2);
            FindAncestorSourceObserver observer3 = new FindAncestorSourceObserver(value, null, 2);

            Assert.AreEqual(ObservableValue.UnsetValue, observer1.Value);
            Assert.AreEqual(ObservableValue.UnsetValue, observer2.Value);
            Assert.AreEqual(ObservableValue.UnsetValue, observer3.Value);

            child.SetValue(ValueProperty, value);
            Assert.AreEqual(child, observer1.Value);
            Assert.AreEqual(parent, observer2.Value);
            Assert.AreEqual(parent, observer3.Value);

            child.SetValue(ValueProperty, null);
            Assert.AreEqual(ObservableValue.UnsetValue, observer1.Value);
            Assert.AreEqual(ObservableValue.UnsetValue, observer2.Value);
            Assert.AreEqual(ObservableValue.UnsetValue, observer3.Value);
        }
Beispiel #4
0
        public void AddTab(TabItem tabItem)
        {
            _headerContainer.AddVisualChild(tabItem._headerElement);

            AddVisualChild(tabItem._bodyElement);

            tabItem._headerElement._root.Click(() => { ActivateTab(tabItem); });
        }
Beispiel #5
0
 void AfterAddChildElement(FrameworkElement element)
 {
     if (LogicalChilderenCount == 1)
     {
         Left.AddVisualChild(element);
     }
     else
     {
         Right.AddVisualChild(element);
     }
 }
Beispiel #6
0
        public void ResourceReferenceExpressionBaseTest()
        {
            FrameworkElement root   = new FrameworkElement();
            FrameworkElement child1 = new FrameworkElement();
            FrameworkElement child2 = new FrameworkElement();

            child1.AddVisualChild(child2);


            int testValueChanged = 0;

            child2.PropertyChanged += (sender, e) => testValueChanged += e.Property == TestValueProperty ? 1 : 0;

            child2.SetValue(TestValueProperty, new ResourceReferenceExpressionProvider("key1"));

            Assert.AreEqual("default", child2.GetValue(TestValueProperty));
            Assert.AreEqual(0, testValueChanged);

            root.Resources = new ResourceDictionary();
            root.Resources.Add("key1", "value1");
            root.AddVisualChild(child1);

            Assert.AreEqual("value1", child2.GetValue(TestValueProperty));
            Assert.AreEqual(1, testValueChanged);

            root.Resources.Remove("key1");

            Assert.AreEqual("default", child2.GetValue(TestValueProperty));
            Assert.AreEqual(2, testValueChanged);

            ResourceDictionary dictionary = new ResourceDictionary();

            dictionary.Add("key1", "value2");

            root.Resources.MergedDictionaries.Add(dictionary);

            Assert.AreEqual("value2", child2.GetValue(TestValueProperty));
            Assert.AreEqual(3, testValueChanged);

            child1.Resources = new ResourceDictionary();
            child1.Resources.Add("key1", "value3");

            Assert.AreEqual("value3", child2.GetValue(TestValueProperty));
            Assert.AreEqual(4, testValueChanged);

            child1.Resources.Remove("key1");

            Assert.AreEqual("value2", child2.GetValue(TestValueProperty));
            Assert.AreEqual(5, testValueChanged);
        }
        public void ContextElementParentTest()
        {
            FrameworkElement root  = new FrameworkElement();
            FrameworkElement child = new FrameworkElement();

            Freezable value    = new Freezable();
            Freezable subValue = new Freezable();

            value.SetValue(ValueProperty, subValue);
            child.SetValue(ValueProperty, value);
            root.AddVisualChild(child);

            Assert.AreEqual(value, ((IContextElement)subValue).ContextParent);
            Assert.AreEqual(child, ((IContextElement)value).ContextParent);
            Assert.AreEqual(root, ((IContextElement)child).ContextParent);
        }
        internal static void BuildControlTemplate(Template xmlTemplate, FrameworkElement control)
        {
            var builder = new UIBuilder
            {
                _rootNode           = xmlTemplate.Root,
                DataContext         = control,
                Caller              = control,
                _isBuildingTemplate = true
            };

            var subControl = builder.BuildNode(builder._rootNode, control);

            var subControlAsFrameworkElement = subControl as FrameworkElement;

            if (subControlAsFrameworkElement == null)
            {
                throw new InvalidOperationException("TemplateControlFirstItemMustBeHTMLElement");
            }

            control._root = subControlAsFrameworkElement._root;
            control.AddVisualChild(subControlAsFrameworkElement);
        }