XAML 如何在wpfmvvm模式中更新usercontrol视图模型的父视图模型属性?

ryevplcw  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(163)

我正在创建一个WPF应用程序使用MVVM模式与棱镜在.net核心6。
在发票窗口中,用户可以附加支持文档,并且此附加文档界面被创建为用户控件。
当用户附加一个文档时,我需要将该文档的详细信息添加到父窗口(发票)视图模型属性。
我在用户控件中创建了依赖属性,

public partial class DocumentList : UserControl
{
    public DocumentList()
    {
        InitializeComponent();
    }

public ObservableCollection<Attachment> Attachments
{
    get { return (ObservableCollection<Attachment>)GetValue(AttachmentsProperty); }
    set { SetValue(AttachmentsProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty AttachmentsProperty =
    DependencyProperty.Register(nameof(Attachments), typeof(ObservableCollection<Attachment>), typeof(DocumentList), new PropertyMetadata(null));

public bool IsAttachmentDownloadable
{
    get { return (bool)GetValue(IsAttachmentDownloadableProperty); }
    set { SetValue(IsAttachmentDownloadableProperty, value); }
}

// Using a DependencyProperty as the backing store for IsAttachmentDownloadable.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsAttachmentDownloadableProperty =
    DependencyProperty.Register("IsAttachmentDownloadable", typeof(bool), typeof(DocumentList), new PropertyMetadata(true));

}
我的usercontrol视图模型有属性Attachments

private ObservableCollection<Attachment> _attachments;

public ObservableCollection<Attachment> Attachments
{
    get { return _attachments; }
    set { SetProperty(ref _attachments, value); }
}

此附件集合绑定到网格视图

<syncfusion:SfDataGrid 
            ItemsSource="{Binding Attachments}"
            x:Name="dataGrid"  
            MaxHeight="100"
                Grid.Row="2"
            AutoGenerateColumns="False"
            SelectionUnit="Row"
            AllowSorting="True"
                ColumnSizer="AutoLastColumnFill"
                VerticalAlignment="Top"
                SelectedItem="{Binding SelectedAttachment}">
                <syncfusion:SfDataGrid.Columns>
                    <syncfusion:GridTextColumn HeaderText="Id" MappingName="Id" IsHidden="True"></syncfusion:GridTextColumn>
                    <syncfusion:GridTextColumn HeaderText="File name" MappingName="Name" Width="400"></syncfusion:GridTextColumn>
                    <syncfusion:GridTextColumn HeaderText="Description" MappingName="Description" MinimumWidth="200" Width="438" IsReadOnly="False"></syncfusion:GridTextColumn>
                </syncfusion:SfDataGrid.Columns>
            </syncfusion:SfDataGrid>

还有另一个用户控件,它上传附件到服务器并更新此附件集合,它工作正常,网格按预期更新。
问题是我需要将这个usercontrol Attachments属性绑定到父视图模型属性Attachments
我的父母视图模型

private ObservableCollection<Attachment> _attachments;

public ObservableCollection<Attachment> Attachments
{
    get { return _attachments; }
    set { SetProperty(ref _attachments, value); }
}

当从父窗口调用用户控件时,我有下面的绑定

<usrctrl:DocumentList HorizontalAlignment="Stretch" 
                                      Margin="0,10,0,0" 
                                      IsAttachmentDownloadable="False"
                                      Attachments="{Binding DataContext.Attachments, 
                    RelativeSource={RelativeSource AncestorType={x:Type local:Expense_Add}}, Mode=TwoWay}"/>

正如我刚才所说的问题是上面的约束力是不工作的预期,可以任何一个帮助我解决这个问题?
谢谢

bvuwiixz

bvuwiixz1#

DocumentListUserControl不应该有自己的DataContext。它应该从其父元素继承DataContext
然后,您可以直接绑定到窗口中父视图模型的Attachments属性:

<usrctrl:DocumentList ... Attachments="{Binding Attachments}"/>

UserControl中,您将像这样绑定到本地Attachments属性:

<syncfusion:SfDataGrid 
            ItemsSource="{Binding Attachments,
                RelativeSource={RelativeSource AncestorType=UserControl}}"

相关问题