A
A
Amffore2018-09-20 13:38:04
WPF
Amffore, 2018-09-20 13:38:04

How to make User Control and MVVM friends?

As part of my coursework, I am making an application in WPF.
At the moment, I have a window like this:
5ba36e9880236071111134.png
In the black frame, this is the user control, which will be discussed further. It consists of two labels, one textblock, image, button. The problem is that I can't figure out how to bind my user control to the ViewModel.
According to the examples in Google, it turns out just like this

private Tour selectedTour;

        public ObservableCollection<Tour> Tours { get; set; }
        public Tour SelectedTour
        {
            get { return selectedTour; }
            set
            {
                selectedTour = value;
                OnPropertyChanged("SelectedTour");

            }
        }

        public TourViewModel()
        {
            Tours = new ObservableCollection<Tour>
            {
                new Tour {Title="Тур по Сочи", Description="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", Price=30000.00},
                new Tour {Title="Пляжи Анапы", Description="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", Price=40000.00},
                new Tour {Title="Тур по Москве", Description="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", Price=25000.00},
                new Tour {Title="Тур по Санкт-Петербургу", Description="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", Price=50000.00}
            };
        }

and here I wrap my user control in a label
<StackPanel Grid.Row="1" HorizontalAlignment="Center">
        <ListBox ItemsSource="{Binding Tours}" SelectedItem="{Binding SelectedTour}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <cntrl:TourBlockControl Title="{Binding Path=Title}" Price="{Binding Path=Price}" ImageSource="/WPFTourFirmApp;component/Images/1.png"  Description="{Binding Path=Description}"></cntrl:TourBlockControl>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        </StackPanel>

but this is far from what I would like to see. The only thing that comes to mind is that the usercontrol needs to be finalized by adding properties similar to the label.
UPD. I would like that it would be to the usercontrol (TourBlockControl) that the binding from the ViewModel would go.
Tell me in which direction to dig and what to get acquainted with.
In order not to throw off a wall of code here, you can check it out here:
https://github.com/Amffore/WPFTourFirmApp
UPD2. I came to the conclusion that in this case, your user control is simply not needed. After all, there is the same ListBox that suits me perfectly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2018-09-20
@Amffore

At least the tour must have title and price properties, once there is a binding

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question