Home > Software design >  AvaloniaUI binding to command outside of ItemsSource
AvaloniaUI binding to command outside of ItemsSource

Time:12-18

I have a problem. I want to give a button a command from ViewModel, which is outside of ItemsRepeater ItemsSource. I need help on how to do such binding

Button in my ItemsRepeater

<Button Command={Binding TestClick} Grid.Column="0" HorizontalAlignment="Stretch" Foreground="#6264a7" HorizontalContentAlignment="Center" CornerRadius="0" Background="#2f2f2f" FontSize="20">Details</Button>

My ItemsRepeater

                            <ItemsRepeater.ItemTemplate>
                                <DataTemplate>
                                    <DockPanel Margin="30,0,30,50">
                                        <StackPanel>
                                            <TextBlock Background="#2f2f2f" FontSize="25" Foreground="AntiqueWhite" TextAlignment="Center" Padding="0,8,0,8" Text="{Binding Name}"></TextBlock>
                                            <TextBlock TextAlignment="Center" Background="#2f2f2f" Foreground="AntiqueWhite" Height="40" FontSize="20" Padding="0,8,0,0" Text="{Binding Date}"></TextBlock>
                                            <TextBlock TextAlignment="Center" Background="#2f2f2f" Foreground="AntiqueWhite" Height="40" FontSize="20" Padding="0,2,0,0" Text="{Binding EventType}"></TextBlock>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="50*" />
                                                    <ColumnDefinition Width="50*" />
                                                </Grid.ColumnDefinitions>

                                                <Button Command={Binding TestClick} Grid.Column="0" HorizontalAlignment="Stretch" Foreground="#6264a7" HorizontalContentAlignment="Center" CornerRadius="0" Background="#2f2f2f" FontSize="20">Details</Button>
                                                <Button Grid.Column="1" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Foreground="#a4373a" CornerRadius="0" Background="#2f2f2f" FontSize="20">Delete</Button>

                                            </Grid>

                                            <ProgressBar Height="10" CornerRadius="0" Value="{Binding TimeLeft}" Minimum="0" Maximum="{Binding DifferenceBetweenDates}" Foreground="{Binding ProgressBarColour}" />
                                        </StackPanel>
                                    </DockPanel>
                                </DataTemplate>
                            </ItemsRepeater.ItemTemplate>
                        </ItemsRepeater>

My ViewModel:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anniverse.ViewModels
{
    class MainPanelViewModel : ViewModelBase
    {
        public string CurrentDate => DateTime.Today.ToString("dd.MM.yyyy");

        public ObservableCollection<Event> Events => new Connector().GetEvents();

        public void TestClick() 
        {
            Console.WriteLine("Hello test");
        }
    }
}

CodePudding user response:

The easiest way to achieve that would be to bind to the ancestor

Command="{Binding $parent[ItemsRepeater].DataContext.YourCommand}"

Note that you need to define the ICommand in your view model for the command binding to make it work, you can find an example here

EDIT: I was not aware of it, but binding directly to methods should work as well

  • Related