0

I've been learning WPF in college this semester but there are still some things I do not fully understand. I have the following code:

<UserControl x:Class="Reversi.SquareControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="48" d:DesignWidth="48">
<Button Command="{Binding Place}" CommandParameter="{Binding ????????}">
    ...
</Button>

and:

    public partial class SquareControl : UserControl
{

    public SquareControl(int x, int y)
    {
        InitializeComponent();
        Coordinates = new Vector2D(x, y);
    }

    public Vector2D Coordinates
    {
        get { return (Vector2D) GetValue(CoordinateProperty); }
        set { SetValue(CoordinateProperty, value); }
    }

    ...

    public static readonly DependencyProperty CoordinateProperty =
        DependencyProperty.Register("Coordinates", typeof(Vector2D), typeof(SquareControl), new PropertyMetadata(new Vector2D(0, 0)));
}

My question is: what do I put in the CommandParameter binding to pass Coordinates to an ICommand in my ViewModel?

Exevan
  • 335
  • 1
  • 3
  • 24
  • If i put the command in the control, then how should i access a method from my viewmodel insode the command? Should i just pass along the viewmodel to the command's constructor? – Exevan Jun 16 '15 at 23:50
  • Well, you're creating a UserControl, so you normally wouldn't use a VM. With a user control, you contain all the properties and data inside it. – CoderForHire Jun 16 '15 at 23:52
  • The usercontrol is part of a larger project involving a given set of domain code. Maybe I should have explicitly stated that in my original question. – Exevan Jun 16 '15 at 23:55
  • Yes, but since you've already created a DP for the corrdinates, bind THAT to the VM in the parent view's XAML. – CoderForHire Jun 16 '15 at 23:55
  • Alright, since my experience with bindings is rather limited could you please provide an example of this (the parent of my control is a Grid in this case). – Exevan Jun 16 '15 at 23:58

1 Answers1

2
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mine="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <mine:SquareControl Coordinates="{Binding VMCoordinates, Mode=TwoWay}"/>
    </Grid>
</Window>

In this case the namespace 'mine' is the namespace of your app. So in the main window the line inside the grid says "Place an instance of SquareControl in my view and bind it's DP called Coordinates to a property on the VM called VMCoordinates"

The 'Mode=TwoWay' means if either the view (the user) or the VM changes the data, pass it it the other.

CoderForHire
  • 121
  • 1
  • 11
  • 35
  • Thanks, your solution made me realize what I was doing wrong. Instead of binding the coordinates to a corresponding property in my viewmodel I was somehow trying to bind it to itself (shows how new I am to this). I also realize I do not need to store the coordinates inside the control at all, I can just store them in the control's viewmodel since they should be readonly anyway. – Exevan Jun 17 '15 at 00:10
  • You're welcome, but I think you should reconsider your design. Typically a UserControl does not have a view model. Think of a third party control you buy. It does not come with a VM. You interact with it through DP's. When you're ready to use it you place it inside another view like in my example and bind it's exposed DP's to the parent. I Typically have a window or parent view that has VM's, but they talk to the user controls via DP binding. Your user control should be stand alone and not need a VM. – CoderForHire Jun 17 '15 at 00:12
  • Good point, I will try to rework my code so all DP's pass through the central viewmodel. – Exevan Jun 17 '15 at 00:20