如何使用SoapBox将数据从View绑定到UserControl [英] How to bind data from View to UserControl with SoapBox

查看:69
本文介绍了如何使用SoapBox将数据从View绑定到UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的SoapBox.Document'Register'

I have my SoapBox.Document 'Register'

[Export(SoapBox.Core.ExtensionPoints.Workbench.Documents, typeof(IDocument))]
[Export(CompositionPoints.Workbench.Documents.Register, typeof(Register))]
[Document(Name = DOC_NAME)]

class Register : AbstractDocument
{
    public Receipt actualReceipt;
    private const string DOC_NAME = "Register";
    public Register()
    {
        Name = DOC_NAME;
        Title = "Recipe Document Title";
        SomeProperty = "Hello from the recipe document!";
    }
}

在本文档中,我要用户UserControls属于自己的视图" 就像所有ReceiptPositions的ListView一样

In this Document I want to user UserControls witch are kind of a own "View" Like a ListView for all ReceiptPositions

所以现在我有了我的模型收据和ReceiptPosition

So now I got my Model Receipt and ReceiptPosition

模型收据

class Receipt
{
    public int Id { get; set; }
    public string Receiptnumber { get; set; }
    public IList<ReceiptPositions> ReceiptPositions { get; set; }

和模型收据位置

class ReceiptPosition
{
    public int Id { get; set; }
    //public Receipt Receipt { get; set; } using for Database 
    public int Position { get; set; }
    public string Article { get; set; }
}

因此,现在我想添加一个UserControl,以便在ReceiptPositions中显示所有文章的列表.

So now I want to add a UserControl witch displays a List of all articles in ReceiptPositions.

但是如何绑定数据,以便在将新的ReceiptPosition添加到Receipt中的IList时,UserControl会自动刷新"?

But how do I bind the data so that when a new ReceiptPosition gets added to the IList in Receipt the UserControl get 'refreshed' automatically?

这是我需要的视觉示例.

Here is a visual example of whatI need..

带有数据的主机和两个PLugins各自显示相同的数据,但是方式不同.

推荐答案

您可以为此使用ItemsControl.

xaml:

<ItemsControl ItemsSource="{Binding MyReceipt.ReceiptPositions}">

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- Where you put your view -->
            <TextBox Text="{Binding Article}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- Can be whatever Panel type you want -->
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

</ItemsControl>

cs:

private Receipt _myReceipt;
public Receipt MyReceipt { get { return _myReceipt; } set { _myReceipt = value; OnPropertyChanged("MyReceipt"); } } 

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
    MyReceipt = new Receipt { ReceiptPositions = new ObservableCollection<ReceiptPosition>() };

    MyReceipt.ReceiptPositions.Add(new ReceiptPosition { Article = "Foo" });
    MyReceipt.ReceiptPositions.Add(new ReceiptPosition { Article = "Bar" });
    MyReceipt.ReceiptPositions.Add(new ReceiptPosition { Article = "Baz" });

    MyReceipt.ReceiptPositions[0].Article = "Frabazamataz";
}

说明:

ItemsControl允许您将列表绑定到其ItemsSource属性,以用作由DataTemplate创建的每个视图的DataContext.

The ItemsControl allows you to bind a list to its ItemsSource Property to use as the DataContext to each view created by the DataTemplate.

Observable集合会在添加,删除或更改每个项目时自动提供PropertyChange通知.

The Observable Collection gives PropertyChange notifications automatically with each item added, removed, or changed.

这使您可以仅根据数据获得非常灵活的项目列表.

This allows you to have a very flexible list of items based solely on your data.

这篇关于如何使用SoapBox将数据从View绑定到UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆