Yet another databinding issue in silverlight
I know this topic has been discussed, but not by me yet. As I have seen on other examples about this issue, I am trying to create some basic custom DataPager
UserControl
. So that I did the following :
XAML:
<ComboBox Name="Size" ItemsSource="{Binding PageSourceSize}"
SelectedValue="{Binding PageSizePager}" />
With the following C#:
ObservableCollection<int> _PageSourceSize;
public ObservableCollection<int> PageSourceSize
{
get { return _PageSourceSize; }
set
{
_PageSourceSize = value;
RaisePropertyChanged("PageSourceSize");
}
}
public MyDataPager()
{
DataContext = this;
PageSizePager = 10;
PageSourceSize = new ObservableCollection<int>() { 10, 20, 50,100 };
}
public int PageSizePager
{
get { return (int)GetValue(PageSizePagerProperty); }
set { SetValue(PageSizePagerProperty, value); }
}
public static readonly DependencyProperty PageSizePagerProperty =
DependencyProperty.Register("PageSizePager", typeof(int), typeof(MyDataPager), new PropertyMetadata(10));
From here I intend to use my pager in a main UserControl
:
<local:MyDataPager PageSizePager="20" x:Name="MyDataPager1" />
This works fine, but I would have liked to get the value from my viewModel
using:
<local:MyDataPager PageSizePager="{Binding Path=PageSize,Mode=TwoWay}" x:Name="MyDataPager1" />
And the view model:
public int PageSize
{
get { return (int)GetValue(PageSizeProperty); }
set { SetValue(PageSizeProperty, value); }
}
public static readonly DependencyProperty PageSizeProperty =
DependencyProperty.Register("PageSize", typeof(int), typeof(ViewSchedeConsuntiviViewModel), new PropertyMetadata(10));
public MyViewModel()
{
PageIndex = 1;
PageSize = 20;
}
Could someone explain me why the binding between the view model and the user control does n开发者_如何学运维ot work?
Looking on the code it seems that you have more then one PageSize
properties defined in different classes. And most probabbly, it's difficult to understand just by looking on code provided, you bind in XAML
one property, but change the value of another one, instead. Vary the name of one of PageSize
properties to be sure where exactly databinding going to read/write.
I think this should help.
Working with a colleague of mine, we found a solution for what I intended to do :
In the Xaml of the MyDataPager usercontrol :
<Grid x:Name="LayoutRoot" Background="White" Loaded="MyDataPager_Loaded">
....
going with this definition of MyDataPager_Loaded :
void MyDataPager_Loaded(object sender, RoutedEventArgs e)
{
((Grid)sender).DataContext = this;
}
From the code above we have changed the ctor of the MyDataPager usercontrol to remove the datacontext binding :
public MyDataPager()
{
//DataContext = this;
Working this way, I am able to bind values in the main usercontrol like this :
<local:MyDataPager PageSizePager="{Binding Path=PageSize,Mode=TwoWay}" x:Name="MyDataPager1" />
So that the binding is made upon properties of the childusercontrol, not upon its control(ie: look of the child control may change without problems), and so that the child usercontrol does not have to use any "known" values from the datacontext of the main usercontrol.
Thanks for reading and for your support, it was greatly welcomed.I Hope these lines might serve another one in need of this.
精彩评论