private void InitializeComponent() { AvaloniaRuntimeXamlLoader.Load(@" <Window xmlns='https://github.com/avaloniaui'> <Border/> </Window>", null, this); }
public void InfersDataTemplateTypeFromDataTypeProperty() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests' x:DataType='local:TestDataContext'> <Window.DataTemplates> <DataTemplate DataType='{x:Type x:String}'> <TextBlock Text='{CompiledBinding}' Name='textBlock' /> </DataTemplate> </Window.DataTemplates> <ContentControl Name='target' Content='{CompiledBinding StringProperty}' /> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var target = window.FindControl <ContentControl>("target"); var dataContext = new TestDataContext(); dataContext.StringProperty = "Initial Value"; window.DataContext = dataContext; window.ApplyTemplate(); target.ApplyTemplate(); ((ContentPresenter)target.Presenter).UpdateChild(); Assert.Equal(dataContext.StringProperty, ((TextBlock)target.Presenter.Child).Text); } }
public void ResolvesElementNameBindingFromLongForm() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests' x:DataType='local:TestDataContext'> <StackPanel> <TextBlock Text='{CompiledBinding StringProperty}' x:Name='text' /> <TextBlock Text='{CompiledBinding Text, ElementName=text}' x:Name='text2' /> </StackPanel> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var textBlock = window.FindControl <TextBlock>("text2"); var dataContext = new TestDataContext { StringProperty = "foobar" }; window.DataContext = dataContext; Assert.Equal(dataContext.StringProperty, textBlock.Text); } }
public void StaticResource_Can_Be_Assigned_To_Binding_Converter_In_DataTemplate() { using (StyledWindow()) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'> <Window.Resources> <local:TestValueConverter x:Key='converter' Append='bar'/> <DataTemplate x:Key='PurpleData'> <TextBlock Name='textBlock' Text='{Binding Converter={StaticResource converter}}'/> </DataTemplate> </Window.Resources> <ContentPresenter Name='presenter' Content='foo' ContentTemplate='{StaticResource PurpleData}'/> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); window.DataContext = "foo"; var presenter = window.FindControl <ContentPresenter>("presenter"); window.Show(); var textBlock = (TextBlock)presenter.GetVisualChildren().Single(); Assert.NotNull(textBlock); Assert.Equal("foobar", textBlock.Text); } }
public void ResolvesObservableIndexerBindingCorrectly() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests' x:DataType='local:TestDataContext'> <TextBlock Text='{CompiledBinding ObservableCollectionProperty[3]}' Name='textBlock' /> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var textBlock = window.FindControl <TextBlock>("textBlock"); var dataContext = new TestDataContext { ObservableCollectionProperty = { "A", "B", "C", "D", "E" } }; window.DataContext = dataContext; Assert.Equal(dataContext.ObservableCollectionProperty[3], textBlock.Text); dataContext.ObservableCollectionProperty[3] = "New Value"; Assert.Equal(dataContext.ObservableCollectionProperty[3], textBlock.Text); } }
public void StaticResource_From_MergedDictionary_In_Style_Can_Be_Assigned_To_Property() { var xaml = @" <UserControl xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <UserControl.Styles> <Style> <Style.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary> <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Style.Resources> </Style> </UserControl.Styles> <Border Name='border' Background='{StaticResource brush}'/> </UserControl>"; var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); var border = userControl.FindControl <Border>("border"); var brush = (SolidColorBrush)border.Background; Assert.Equal(0xff506070, brush.Color.ToUint32()); }
public void StaticResource_From_Style_Can_Be_Assigned_To_Setter() { using (StyledWindow()) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Styles> <Style> <Style.Resources> <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> </Style.Resources> </Style> <Style Selector='Button'> <Setter Property='Background' Value='{StaticResource brush}'/> </Style> </Window.Styles> <Button Name='button'/> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var button = window.FindControl <Button>("button"); var brush = (SolidColorBrush)button.Background; Assert.Equal(0xff506070, brush.Color.ToUint32()); } }
public void Style_Can_Use_Pseudolass_Selector_With_Dash() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Styles> <Style Selector='Border:foo-bar'> <Setter Property='Background' Value='Red'/> </Style> </Window.Styles> <StackPanel> <Border Name='foo'/> </StackPanel> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var foo = window.FindControl <Border>("foo"); Assert.Null(foo.Background); ((IPseudoClasses)foo.Classes).Add(":foo-bar"); Assert.Equal(Colors.Red, ((ISolidColorBrush)foo.Background).Color); } }
public void DataTemplate_Can_Be_Empty() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <s:SampleTemplatedObjectContainer xmlns='https://github.com/avaloniaui' xmlns:sys='clr-namespace:System;assembly=netstandard' xmlns:s='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <s:SampleTemplatedObjectContainer.Template> <s:SampleTemplatedObjectTemplate> <s:SampleTemplatedObject x:Name='root'> <s:SampleTemplatedObject x:Name='child1' Foo='foo' /> <s:SampleTemplatedObject x:Name='child2' Foo='bar' /> </s:SampleTemplatedObject> </s:SampleTemplatedObjectTemplate> </s:SampleTemplatedObjectContainer.Template> </s:SampleTemplatedObjectContainer>"; var container = (SampleTemplatedObjectContainer)AvaloniaRuntimeXamlLoader.Load(xaml, typeof(GenericTemplateTests).Assembly); var res = TemplateContent.Load <SampleTemplatedObject>(container.Template.Content); Assert.Equal(res.Result, res.NameScope.Find("root")); Assert.Equal(res.Result.Content[0], res.NameScope.Find("child1")); Assert.Equal(res.Result.Content[1], res.NameScope.Find("child2")); Assert.Equal("foo", res.Result.Content[0].Foo); Assert.Equal("bar", res.Result.Content[1].Foo); } }
public void Style_Can_Use_Or_Selector_1() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Styles> <Style Selector='Border.foo, Border.bar'> <Setter Property='Background' Value='Red'/> </Style> </Window.Styles> <StackPanel> <Border Name='foo' Classes='foo'/> <Border Name='bar' Classes='bar'/> <Border Name='baz' Classes='baz'/> </StackPanel> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var foo = window.FindControl <Border>("foo"); var bar = window.FindControl <Border>("bar"); var baz = window.FindControl <Border>("baz"); Assert.Equal(Brushes.Red, foo.Background); Assert.Equal(Brushes.Red, bar.Background); Assert.Null(baz.Background); } }
public void Style_Can_Use_Or_Selector_2() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Styles> <Style Selector='Button,Carousel,ListBox'> <Setter Property='Background' Value='Red'/> </Style> </Window.Styles> <StackPanel> <Button Name='button'/> <Carousel Name='carousel'/> <ListBox Name='listBox'/> </StackPanel> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var button = window.FindControl <Button>("button"); var carousel = window.FindControl <Carousel>("carousel"); var listBox = window.FindControl <ListBox>("listBox"); Assert.Equal(Brushes.Red, button.Background); Assert.Equal(Brushes.Red, carousel.Background); Assert.Equal(Brushes.Red, listBox.Background); } }
public void Setter_Value_Is_Bound_Directly_If_The_Target_Type_Derives_From_ITemplate() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Styles> <Style Selector=':is(Control)'> <Setter Property='FocusAdorner'> <FocusAdornerTemplate> <Rectangle Stroke='Black' StrokeThickness='1' StrokeDashArray='1,2'/> </FocusAdornerTemplate> </Setter> </Style> </Window.Styles> <TextBlock Name='target'/> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var target = window.Find <TextBlock>("target"); Assert.NotNull(target.FocusAdorner); } }
public void Setter_Can_Contain_Template() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Styles> <Style Selector='ContentControl'> <Setter Property='Content'> <Template> <TextBlock>Hello World!</TextBlock> </Template> </Setter> </Style> </Window.Styles> <ContentControl Name='target'/> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var target = window.Find <ContentControl>("target"); Assert.IsType <TextBlock>(target.Content); Assert.Equal("Hello World!", ((TextBlock)target.Content).Text); } }
public void ResourceInclude_Loads_ResourceDictionary() { var includeXaml = @" <ResourceDictionary xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> </ResourceDictionary> "; using (StartWithResources(("test:include.xaml", includeXaml))) { var xaml = @" <UserControl xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceInclude Source='test:include.xaml'/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> <Border Name='border' Background='{StaticResource brush}'/> </UserControl>"; var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); var border = userControl.FindControl <Border>("border"); var brush = (ISolidColorBrush)border.Background; Assert.Equal(0xff506070, brush.Color.ToUint32()); } }
public void DynamicResource_Tracks_Added_Style_MergedResource_Dictionary() { var xaml = @" <UserControl xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <UserControl.Styles> <Style> </Style> </UserControl.Styles> <Border Name='border' Background='{DynamicResource brush}'/> </UserControl>"; var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); var border = userControl.FindControl <Border>("border"); DelayedBinding.ApplyBindings(border); Assert.Null(border.Background); var dictionary = new ResourceDictionary { { "brush", new SolidColorBrush(0xff506070) }, }; ((Style)userControl.Styles[0]).Resources.MergedDictionaries.Add(dictionary); var brush = (SolidColorBrush)border.Background; Assert.NotNull(brush); Assert.Equal(0xff506070, brush.Color.ToUint32()); }
public void MultiBinding_TemplatedParent_Works() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Data;assembly=Avalonia.Markup.Xaml.UnitTests'> <TextBox Name='textBox' Text='Foo' Watermark='Bar'> <TextBox.Template> <ControlTemplate> <TextPresenter Name='PART_TextPresenter'> <TextPresenter.Text> <MultiBinding Converter='{x:Static local:ConcatConverter.Instance}'> <Binding RelativeSource='{RelativeSource TemplatedParent}' Path='Text'/> <Binding RelativeSource='{RelativeSource TemplatedParent}' Path='Watermark'/> </MultiBinding> </TextPresenter.Text> </TextPresenter> </ControlTemplate> </TextBox.Template> </TextBox> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var textBox = window.FindControl <TextBox>("textBox"); window.ApplyTemplate(); textBox.ApplyTemplate(); var target = (TextPresenter)textBox.GetVisualChildren().Single(); Assert.Equal("Foo,Bar", target.Text); } }
public void Control_Property_Is_Updated_When_Parent_Is_Changed() { using (StyledWindow()) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Resources> <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> </Window.Resources> <Border Name='border' Background='{DynamicResource brush}'/> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var border = window.FindControl <Border>("border"); DelayedBinding.ApplyBindings(border); var brush = (SolidColorBrush)border.Background; Assert.Equal(0xff506070, brush.Color.ToUint32()); window.Content = null; Assert.Null(border.Background); window.Content = border; brush = (SolidColorBrush)border.Background; Assert.Equal(0xff506070, brush.Color.ToUint32()); } }
public void Should_Close_When_Tip_Is_Opened_And_Detached_From_Visual_Tree() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Panel x:Name='PART_panel'> <Decorator x:Name='PART_target' ToolTip.Tip='{Binding Tip}' ToolTip.ShowDelay='0' /> </Panel> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); window.DataContext = new ToolTipViewModel(); window.ApplyTemplate(); window.Presenter.ApplyTemplate(); var target = window.Find <Decorator>("PART_target"); var panel = window.Find <Panel>("PART_panel"); Assert.True((target as IVisual).IsAttachedToVisualTree); _mouseHelper.Enter(target); Assert.True(ToolTip.GetIsOpen(target)); panel.Children.Remove(target); Assert.False(ToolTip.GetIsOpen(target)); } }
public void StaticResource_From_Application_Can_Be_Assigned_To_Property_In_UserControl() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { Application.Current.Resources.Add("brush", new SolidColorBrush(0xff506070)); var xaml = @" <UserControl xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Border Name='border' Background='{StaticResource brush}'/> </UserControl>"; var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml); var border = userControl.FindControl <Border>("border"); // We don't actually know where the global styles are until we attach the control // to a window, as Window has StylingParent set to Application. var window = new Window { Content = userControl }; window.Show(); var brush = (SolidColorBrush)border.Background; Assert.Equal(0xff506070, brush.Color.ToUint32()); } }
public void MultiBinding_To_TextBlock_Text_With_StringConverter_Works(string fmt) { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'> <TextBlock Name='textBlock'> <TextBlock.Text> <MultiBinding StringFormat='" + fmt + @"'> <Binding Path='Greeting1'/> <Binding Path='Greeting2'/> </MultiBinding> </TextBlock.Text> </TextBlock> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var textBlock = window.FindControl <TextBlock>("textBlock"); textBlock.DataContext = new WindowViewModel(); window.ApplyTemplate(); Assert.Equal("Hello World!", textBlock.Text); } }
public void StaticResource_Can_Be_Assigned_To_Setter_In_Styles_File() { var styleXaml = @" <Styles xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Styles.Resources> <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> </Styles.Resources> <Style Selector='Border'> <Setter Property='Background' Value='{StaticResource brush}'/> </Style> </Styles>"; using (StyledWindow(assets: ("test:style.xaml", styleXaml))) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Styles> <StyleInclude Source='test:style.xaml'/> </Window.Styles> <Border Name='border'/> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var border = window.FindControl <Border>("border"); var brush = (SolidColorBrush)border.Background; Assert.Equal(0xff506070, brush.Color.ToUint32()); } }
public void Binding_Classes_Works() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { // Note, this test also checks `Classes` reordering, so it should be kept AFTER the last single class var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'> <Button Name='button' Classes.MyClass='{Binding Foo}' Classes.MySecondClass='True' Classes='foo bar'/> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var button = window.FindControl <Button>("button"); button.DataContext = new { Foo = true }; window.ApplyTemplate(); Assert.True(button.Classes.Contains("MyClass")); Assert.True(button.Classes.Contains("MySecondClass")); Assert.True(button.Classes.Contains("foo")); Assert.True(button.Classes.Contains("bar")); button.DataContext = new { Foo = false }; Assert.False(button.Classes.Contains("MyClass")); Assert.True(button.Classes.Contains("MySecondClass")); Assert.True(button.Classes.Contains("foo")); Assert.True(button.Classes.Contains("bar")); } }
public void ResolvesStreamObservableBindingCorrectly() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests' x:DataType='local:TestDataContext'> <TextBlock Text='{CompiledBinding ObservableProperty^}' Name='textBlock' /> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var textBlock = window.FindControl <TextBlock>("textBlock"); DelayedBinding.ApplyBindings(textBlock); var subject = new Subject <string>(); var dataContext = new TestDataContext { ObservableProperty = subject }; window.DataContext = dataContext; subject.OnNext("foobar"); Assert.Equal("foobar", textBlock.Text); } }
public void DataTemplate_Can_Contain_Name() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:sys='clr-namespace:System;assembly=netstandard' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.DataTemplates> <DataTemplate DataType='{x:Type sys:String}'> <Canvas Name='foo'/> </DataTemplate> </Window.DataTemplates> <ContentControl Name='target' Content='Foo'/> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var target = window.FindControl <ContentControl>("target"); window.ApplyTemplate(); target.ApplyTemplate(); ((ContentPresenter)target.Presenter).UpdateChild(); Assert.IsType <Canvas>(target.Presenter.Child); } }
public void ResolvesNonIntegerIndexerBindingFromParentInterfaceCorrectly() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests' x:DataType='local:TestDataContext'> <TextBlock Text='{CompiledBinding NonIntegerIndexerInterfaceProperty[Test]}' Name='textBlock' /> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var textBlock = window.FindControl <TextBlock>("textBlock"); var dataContext = new TestDataContext(); dataContext.NonIntegerIndexerInterfaceProperty["Test"] = "Initial Value"; window.DataContext = dataContext; Assert.Equal(dataContext.NonIntegerIndexerInterfaceProperty["Test"], textBlock.Text); dataContext.NonIntegerIndexerInterfaceProperty["Test"] = "New Value"; Assert.Equal(dataContext.NonIntegerIndexerInterfaceProperty["Test"], textBlock.Text); } }
public void DataTemplate_Can_Contain_Named_UserControl() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:sys='clr-namespace:System;assembly=mscorlib' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <ItemsControl Name='itemsControl' Items='{Binding}'> <ItemsControl.ItemTemplate> <DataTemplate> <UserControl Name='foo'/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var itemsControl = window.FindControl <ItemsControl>("itemsControl"); window.DataContext = new[] { "item1", "item2" }; window.ApplyTemplate(); itemsControl.ApplyTemplate(); itemsControl.Presenter.ApplyTemplate(); Assert.Equal(2, itemsControl.Presenter.Panel.Children.Count); } }
public void InfersDataTemplateTypeFromParentCollectionItemsType() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests' x:DataType='local:TestDataContext'> <ItemsControl Items='{CompiledBinding ListProperty}' Name='target'> <ItemsControl.DataTemplates> <DataTemplate> <TextBlock Text='{CompiledBinding}' Name='textBlock' /> </DataTemplate> </ItemsControl.DataTemplates> </ItemsControl> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var target = window.FindControl <ItemsControl>("target"); var dataContext = new TestDataContext(); dataContext.ListProperty.Add("Test"); window.DataContext = dataContext; window.ApplyTemplate(); target.ApplyTemplate(); target.Presenter.ApplyTemplate(); Assert.Equal(dataContext.ListProperty[0], (string)((ContentPresenter)target.Presenter.Panel.Children[0]).Content); } }
public void Missing_ResourceKey_In_StyleInclude_Does_Not_Cause_StackOverflow() { var styleXaml = @" <Style xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Style.Resources> <StaticResource x:Key='brush' ResourceKey='missing' /> </Style.Resources> </Style>"; using (StartWithResources(("test:style.xaml", styleXaml))) { var xaml = @" <Application xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Application.Styles> <StyleInclude Source='test:style.xaml'/> </Application.Styles> </Application>"; var app = Application.Current; try { AvaloniaRuntimeXamlLoader.Load(xaml, null, app); } catch (KeyNotFoundException) { } } }
public void CompilesBindingWhenRequested() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests' x:DataType='local:TestDataContext' x:CompileBindings='true'> <TextBlock Text='{Binding StringProperty}' Name='textBlock' /> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var textBlock = window.FindControl <TextBlock>("textBlock"); var dataContext = new TestDataContext { StringProperty = "foobar" }; window.DataContext = dataContext; Assert.Equal(dataContext.StringProperty, textBlock.Text); } }
public void Binding_To_Self_In_Style_Works() { using (UnitTestApplication.Start(TestServices.StyledWindow)) { var xaml = @" <Window xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> <Window.Styles> <Style Selector='Button'> <Setter Property='IsVisible' Value='{Binding $self.IsEnabled}' /> </Style> </Window.Styles> <Button Name='button' /> </Window>"; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); var button = window.FindControl <Button>("button"); window.ApplyTemplate(); window.Presenter.ApplyTemplate(); Assert.True(button.IsVisible); button.IsEnabled = false; Assert.False(button.IsVisible); } }