简体   繁体   English

将带有构造函数的UserControl绑定到ViewModel

[英]Binding an UserControl with a constructor to ViewModel

Just started using Caliburn and WPF and got stuck on including an UserControl with a parameter to my Window. 刚开始使用Caliburn和WPF,并被困在将Window的参数作为UserControl的一部分。

Got one class named Item with a property named SellPrice which returns a Money object. 得到了一个名为Item类,该类具有名为SellPrice的属性,该属性返回Money对象。 I want to transfer this Money object to a UserControl to format the data in this object. 我想将此Money对象转移到UserControl以格式化该对象中的数据。 How do I transfer the object? 如何转移物品? Do I need the use the constructor? 我需要使用构造函数吗?

MainView.xaml MainView.xaml

<DataGridTemplateColumn Header="Sell Price">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
            <local:MoneyControlView/>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn>

Above code works fine if there wouldn't be any parameter but how do I pass the Money object? 如果没有任何参数,以上代码可以正常工作,但是如何传递Money对象呢?

I did try to do it via DependencyProperty as follows: 我确实尝试通过DependencyProperty做到这一点,如下所示:

public partial class MoneyControlView : UserControl
{
    public static readonly DependencyProperty MoneyProperty = DependencyProperty.Register
    (
            "Money",
            typeof(Money),
            typeof(MoneyControlView),
            new PropertyMetadata(new Money())
    );

    public Money Money
    {
        get { return (Money)GetValue(MoneyProperty); }
        set { SetValue(MoneyProperty, value); }
    }

    public MoneyControlView()
    {
        InitializeComponent();
        DataContext = new MoneyControlViewModel(Money);
    }
}

However, when using it: 但是,使用时:

<local:MoneyControlView Money="{Binding BuyPrice}"/>

it doesn't work. 它不起作用。 the Property remains empty. 该属性保持为空。

It isn't clear from your question how an Item relates to a MoneyControlViewModel . 从您的问题尚不清楚, ItemMoneyControlViewModel关系如何。 There are several potential changes to the listed code, though: 但是,对列出的代码有一些潜在的更改:

1) Change Collection<Item> to ObservableCollection<Item> 1)将Collection<Item>更改为ObservableCollection<Item>

2) Assuming the Item class has a Money property that returns a MoneyControlViewModel ... In the XAML, change the DataGridTemplateColumn: 2)假设Item类具有Money属性,该属性返回MoneyControlViewModel ...在XAML中,更改DataGridTemplateColumn:

<DataGridTemplateColumn Header="Sell Price">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
            <ContentControl x:Name="SellPrice" cal:View.Model="{Binding Money}"/>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn>

This part of your question is not clear: 您的问题的这一部分不清楚:

How do I implement the Money UserControl for each Item in my list? 如何为列表中的每个项目实现Money UserControl? Wouldn't be a problem if I got the current index of the DataGrid in order to retrieve the Money object from the Item... 如果我得到了DataGrid的当前索引以便从Item中检索Money对象,那将不是问题...

It looks like you might want an ItemsControl and use a DataTemplate with a MoneyControl. 看来您可能需要ItemsControl并将DataTemplate与MoneyControl一起使用。 This question might help: ItemsControl ItemTemplate Binding 这个问题可能会有所帮助: ItemsControl ItemTemplate绑定

在包含它的BuyPrice属性的类上,要求您实现INotifyPropertyChanged ,在BuyPriceSetter上,您必须引发实现的方法OnRaisedPropertyChanged("BuyPrice") (.Net 4.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM