Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何使这个CommandParameter工作?_C#_Wpf_Xaml_Binding_Commandparameter - Fatal编程技术网

C# 我如何使这个CommandParameter工作?

C# 我如何使这个CommandParameter工作?,c#,wpf,xaml,binding,commandparameter,C#,Wpf,Xaml,Binding,Commandparameter,这学期我在大学里一直在学习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

这学期我在大学里一直在学习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>
我的问题是:我应该在
CommandParameter
绑定中添加什么来将
坐标传递给ViewModel中的ICommand?


<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>
在本例中,名称空间“mine”是应用程序的名称空间。因此,在主窗口中,网格内的行显示“在我的视图中放置SquareControl的实例,并将其称为坐标的DP绑定到VM上称为VMCoordinates的属性”


“Mode=TwoWay”表示如果视图(用户)或虚拟机更改了数据,则将其传递给另一个。

updated-如果我将命令放入控件中,那么我应该如何从命令中的viewmodel访问方法?我是否应该将viewmodel传递给命令的构造函数?好吧,您正在创建一个UserControl,因此通常不会使用VM。对于用户控件,您可以包含其中的所有属性和数据。用户控件是涉及给定域代码集的大型项目的一部分。也许我应该在我最初的问题中明确指出这一点。是的,但是因为您已经为相关的数据创建了DP,所以请将其绑定到父视图的XAML中的VM。谢谢,您的解决方案让我意识到我做错了什么。我没有将坐标绑定到viewmodel中的相应属性,而是尝试将其绑定到自身(显示了我对此有多么陌生)。我还意识到我根本不需要在控件中存储坐标,我可以将它们存储在控件的viewmodel中,因为它们应该是只读的。不客气,但我认为您应该重新考虑您的设计。通常,UserControl没有视图模型。想象一下你购买的第三方控制权。它不附带虚拟机。你通过DP与它互动。当您准备好使用它时,您可以将它放置在另一个视图中,如我的示例中所示,并将它的公开DP绑定到父视图。我通常有一个包含VM的窗口或父视图,但它们通过DP绑定与用户控件进行通信。您的用户控件应该是独立的,不需要VM。很好,我将尝试重新编写代码,以便所有DP都通过central viewmodel。
<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>