带有视图模型的WPF用户控件

带有视图模型的WPF用户控件,wpf,viewmodel,Wpf,Viewmodel,我创建了一个我自己的用户控件包装一个GridControl,并定义了一些列,所以我可以在多个地方重用这个控件。我在用户控件中创建了一个名为AttachmentList的依赖属性,以保存要在网格中显示的字符串列表 <dxg:GridControl AutoGenerateColumns="None" EnableSmartColumnsGeneration="True" ItemsSource="{Binding Path=AttachmentLi

我创建了一个我自己的用户控件包装一个GridControl,并定义了一些列,所以我可以在多个地方重用这个控件。我在用户控件中创建了一个名为AttachmentList的依赖属性,以保存要在网格中显示的字符串列表

<dxg:GridControl AutoGenerateColumns="None" EnableSmartColumnsGeneration="True" 
                     ItemsSource="{Binding Path=AttachmentList, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, UpdateSourceTrigger=PropertyChanged}"/>
我使用我的父窗口

<vm:FileListGridControl AllowDrop="True"
                     AttachmentList="{Binding Path=Email.FileList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    <dxmvvm:Interaction.Behaviors>
                        <vm:GridDragDropBehavior/>
                    </dxmvvm:Interaction.Behaviors>
</vm:FileListGridControl>
Email.FileList在我的ViewModel中定义为ObservableCollection。GridDragDropBehavior将调用Email.FileList.Add向Email.FileList添加字符串。问题是我可以看到字符串已经添加到文件列表中,但没有显示在GridControl中

我认为使用ObserableCollection和双向绑定应该可以更新UI。我做错了什么

编辑这里是定义

public static readonly DependencyProperty AttachmentListProperty = DependencyProperty.Register("AttachmentList", typeof(IList<FileWrapper>), typeof(FileListGridControl));

public IList<FileWrapper> AttachmentList
{
    get { return (IList<FileWrapper>)GetValue(AttachmentListProperty); }
    set { SetValue(AttachmentListProperty, value); }
}
在我的电子邮件课上,我定义了

ObservableCollection<FileWrapper> _FileList = new ObservableCollection<FileWrapper>();
public ObservableCollection<FileWrapper> FileList { get { return _FileList; } set { _FileList = value; } }

如果我删除我的控件并直接在父窗口中添加GridControl,则一切正常。

@Clemens我已经更新了我的帖子。关于双向绑定,我应该使用哪种模式?我正在将文件拖放到GridControl中,GridControl会将文件名添加到集合中,并将更改通知UI。您使用的绑定模式取决于您的要求。有关更多信息,请参阅MSDN页面的“数据流方向”部分。根本不需要设置模式或UpdateSourceTrigger。但是,请确保将FileListGridControl的DataContext属性设置为声明电子邮件属性的视图模型类的实例。为什么FileList是读/写属性?它真的设置在什么地方吗?如果是这样,则必须发出属性更改通知,即实现INotifyPropertyChanged接口。@Clements ObservableCollection应在向集合添加/删除任何内容时通知UI。我认为我的附件列表应该是可观察的,而不是ILST