Skip to content

Commit fb5d364

Browse files
committed
Enhance data binding and simplify activation logic
- Updated `ListBox` in `ConductorView.axaml` to bind `SelectedItem` to `ActiveItem` for two-way data binding. - Removed `OnActivateAsync` method in `ConductorViewModel.cs` to simplify the activation process. - Adjusted `Loaded` event handling in `ActionMessage.cs` for improved clarity. - Added convention in `ConventionManager.cs` to bind `ItemsSource` of `ListBox` to `DataContext`. - Changed `Unloaded` event handler in `View.cs` to use `RoutedEventArgs` for proper cleanup in Avalon.
1 parent 92a47a7 commit fb5d364

File tree

5 files changed

+5
-10
lines changed

5 files changed

+5
-10
lines changed

samples/features/Features.Avalonia/Views/ConductorView.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</StackPanel>
1919

2020

21-
<ListBox Name="Items" Grid.Row="1" Margin="40,0">
21+
<ListBox Name="Items" Grid.Row="1" Margin="40,0" SelectedItem="{Binding ActiveItem, Mode=TwoWay}">
2222
<ListBox.ItemsPanel>
2323
<ItemsPanelTemplate>
2424
<VirtualizingStackPanel Orientation="Horizontal"/>

samples/features/Features.CrossPlatform.Shared/ViewModels/ConductorViewModel.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ public ConductorViewModel()
1313
Items.CollectionChanged += (s, e) => NotifyOfPropertyChange(() => CanCloseTab);
1414
}
1515

16-
protected override async Task OnActivateAsync(CancellationToken cancellationToken)
17-
{
18-
// Optionally perform any logic before activation here
19-
await base.OnActivateAsync(cancellationToken);
20-
}
2116
protected override async Task OnActivatedAsync(CancellationToken cancellationToken)
2217
{
2318
// Optionally perform any logic after activation here

src/Caliburn.Micro.Platform/ActionMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ protected override void OnAttached()
214214
if (View.ExecuteOnLoad(AssociatedObject, ElementLoaded))
215215
{
216216
#if AVALONIA
217-
//string eventName = "AttachedToLogicalTree";
218217
string eventName = "Loaded";
219218
var trigger = Interaction.GetBehaviors(AssociatedObject)
220219
.OfType<EventTrigger>()

src/Caliburn.Micro.Platform/ConventionManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ static ConventionManager()
340340
#endif
341341
#if AVALONIA
342342
AddElementConvention<UserControl>(UserControl.IsVisibleProperty, "DataContext", loadedEvent);
343+
AddElementConvention<ListBox>(ListBox.ItemsSourceProperty, "DataContext", loadedEvent);
343344
#else
344345
AddElementConvention<UserControl>(UserControl.VisibilityProperty, "DataContext", loadedEvent);
345346
#endif

src/Caliburn.Micro.Platform/View.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ public static bool ExecuteOnLoad(FrameworkElement element, RoutedEventHandler ha
234234
#if AVALONIA
235235
public static void ExecuteOnUnload(FrameworkElement element, EventHandler handler)
236236
{
237-
EventHandler<LogicalTreeAttachmentEventArgs> unloaded = null;
237+
EventHandler<RoutedEventArgs> unloaded = null;
238238
unloaded = (s, e) =>
239239
{
240-
element.DetachedFromLogicalTree -= unloaded;
240+
element.Unloaded -= unloaded;
241241
handler(s, e);
242242
};
243-
element.DetachedFromLogicalTree += unloaded;
243+
element.Unloaded += unloaded;
244244
}
245245
#else
246246
public static void ExecuteOnUnload(FrameworkElement element, RoutedEventHandler handler)

0 commit comments

Comments
 (0)