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

x:Load/ElementStub doesn't work with Border, among others #19173

Open
ramezgerges opened this issue Jan 9, 2025 · 0 comments
Open

x:Load/ElementStub doesn't work with Border, among others #19173

ramezgerges opened this issue Jan 9, 2025 · 0 comments
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification

Comments

@ramezgerges
Copy link
Contributor

Current behavior

ElementStub works by adding the lazy-loaded element to the parent's UIElement._children collection. This doesn't work with Border, which needs its Child property set for layout purposes (and many other things). I have a repro with a Border, but conceptually, the same problem will appear with a large number of classes.

Expected behavior

No response

How to reproduce it (as minimally and precisely as possible)

Clicking the red square should toggle the visibility of the textblock.

using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.UI.Xaml.Input;
using Uno.Extensions;

namespace UnoApp1;

public class SquareNavigationViewItem : INotifyPropertyChanged
{
    private bool _isSelected;
    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    public bool IsSelected
    {
        get => _isSelected;
        set => SetField(ref _isSelected, value);
    }
}

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
    {
        ((Border)sender).IsHoldingEnabled = !((Border)sender).IsHoldingEnabled;
    }
}
<Page
  x:Class="UnoApp1.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:UnoApp1"
  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  <ContentControl>
    <ContentControl.ContentTemplate>
      <DataTemplate
        x:DataType="local:SquareNavigationViewItem">
        <Border x:Name="nvi" PointerPressed="OnPointerPressed" Width="100" Height="100" Background="Red" IsHoldingEnabled="{x:Bind IsSelected, Mode=TwoWay}">
          <TextBlock
            x:Name="tb"
            x:Load="{x:Bind IsSelected, Mode=OneWay}"
            Text="visible" />
        </Border>
      </DataTemplate>
    </ContentControl.ContentTemplate>
    <ContentControl.Content>
      <local:SquareNavigationViewItem />
    </ContentControl.Content>
  </ContentControl>

</Page>

Workaround

No response

Works on UWP/WinUI

None

Environment

No response

NuGet package version(s)

No response

Affected platforms

No response

IDE

No response

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

@ramezgerges ramezgerges added kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification
Projects
None yet
Development

No branches or pull requests

1 participant