讨教,讨教,大神解答一下,效率啊,功能啊,性能啊,实现方式啊
解决方案
40
例子:
实现一个自定义控件 class UserControl1 : UserControl
包含一个依赖属性, public List<MovePic> ImageList
{
get { return (List<MovePic>)GetValue(ImageListProperty); }
set { SetValue(ImageListProperty , value); }
}
// Using a DependencyProperty as the backing store for ImageList. This enables animation, styling, binding, etc…
public static readonly DependencyProperty ImageListProperty =
DependencyProperty.Register(“ImageList” , typeof(List<MovePic>) , typeof(UserControl1));
注意:UserControl是继承自DependencyObject,所以UserControl1 也就继承DependencyObject了。这个你可以在VS中查看。
在MainWindow的XAML中,
<Button HorizontalAlignment=”Right” VerticalAlignment=”Top” Click=”Button_Click”>test</Button>
<local:UserControl1 ImageList=”{Binding list}”></local:UserControl1>
在MainWindow的cs中:
private List<MovePic> _list;
public List<MovePic> list
{
get { return _list; }
set { _list = value; }
}
public MainWindow( )
{
InitializeComponent( );
list = new List<MovePic>( );
list.Add(new MovePic(“http://banbao.chazidian.com/uploadfile/2016-01-25/s145368924044608.jpg” , 0 , 0));
list.Add(new MovePic(“http://pic36.nipic.com/20131217/6704106_233034463381_2.jpg” , 300 , 300));
this.DataContext = this;
}
private void Button_Click(object sender , RoutedEventArgs e)
{
list = new List<MovePic>( );
list.Add(new MovePic(“http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg” , 100 , 200));
list.Add(new MovePic(“http://img05.tooopen.com/images/20140604/sy_62331342149.jpg” , 500 , 200));
OnPropertyChanged(“list”);
}
可以看到,本人将UserControl1的依赖属性ImageList绑定到了MainWindow的list,并且在MainWindow的按钮点击事件中更改了list,假如你删除 OnPropertyChanged(“list”);这个行代码,那么UserControl1的ImageList是不会变化的。
而OnPropertyChanged(“list”);这个就是实现了INotifyPropertyChanged接口后调用的方法。
实现一个自定义控件 class UserControl1 : UserControl
包含一个依赖属性, public List<MovePic> ImageList
{
get { return (List<MovePic>)GetValue(ImageListProperty); }
set { SetValue(ImageListProperty , value); }
}
// Using a DependencyProperty as the backing store for ImageList. This enables animation, styling, binding, etc…
public static readonly DependencyProperty ImageListProperty =
DependencyProperty.Register(“ImageList” , typeof(List<MovePic>) , typeof(UserControl1));
注意:UserControl是继承自DependencyObject,所以UserControl1 也就继承DependencyObject了。这个你可以在VS中查看。
在MainWindow的XAML中,
<Button HorizontalAlignment=”Right” VerticalAlignment=”Top” Click=”Button_Click”>test</Button>
<local:UserControl1 ImageList=”{Binding list}”></local:UserControl1>
在MainWindow的cs中:
private List<MovePic> _list;
public List<MovePic> list
{
get { return _list; }
set { _list = value; }
}
public MainWindow( )
{
InitializeComponent( );
list = new List<MovePic>( );
list.Add(new MovePic(“http://banbao.chazidian.com/uploadfile/2016-01-25/s145368924044608.jpg” , 0 , 0));
list.Add(new MovePic(“http://pic36.nipic.com/20131217/6704106_233034463381_2.jpg” , 300 , 300));
this.DataContext = this;
}
private void Button_Click(object sender , RoutedEventArgs e)
{
list = new List<MovePic>( );
list.Add(new MovePic(“http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg” , 100 , 200));
list.Add(new MovePic(“http://img05.tooopen.com/images/20140604/sy_62331342149.jpg” , 500 , 200));
OnPropertyChanged(“list”);
}
可以看到,本人将UserControl1的依赖属性ImageList绑定到了MainWindow的list,并且在MainWindow的按钮点击事件中更改了list,假如你删除 OnPropertyChanged(“list”);这个行代码,那么UserControl1的ImageList是不会变化的。
而OnPropertyChanged(“list”);这个就是实现了INotifyPropertyChanged接口后调用的方法。