Here is my view:
<UserControl 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" d:DesignWidth="250" d:DesignHeight="450"
xmlns:vm="clr-namespace:Application.ViewModels;assembly=Application"
x:DataType="vm:TaskListViewModel"
x:Class="TaskTracker.Views.TaskListView">
<DockPanel>
<Button DockPanel.Dock="Bottom"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
x:CompileBindings="False"
Command="{Binding $parent[Window].DataContext.AddTask}">
Add Item
</Button>
<ItemsControl ItemsSource="{Binding Tasks}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Aqua" BorderThickness="2">
<DockPanel Margin="10">
<TextBlock HorizontalAlignment="Left" FontSize="24"
Text="{Binding Content}" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock FontSize="24" Text="{Binding TimeWasted}" />
<Button
Command="{Binding }"
CommandParameter="{Binding Id}">
<PathIcon Data="{StaticResource play_circle_regular}" />
</Button>
</StackPanel>
</DockPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>
</UserControl>
In the most nested button I need to bind to UserControl’s DataContext, but the current DataContext is Task because of ItemsControl. How can I reach TaskListViewModel from there?
I tried to do it with
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type vm:TaskListViewModel}}}"
but it didn’t help
I also tried
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
but the DataContext was TaskListView instead of TaskListViewModel