How can i make this CommandParameter work?

131 views Asked by At

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?

1

There are 1 answers

3
CoderForHire On BEST ANSWER
<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.