1
我的解决方案在MVVM中实现。该视图是托管用户控件的窗口。我已经创造了这个用户控件,如下一个依赖属性:UserControl中的依赖属性绑定
public static DependencyProperty ListProperty = DependencyProperty.Register(
"ItemsList", typeof(List<RequiredClass>), typeof(UsercontrolTest));
public List<RequiredClass> ItemsList
{
get { return (List<RequiredClass>)GetValue(ListProperty); }
set
{
SetValue(ListProperty, value);
}
}
此属性在XAML绑定到我的视图模型属性(LISTOFITEMS):
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Test="clr-namespace:TestProject"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Test:UserControlTest Grid.Row="0" ItemsList="{Binding Path=ListOfItems}" />
<Button Grid.Row="1" Content="AddItems" Click="Button_Click" />
</Grid>
</Window>
而且我已经初始化的窗口中的datacontext代码隐藏到视图模型中。问题是绑定永远不会发生,set属性永远不会调用依赖项属性。我在这里错过了什么吗?
邦正在发生什么事情。因为我不能把代码放在setter中,任何想法如何识别我的列表是否被更改并执行一些代码? – 2012-02-05 06:48:16
@AabidAli:您可以使用['ObservableCollection'](http://msdn.microsoft.com/en-us/library/ms668604.aspx)在更改时触发事件,但不确定您尝试执行的操作... – 2012-02-05 06:56:45
@ HB我的意思是依赖属性的变化。 Np我知道了,你只需要在初始化dp时添加一个Property Metadata来处理propertychanged。感谢您的帮助 – 2012-02-05 07:50:41