Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom styles not applied if control is removed & re-added #10012

Closed
derekantrican opened this issue Jan 19, 2023 · 4 comments
Closed

Custom styles not applied if control is removed & re-added #10012

derekantrican opened this issue Jan 19, 2023 · 4 comments

Comments

@derekantrican
Copy link
Contributor

Describe the bug

I have a control with a custom style applied via a class selector. If the control is programmatically removed & re-added, the style is not applied.

To Reproduce
Min repro:

MainWindow.axaml

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Width="400" Height="450"
        x:Class="AvaloniaMissingStylesDemo.MainWindow"
        Title="AvaloniaMissingStylesDemo">
	<Window.Styles>
		<Style Selector="ComboBox.myClass">
			<Setter Property="ItemTemplate">
				<Setter.Value>
					<DataTemplate>
						<TextBlock Text="{Binding Name}"/>
					</DataTemplate>
				</Setter.Value>
			</Setter>
		</Style>
	</Window.Styles>
	<StackPanel>
		<Button Content="Toggle control" Command="{Binding ToggleControl}" Margin="5"/>
		<Panel x:Name="myPanel">
			<ComboBox Classes="myClass" Width="300" Items="{Binding Items}" Margin="5"/>
		</Panel>
	</StackPanel>
</Window>

MainWindow.axaml.cs

using System.Collections.Generic;
using System.Collections.ObjectModel;
using Avalonia.Controls;

namespace AvaloniaMissingStylesDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;
        }

        public ObservableCollection<MyClass> Items { get; set; } = new ObservableCollection<MyClass>(new List<MyClass>
        {
            new MyClass
            {
                Name = "Foo1",
            },
            new MyClass
            {
                Name = "Foo2",
            },
            new MyClass
            {
                Name = "Foo3",
            },
        });

        Control childControl = null;
        public void ToggleControl()
        {
            Panel panel = this.FindControl<Panel>("myPanel");

            if (panel.Children.Count > 0)
            {
                childControl = panel.Children[0] as Control;
                panel.Children.Clear();
            }
            else
            {
                panel.Children.Add(childControl);
            }
        }

        public class MyClass
        {
            public string Name { get; set; }
        }
    }
}

Expected behavior
The control still has the classes, when the control is shown again, I expect the style to be re-applied.

Desktop (please complete the following information):

  • OS: Windows 11
  • Version: 0.10.18
@derekantrican
Copy link
Contributor Author

Also tested in 11.0.0-preview4. Issue still repros with above code

@derekantrican
Copy link
Contributor Author

A valid workaround for the time being is to toggle childControl.IsVisible as IsVisible means the control doesn't take up space (same as Collapsed in WPF)

@grokys
Copy link
Member

grokys commented Feb 14, 2023

Seems to be fixed in preview5 and current master. From a bisect it looks like it was fixed by #9677.

@grokys grokys closed this as completed Feb 14, 2023
@derekantrican
Copy link
Contributor Author

@grokys Interesting - that looks like quite the PR! Thanks for the update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants