0

这个学期我在大学里一直在学习WPF,但还有一些我不完全理解的东西。我有以下代码:

<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>

和:

    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)));
}

我的问题是:我在CommandParameter绑定中放入什么以传递Coordinates给 ViewModel 中的 ICommand?

4

1 回答 1

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>

在这种情况下,命名空间“我的”是您的应用程序的命名空间。因此,在主窗口中,网格内的行显示“在我的视图中放置一个 SquareControl 的实例,并将其名为 Coordinates 的 DP 绑定到 VM 上名为 VMCoordinates 的属性”

'Mode=TwoWay' 意味着如果视图(用户)或 VM 更改了数据,则将其传递给另一个。

于 2015-06-17T00:02:33.213 回答