0

Just started using Caliburn and WPF and got stuck on including an UserControl with a parameter to my Window.

Got one class named Item with a property named SellPrice which returns a Money object. I want to transfer this Money object to a UserControl to format the data in this object. How do I transfer the object? Do I need the use the constructor?

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?

I did try to do it via DependencyProperty as follows:

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.

0x8BADF00D
  • 962
  • 1
  • 8
  • 18

2 Answers2

0

It isn't clear from your question how an Item relates to a MoneyControlViewModel. There are several potential changes to the listed code, though:

1) Change Collection<Item> to ObservableCollection<Item>

2) Assuming the Item class has a Money property that returns a MoneyControlViewModel... In the XAML, change the 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? Wouldn't be a problem if I got the current index of the DataGrid in order to retrieve the Money object from the Item...

It looks like you might want an ItemsControl and use a DataTemplate with a MoneyControl. This question might help: ItemsControl ItemTemplate Binding

Community
  • 1
  • 1
Ryan
  • 7,835
  • 2
  • 29
  • 36
  • Hi, sorry in being so unclear, completely updated my question. I think that your solution is based on a different problem. – 0x8BADF00D Apr 11 '14 at 17:34
  • You are iterating over `Items`. The binding you have specified indicates each `Item` has a `SellPrice` property of type `Money`, e.g. `Item.SellPrice`. For binding issues, you should really look into Snoop WPF: https://snoopwpf.codeplex.com/ – Ryan Apr 11 '14 at 18:35
  • That's correct, each `Item` got this property. I don't see the point why it won't work. – 0x8BADF00D Apr 11 '14 at 18:47
  • Well, after I spent some more time on this problem, I found out that the binding was the problem. I did specify the binding of the DataGrid so the binding on the user control was wrong. Anyway, thanks for helping me out and giving some useful tips. – 0x8BADF00D Apr 11 '14 at 19:57
0

On the class that your BuyPrice property it contains, requires you to implement INotifyPropertyChanged and on the Setter of the BuyPrice you have to raise the implemented method OnRaisedPropertyChanged("BuyPrice") (.Net 4.0).

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72