集合依赖属性 [英] collection dependency properties
问题描述
我有一个自定义控件,它具有 DependencyProperty
类型 ObservableCollection
,绑定到一个 observableCollection
:
< MyControl MyCollectionProperty = {Binding MyObservableCollection} ...
问题是添加到 MyObservableCollection
不更新 MyCollectionProperty
。
我需要完全替换 MyObservableCollection
工作如
MyObservableCollection = null;
MyObservableCollection = new ObservableCollection(){...}
有更好的方法处理这个?
编辑:
public ObservableCollection< string>列
{
get {return(ObservableCollection< string>)GetValue(ColumnsProperty); }
set {SetValue(ColumnsProperty,value);
public static readonly DependencyProperty ColumnsProperty =
DependencyProperty.Register(Columns,typeof(ObservableCollection< string>),typeof(MyControl),
new PropertyMetadata(new ObservableCollection< string>(),OnChanged));
下面是一个可以帮助的工作示例。 p>
在此示例中,立即调用OnChanged方法,当添加按钮被点击时,更改被写入控制台。
控件
public class MyControl:Control
{
public ObservableCollection< ;字符串> ExtraColumns
{
get {return(ObservableCollection< string>)GetValue(ExtraColumnsProperty); }
set {SetValue(ExtraColumnsProperty,value); }
public static readonly DependencyProperty ExtraColumnsProperty =
DependencyProperty.Register(ExtraColumns,typeof(ObservableCollection< string>),typeof(MyControl),
new PropertyMetadata(new ObservableCollection< string>(),OnChanged));
static void OnChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
{
(发件人为MyControl).OnChanged();
}
void OnChanged()
{
if(ExtraColumns!= null)
ExtraColumns.CollectionChanged + = new System.Collections .Specialized.NotifyCollectionChangedEventHandler(ExtraColumns_CollectionChanged);
}
void ExtraColumns_CollectionChanged(object sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine(Changed);
}
}
窗口
< Window x:Class =WpfApplication18.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/演示
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local =clr-namespace:WpfApplication18
Title =MainWindow Height =350Width =525>
< StackPanel>
< local:MyControl ExtraColumns ={Binding Extras}/>
< Button Click =Button_Click>添加< / Button>
< / StackPanel>
< / Window>
后面的窗口代码
public partial class MainWindow:Window
{
private ObservableCollection< string> _extras = new ObservableCollection< string>();
public ObservableCollection< string>其他
{
get {return _extras; }
set
{
if(value!= _extras)
{
_extras = value;
}
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender,RoutedEventArgs e)
{
Extras.Add(Additional);
}
}
I have a custom control that has a DependencyProperty
of type ObservableCollection
that is bound to an observableCollection
:
<MyControl MyCollectionProperty = {Binding MyObservableCollection} ...
Problem is adding to MyObservableCollection
does not update MyCollectionProperty
.
I need to completly replace the MyObservableCollection
to make it work e.g.
MyObservableCollection = null;
MyObservableCollection = new ObservableCollection(){...}
Is there a better way to deal with this?
EDIT:
public ObservableCollection<string> Columns
{
get { return (ObservableCollection<string>)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
public static readonly DependencyProperty ColumnsProperty =
DependencyProperty.Register("Columns", typeof(ObservableCollection<string>), typeof(MyControl),
new PropertyMetadata(new ObservableCollection<string>(), OnChanged));
Below is a working example that may help.
In this example, the method OnChanged is called immediately, when the Add button is clicked "Changed" is written to the console.
The Control
public class MyControl : Control
{
public ObservableCollection<string> ExtraColumns
{
get { return (ObservableCollection<string>)GetValue(ExtraColumnsProperty); }
set { SetValue(ExtraColumnsProperty, value); }
}
public static readonly DependencyProperty ExtraColumnsProperty =
DependencyProperty.Register("ExtraColumns", typeof(ObservableCollection<string>), typeof(MyControl),
new PropertyMetadata(new ObservableCollection<string>(), OnChanged));
static void OnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as MyControl).OnChanged();
}
void OnChanged()
{
if ( ExtraColumns != null )
ExtraColumns.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ExtraColumns_CollectionChanged);
}
void ExtraColumns_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("Changed");
}
}
The Window
<Window x:Class="WpfApplication18.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication18"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<local:MyControl ExtraColumns="{Binding Extras}"/>
<Button Click="Button_Click">Add</Button>
</StackPanel>
</Window>
Window Code Behind
public partial class MainWindow : Window
{
private ObservableCollection<string> _extras = new ObservableCollection<string>( );
public ObservableCollection<string> Extras
{
get { return _extras; }
set
{
if (value != _extras)
{
_extras = value;
}
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Extras.Add("Additional");
}
}
这篇关于集合依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!