Skip to content

Commit

Permalink
Use styled properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sandermvanvliet committed Nov 19, 2023
1 parent e1d38bf commit 1de2535
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/RoadCaptain.App.RouteBuilder/Views/LandingPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
Grid.Row="1"
Routes="{Binding Path=MyRoutes, Mode=OneWay}"
Margin="0,0,4,8"
SelectedRoute="{Binding SelectedRoute}" />
SelectedRoute="{Binding SelectedRoute, Mode=TwoWay}" />
<Grid
Grid.Row="1"
IsVisible="{Binding InProgress}"
Expand Down
3 changes: 2 additions & 1 deletion src/RoadCaptain.App.Shared/Controls/RoutesList.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
ScrollViewer.VerticalScrollBarVisibility="Auto"
VerticalAlignment="Stretch"
DoubleTapped="RoutesListBox_OnDoubleTapped"
SelectionChanged="RoutesListBox_OnSelectionChanged">
SelectionChanged="RoutesListBox_OnSelectionChanged"
SelectedItem="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:RoutesList}}, Path=SelectedRoute}">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Margin" Value="0" />
Expand Down
37 changes: 11 additions & 26 deletions src/RoadCaptain.App.Shared/Controls/RoutesList.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ namespace RoadCaptain.App.Shared.Controls
{
public partial class RoutesList : UserControl
{
public event EventHandler<RouteSelectedEventArgs> RouteSelected;
public static readonly DirectProperty<RoutesList, RouteViewModel?> SelectedRouteProperty = AvaloniaProperty.RegisterDirect<RoutesList, RouteViewModel?>(
nameof(SelectedRoute),
control => control.SelectedRoute,
(control, value) => control.SelectedRoute = value);

public static readonly DirectProperty<RoutesList, RouteViewModel[]> RoutesProperty = AvaloniaProperty.RegisterDirect<RoutesList, RouteViewModel[]>(
nameof(Routes),
control => control.Routes,
(control, value) => control.Routes = value);

private RouteViewModel? _selectedRoute;
private RouteViewModel[] _routes = Array.Empty<RouteViewModel>();
public event EventHandler<RouteSelectedEventArgs>? RouteSelected;

public static readonly StyledProperty<RouteViewModel?> SelectedRouteProperty =
AvaloniaProperty.Register<RoutesList, RouteViewModel?>(nameof(SelectedRoute));

public static readonly StyledProperty<RouteViewModel[]> RoutesProperty =
AvaloniaProperty.Register<RoutesList, RouteViewModel[]>(nameof(Routes));

public RoutesList()
{
Expand All @@ -45,7 +39,6 @@ private void RoutesListBox_OnDoubleTapped(object? sender, RoutedEventArgs e)
return;
}

SelectedRoute = selectedRoute;
RouteSelected?.Invoke(this, new RouteSelectedEventArgs(selectedRoute, SelectionIntent.SelectAndChoose));
}

Expand All @@ -61,27 +54,19 @@ private void RoutesListBox_OnSelectionChanged(object? sender, SelectionChangedEv
return;
}

SelectedRoute = selectedRoute;
RouteSelected?.Invoke(this, new RouteSelectedEventArgs(selectedRoute, SelectionIntent.Select));
}

public RouteViewModel? SelectedRoute
{
get => _selectedRoute;
set
{
_selectedRoute = value;
if (value == null)
{
this.Find<ListBox>("RoutesListBox").SelectedItem = null;
}
}
get => GetValue(SelectedRouteProperty);
set => SetValue(SelectedRouteProperty, value);
}

public RouteViewModel[] Routes
{
get => _routes;
set => SetAndRaise(RoutesProperty, ref _routes, value);
get => GetValue(RoutesProperty);
set => SetValue(RoutesProperty, value);
}
}

Expand Down

0 comments on commit 1de2535

Please sign in to comment.