Skip to content

Commit

Permalink
fix PropertyGrid not opening complex child viewmodel in new window, a…
Browse files Browse the repository at this point in the history
…dded sukihost support back
  • Loading branch information
Insire committed Nov 8, 2024
1 parent c5b7833 commit 4bf06e9
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 51 deletions.
31 changes: 21 additions & 10 deletions SukiUI.Demo/Features/ControlsLibrary/PropertyGridView.axaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
<UserControl x:Class="SukiUI.Demo.Features.ControlsLibrary.PropertyGridView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:suki="https://github.com/kikipoulet/SukiUI"
xmlns:controlsLibrary="clr-namespace:SukiUI.Demo.Features.ControlsLibrary"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:suki="https://github.com/kikipoulet/SukiUI"
d:DesignHeight="1050"
d:DesignWidth="800"
x:DataType="controlsLibrary:PropertyGridViewModel"
mc:Ignorable="d">

<UserControl.Resources>
<suki:PropertyGridTemplateSelector x:Key="PropertyGridTemplateSelector" UseSukiHost="True" />
</UserControl.Resources>

<WrapPanel Classes="PageContainer" Orientation="Horizontal">
<suki:GlassCard Width="500">
<ScrollViewer>
<suki:PropertyGrid Item="{Binding Form}">
<suki:PropertyGrid.DataTemplates>
<!--
replace the PropertyGridTemplateSelector with your own type or subclass it,
if you want to customize the dataTemplates being used
-->
<suki:PropertyGridTemplateSelector UseSukiHost="False" />
</suki:PropertyGrid.DataTemplates>
</suki:PropertyGrid>
<DockPanel>
<ToggleButton HorizontalAlignment="Right"
Content="Use external dialog window"
DockPanel.Dock="Top"
IsChecked="True"
IsCheckedChanged="ToggleButton_OnIsCheckedChanged" />
<suki:PropertyGrid Item="{Binding Form}">
<suki:PropertyGrid.DataTemplates>
<!--
replace the PropertyGridTemplateSelector with your own type or subclass it,
if you want to customize the dataTemplates being used
-->
<StaticResource ResourceKey="PropertyGridTemplateSelector" />
</suki:PropertyGrid.DataTemplates>
</suki:PropertyGrid>
</DockPanel>
</ScrollViewer>
</suki:GlassCard>

Expand Down
17 changes: 17 additions & 0 deletions SukiUI.Demo/Features/ControlsLibrary/PropertyGridView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using SukiUI.Controls;

namespace SukiUI.Demo.Features.ControlsLibrary;

Expand All @@ -8,4 +11,18 @@ public PropertyGridView()
{
InitializeComponent();
}

private void ToggleButton_OnIsCheckedChanged(object? sender, RoutedEventArgs e)
{
if (sender is not ToggleButton toggleButton)
{
return;
}

var resource = this.FindResource("PropertyGridTemplateSelector");
if (resource is PropertyGridTemplateSelector templateSelector)
{
templateSelector.UseSukiHost = toggleButton.IsChecked == true;
}
}
}
92 changes: 65 additions & 27 deletions SukiUI/Controls/PropertyGrid/PropertyGridTemplateSelector.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Avalonia.Controls;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
Expand All @@ -9,8 +11,10 @@ namespace SukiUI.Controls;

public partial class PropertyGridTemplateSelector : ResourceDictionary, IDataTemplate
{
public SukiDialogHost? SukiDialogHost { get; set; }

public bool UseSukiHost { get; set; } = true;

public PropertyGridTemplateSelector()
{
InitializeComponent();
Expand Down Expand Up @@ -57,46 +61,80 @@ public bool Match(object? data)
return false;
}

if (ContainsKey(key) == false)
return ContainsKey(key) != false;
}

private static void ShowSukiHostDialog(ISukiDialogManager manager, ComplexTypeViewModel viewModel)
{
manager
.CreateDialog()
.WithContent(new PropertyGridDialog()
{
DataContext = viewModel.Value
})
.WithTitle(viewModel.DisplayName)
.Dismiss().ByClickingBackground()
.TryShow();
}

private static async Task ShowWindowDialogAsync(Control control)
{
var root = control.GetVisualRoot();
if (root is not Window parentWindow || control.DataContext is not ComplexTypeViewModel childViewModel || childViewModel.Value is null)
{
return false;
return;
}

return true;
var window = new PropertyGridWindow()
{
DataContext = childViewModel.Value,
Title = childViewModel.DisplayName,
};

await window.ShowDialog(parentWindow);
}

private async void OnMoreInfoClick(object sender, RoutedEventArgs e)
protected virtual async void OnMoreInfoClick(object sender, RoutedEventArgs e)
{
if (sender is not Control control)
{
return;
}
// TODO: No longer possible to just statically use SukiHost to show dialogs.
// if (UseSukiHost)
// {
// if (control.DataContext is not ComplexTypeViewModel childViewModel || childViewModel.Value is null)
// {
// return;
// }
// SukiHost.ShowDialog(new PropertyGridDialog()
// {
// DataContext = childViewModel.Value
// }, true, true);
// }
else

var sukiDialogHost = SukiDialogHost;
if (UseSukiHost)
{
var root = control.GetVisualRoot();
if (root is not Window parentWindow || control.DataContext is not ComplexTypeViewModel childViewModel || childViewModel.Value is null)
if (sukiDialogHost is not null)
{
return;
}
if (control.DataContext is not ComplexTypeViewModel childViewModel || childViewModel.Value is null)
{
return;
}

var window = new PropertyGridWindow()
ShowSukiHostDialog(sukiDialogHost.Manager, childViewModel);
}
else
{
DataContext = childViewModel,
};
var root = control.GetVisualRoot();
if (root is not SukiWindow parentWindow || control.DataContext is not ComplexTypeViewModel childViewModel || childViewModel.Value is null)
{
return;
}

await window.ShowDialog(parentWindow);
sukiDialogHost = parentWindow.Hosts.Where(p => p is SukiDialogHost).Cast<SukiDialogHost>().FirstOrDefault();
if (sukiDialogHost is not null)
{
ShowSukiHostDialog(sukiDialogHost.Manager, childViewModel);
}
else
{
await ShowWindowDialogAsync(control);
}
}
}
else
{
await ShowWindowDialogAsync(control);
}
}
}
25 changes: 11 additions & 14 deletions SukiUI/Controls/PropertyGrid/PropertyGridWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
xmlns:controls="clr-namespace:SukiUI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="{Binding DisplayName}"
Width="400"
Height="500"
d:DesignHeight="400"
Expand All @@ -14,20 +13,18 @@
mc:Ignorable="d">

<Border Padding="5" Background="{DynamicResource SukiBackground}">
<Panel>
<ScrollViewer>
<controls:PropertyGrid Item="{Binding Viewmodel}">
<controls:PropertyGrid.DataTemplates>
<ScrollViewer>
<controls:PropertyGrid Item="{Binding}">
<controls:PropertyGrid.DataTemplates>

<!--
replace the PropertyGridTemplateSelector with your own type or subclass it,
if you want to customize the datatemplates being used
-->
<!--
replace the PropertyGridTemplateSelector with your own type or subclass it,
if you want to customize the datatemplates being used
-->

<controls:PropertyGridTemplateSelector />
</controls:PropertyGrid.DataTemplates>
</controls:PropertyGrid>
</ScrollViewer>
</Panel>
<controls:PropertyGridTemplateSelector />
</controls:PropertyGrid.DataTemplates>
</controls:PropertyGrid>
</ScrollViewer>
</Border>
</controls:SukiWindow>

0 comments on commit 4bf06e9

Please sign in to comment.