Wpf 如何在mvvm中对矩形命令使用多重绑定?
我得到了下面的矩形Wpf 如何在mvvm中对矩形命令使用多重绑定?,wpf,mvvm-light,multibinding,Wpf,Mvvm Light,Multibinding,我得到了下面的矩形 <Rectangle Width="{Binding Width}" Height="{Binding Length}" Tag="{Binding Id}" Name="rectangleDrawnMachine"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDown">
<Rectangle
Width="{Binding Width}"
Height="{Binding Length}"
Tag="{Binding Id}"
Name="rectangleDrawnMachine">
<i:Interaction.Triggers>
<i:EventTrigger
EventName="MouseDown">
<cmd:EventToCommand
Command="{Binding Main.UpdateSelectedMachine, Mode=OneWay, Source={StaticResource Locator}}"
PassEventArgsToCommand="True"
CommandParameter="{Binding ElementName=rectangleDrawnMachine}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
矩形绑定到在上面的ItemsControl中声明的模型。文档结构如下所示:
<Grid>
<ItemsControl ItemsSource="{Binding AllMachines}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Name="canvasDrawnMachines" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Rectangle Name="rectangleDrawnMachine"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
现在,我的UpdateSelectedMachine命令至少需要矩形的三个属性:
- 位置x
- 位置y
- ID/标签
所以我的问题是:如何在矩形命令上使用多重绑定?以及如何转移画布的位置?您可以获得
画布左
和画布顶部
附加属性的值,这些属性是作为命令参数传递给命令的矩形
,如下所示:
double x = Canvas.GetLeft(rectangle);
double y = Canvas.GetTop(rectangle);
你知道如何用XAML的方式得到这个职位吗
使用带转换器的多重绑定
,并绑定到画布。左侧
和画布。顶部
属性:
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="(Canvas.Left)" ElementName="canvasDrawnMachines"/>
<Binding Path="(Canvas.Top)" ElementName="canvasDrawnMachines"/>
<Binding Path="Tag" ElementName="canvasDrawnMachines"/>
</MultiBinding>
您可以获得作为命令参数传递给命令的
矩形的Canvas.Left
和Canvas.Top
附加属性的值,如下所示:
double x = Canvas.GetLeft(rectangle);
double y = Canvas.GetTop(rectangle);
你知道如何用XAML的方式得到这个职位吗
使用带转换器的多重绑定
,并绑定到画布。左侧
和画布。顶部
属性:
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="(Canvas.Left)" ElementName="canvasDrawnMachines"/>
<Binding Path="(Canvas.Top)" ElementName="canvasDrawnMachines"/>
<Binding Path="Tag" ElementName="canvasDrawnMachines"/>
</MultiBinding>
不能使用命令参数传递多个值
为此,必须使用多重绑定
<cmd:EventToCommand
Command="{Binding Main.UpdateSelectedMachine, Mode=OneWay, Source={StaticResource Locator}}"
PassEventArgsToCommand="True">
<cmd:EventToCommand.CommandParameter>
<MultiBinding Converter="{StaticResource YourConverter}">
<Binding Path="Canvas.Left" ElementName="canvasDrawnMachines"/>
<Binding Path="Canvas.Top" ElementName="canvasDrawnMachines"/>
<Binding Path="Tag" ElementName="canvasDrawnMachines"/>
</MultiBinding>
</cmd:EventToCommand.CommandParameter>
然后,执行命令逻辑:
public void OnExecute(object parameter)
{
var values = (object[])parameter;
var left = (double)values[0];
var top = (double)values[1];
var tag = values[2];
}
不能使用命令参数传递多个值
为此,必须使用多重绑定
<cmd:EventToCommand
Command="{Binding Main.UpdateSelectedMachine, Mode=OneWay, Source={StaticResource Locator}}"
PassEventArgsToCommand="True">
<cmd:EventToCommand.CommandParameter>
<MultiBinding Converter="{StaticResource YourConverter}">
<Binding Path="Canvas.Left" ElementName="canvasDrawnMachines"/>
<Binding Path="Canvas.Top" ElementName="canvasDrawnMachines"/>
<Binding Path="Tag" ElementName="canvasDrawnMachines"/>
</MultiBinding>
</cmd:EventToCommand.CommandParameter>
然后,执行命令逻辑:
public void OnExecute(object parameter)
{
var values = (object[])parameter;
var left = (double)values[0];
var top = (double)values[1];
var tag = values[2];
}
谢谢你的支持。但我想在XAML上用MVVM模式实现它。您知道如何以XAML方式获取位置吗?那么为什么要将矩形作为参数传递给命令?我的观点是,您可以访问坐标,就像您在命令的Execute方法中访问矩形的任何其他属性一样,因此我并不真正理解您的问题?如果您使用多重绑定,您应该根据我编辑的答案绑定到Canvas.Left和Canvas.Top属性,即该属性称为“Canvas.Left”不仅仅是“左”,谢谢你的更新。有问题的,例如Canvas.Top返回我{dependencProperty.UnsetValue}。coe
在属性名称周围使用括号没有任何问题;“(Canvas.Left)”。感谢您的支持。但我想在XAML上用MVVM模式实现它。您知道如何以XAML方式获取位置吗?那么为什么要将矩形作为参数传递给命令?我的观点是,您可以访问坐标,就像您在命令的Execute方法中访问矩形的任何其他属性一样,因此我并不真正理解您的问题?如果您使用多重绑定,您应该根据我编辑的答案绑定到Canvas.Left和Canvas.Top属性,即该属性称为“Canvas.Left”不仅仅是“左”,谢谢你的更新。有问题的,例如Canvas.Top返回我{dependencProperty.UnsetValue}。coe
在属性名称周围使用括号没有任何问题;“(画布,左)”谢谢你的回答。多重绑定方式工作得非常好。问题是画布的矩形位置没有传递正确的值。上面写着“未定义”。可能是因为我的datatemplate有多个矩形?这不是多个矩形的问题。你想要多少就有多少。我已经更新了答案中的绑定。感谢您的更新。有问题的,例如Canvas.Top返回我{dependencProperty.UnsetValue}。代码
工作时没有问题谢谢你的回答。多重绑定方式工作得非常好。问题是画布的矩形位置没有传递正确的值。上面写着“未定义”。可能是因为我的datatemplate有多个矩形?这不是多个矩形的问题。你想要多少就有多少。我已经更新了答案中的绑定。感谢您的更新。有问题的,例如Canvas.Top返回我{dependencProperty.UnsetValue}。代码
工作时没有任何问题