I made a Page MainPage
and a UserControl Pager
. Both have their ViewModel. In Pager
, there are three dependency properties Rows
, Columns
, Source
. I want to pass these properties from Pager
's View to Pager
's ViewModel. I tried this on View's code behind. But it doesn't work...the set
property in PagerViewModel
never be called on debugging. Please, help me...
Here is detail mechanism:
MainPageViewModel
↓Pass the values with binding
MainPage
↓Set tht properties with values from MainPagerViewModel
Pager
(code behind)
↓Bind the properties to PagerViewModel <--- This part is PROBLEM!!!
PagerViewModel
↓Pass the values with binding
Pager
(XAML)
and here is source
[MainPageViewModel.cs]
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Client.Model;
namespace Client.ViewModel
{
public class MainPageViewModel : ViewModelBase
{
...
public ObservableCollection<IPagableEntry> PagerTableCategoriesItems { get { return TableCategoryRepository.Instance.TableCategories; } }
public int PagerTableCategoriesRows { get { return 1; } }
public int PagerTableCategoriesColumns { get { return 3; } }
...
}
}
[MainPage.xaml]
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:Client.View"
xmlns:viewModel="clr-namespace:Client.ViewModel"
xmlns:resStr="clr-namespace:Client.CommonResources.String"
x:Class="Client.View.MainPage"
Style="{StaticResource common}">
<Page.DataContext>
<viewModel:MainPageViewModel />
</Page.DataContext>
...
<view:Pager x:Name="pagerTableCategories"
Source="{Binding Path=PagerTableCategoriesItems}"
Rows="{Binding Path=PagerTableCategoriesRows}"
Columns="{Binding Path=PagerTableCategoriesColumns}">
</view:Pager>
...
</Page>
[Pager.xaml.cs]
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using Client.Model;
using Client.ViewModel;
namespace Client.View
{
public partial class Pager
{
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ObservableCollection<IPagableEntry>), typeof(Pager), new PropertyMetadata(null, OnSourceChanged));
public static readonly DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(int), typeof(Pager), new PropertyMetadata(1, OnRowsChanged));
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(int), typeof(Pager), new PropertyMetadata(1, OnColumnsChanged));
public static readonly DependencyProperty SelectedEntryProperty = DependencyProperty.Register("SelectedEntry", typeof(object), typeof(Pager), new PropertyMetadata(null, OnSelectedEntryChanged));
public int Rows
{
get { return (int)GetValue(RowsProperty); }
set { SetValue(RowsProperty, value); }
}
public int Columns
{
get { return (int)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
public object SelectedEntry
{
get { return GetValue(SelectedEntryProperty); }
set { SetValue(SelectedEntryProperty, value); }
}
public ObservableCollection<IPagableEntry> Source
{
get { return (ObservableCollection<IPagableEntry>)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public Pager()
{
InitializeComponent();
// I want to bind the three custom properties(Rows, Columns, Source) to PagerViewModel's Rows, Columns, Collection
Binding bindingRows = new Binding("Rows");
bindingRows.Mode = BindingMode.TwoWay;
bindingRows.Source = gridPager.DataContext;
gridPager.SetBinding(RowsProperty, bindingRows);
Binding bindingColumns = new Binding("Columns");
bindingColumns.Mode = BindingMode.TwoWay;
bindingColumns.Source = gridPager.DataContext;
gridPager.SetBinding(ColumnsProperty, bindingColumns);
Binding bindingSource = new Binding("Collection");
bindingSource.Mode = BindingMode.TwoWay;
bindingSource.Source = gridPager.DataContext;
gridPager.SetBinding(SourceProperty, bindingSource);
}
private void ListBoxEntriesOnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedEntry = (sender as ListBox).SelectedItem;
}
private static void OnSelectedEntryChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
(pager as Pager).SelectedEntry = e.NewValue;
}
private static void OnSourceChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
(pager as Pager).Source = (ObservableCollection<IPagableEntry>)e.NewValue;
}
private static void OnRowsChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
(pager as Pager).Rows = (int)e.NewValue;
}
private static void OnColumnsChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
(pager as Pager).Columns = (int)e.NewValue;
}
}
}
[Pager.xaml]
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:Client.View"
xmlns:viewModel="clr-namespace:Client.ViewModel"
xmlns:resStr="clr-namespace:Client.CommonResources.String"
x:Class="Client.View.Pager">
<Grid x:Name="gridPager">
<Grid.DataContext>
<viewModel:PagerViewModel />
</Grid.DataContext>
...
<ListBox x:Name="listBoxEntries"
ItemsSource="{Binding Path=Collection}"
BorderThickness="0"
Margin="0"
Style="{StaticResource common}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
ItemTemplate="{StaticResource templateTableCategory}"
SelectedItem="{Binding Path=SelectedEntry, Mode=TwoWay}"
SelectionChanged="ListBoxEntriesOnSelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding Path=Rows}"
Columns="{Binding Path=Columns}"
IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
...
</Grid>
</UserControl>
[PagerViewModel.cs]
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Data;
using System.Windows.Media;
using Client.Model;
namespace Client.ViewModel
{
public class PagerViewModel : ViewModelBase
{
...
ListCollectionView _listCollectionView;
ObservableCollection<IPagableEntry> _collection;
int _rows;
int _columns;
public int Rows
{
get { return _rows; }
set
{
_rows = value;
OnPropertyChanged();
}
}
public int Columns
{
get { return _columns; }
set
{
_columns = value;
OnPropertyChanged();
}
}
public ListCollectionView ListCollectionView
{
get { return _listCollectionView; }
set
{
_listCollectionView = value;
OnPropertyChanged();
}
}
public ObservableCollection<IPagableEntry> Collection
{
get
{
return _collection;
}
set
{
_collection = value;
OnPropertyChanged();
}
}
...
}
}