1

So i have a view with an ItemsControl which is bound to some ObservableCollection. In the DataTemplate i need two buttons. When i try to bind these buttons to where i have defined them, and i start the application, nothing happens on button click.

The view:

<UserControl x:Class="GraphicalUserInterface.Views._2_ToDoList.ToDoListMainView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:GraphicalUserInterface.Views._2_ToDoList"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="600"
         DataContext="{Binding Source={StaticResource Locator}, Path=ToDoListMain}">
<Grid>
    <ItemsControl Margin="5" ItemsSource="{Binding ListEntries}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border CornerRadius="5" BorderThickness="2" BorderBrush="Black" Height="50" Margin="5">
                    <StackPanel Orientation="Horizontal" Margin="0,5">
                        <Label FontWeight="Bold">Customer:</Label>
                        <Label Content="{Binding Customer}" Margin="0,0,20,0"/>
                        <Label FontWeight="Bold">Trainer:</Label>
                        <Label Content="{Binding Trainer}" Margin="0,0,20,0"/>
                        <Label FontWeight="Bold">Date:</Label>
                        <Label Content="{Binding Date}" Margin="0,0,20,0"/>
                        <Label FontWeight="Bold">RequestType:</Label>
                        <Label Content="{Binding RequestType}" Margin="0,0,20,0"/>
                        <Button Margin="5" Width="100" CommandParameter="{Binding}" Command="{Binding Path=DataContext.ContactBtnClickCommand, RelativeSource= {RelativeSource FindAncestor,AncestorType={x:Type ItemsControl}}}">Contact</Button>
                        <Button Margin="5" Width="100" CommandParameter="{Binding}" Command="{Binding DataContext.AcceptBtnClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}">Accept</Button>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

The class:

public class ToDoListMainVM : ViewModelBase
{
    private ObservableCollection<ToDoVM> listEntries;
    public ObservableCollection<ToDoVM> ListEntries
    {
        get { return listEntries; }
        set
        {
            listEntries = value;
            RaisePropertyChanged();
        }
    }

    SelectHandler selectHandler = new SelectHandler();
    InsertHandler insertHandler = new InsertHandler();
    DeleteHandler deleteHandler = new DeleteHandler();

    NavigationService navService = new NavigationService();

    public RelayCommand<ToDoVM> AcceptBtnClickCommand;
    public RelayCommand<ToDoVM> ContactBtnClickCommand;

    public ToDoListMainVM()
    {
        UpdateToDoList();

        AcceptBtnClickCommand = new RelayCommand<ToDoVM>((p) =>
        {
            //Enter into database
            insertHandler.InsertAppointmentToDatabase(new AppointmentVM()
            {
                Customer = p.Customer,
                Date = p.Date,
                Trainer = p.Trainer
            });
            //Make it instantly visible in the Calender
            Messenger.Default.Send<NewAppointmentMessage>(new NewAppointmentMessage(p.Customer, p.Date));

            //Delete from ToDo (View)
            ListEntries.Remove(p);

            //Delete from Db
            deleteHandler.DeleteToDo(p);

            //Set view to Calender
            navService.NavigateTo("MyTrainingsMain");

        });

View Model:

public class ToDoVM
{
    public int ToDoVMID { get; set; }
    public string RequestType { get; set; }
    public DateTime Date { get; set; }
    public CustomerVM Customer { get; set; }
    public TrainerVM Trainer { get; set; }
}
2
  • Try changing AncestorType to UserControl. The ItemsControl data context is ListEntries, and that does not contain the commands. You need to reference up the chain to the context that contains the commands. Commented Jun 28, 2017 at 14:22
  • 1
    @EdPlunkett, I just looked closer and was going to delete the comment Commented Jun 28, 2017 at 14:30

1 Answer 1

6

The command properties need to be properties, with a getter. You can't bind to a field.

    public RelayCommand<ToDoVM> AcceptBtnClickCommand { get; private set; }
    public RelayCommand<ToDoVM> ContactBtnClickCommand  { get; private set; }

The rest of your code is fine. The bindings are correct. You could simplify them slightly, but they work perfectly just the way you wrote them.

Command="{Binding DataContext.ContactBtnClickCommand, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.