如何让这个CommandParameter工作?

时间:2015-06-16 23:43:14

标签: c# wpf xaml binding commandparameter

本学期我一直在大学学习WPF,但仍有一些我不太了解的事情。我有以下代码:

$KCODE

<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传递给我的ViewModel中的ICommand?

1 个答案:

答案 0 :(得分: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>

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

的属性

&#39; Mode = TwoWay&#39;表示如果视图(用户)或VM更改数据,则将其传递给另一个。