2017-04-03 80 views
0

我得到了以下矩形如何在mvvm的矩形命令上使用多重綁定?

<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 -command需要矩形的至少三個屬性:

  • 位置X
  • 位置Y
  • ID /標籤

使用CommandParameter o如果矩形本身,我的命令會得到很多關於矩形的信息(比如需要的標籤)。但它沒有獲得關於畫布的(X-& Y-)位置的必要信息。

所以我的問題是:如何在我的矩形命令上使用多重綁定?以及如何轉移畫布的位置?

回答

1

您不能使用命令參數傳遞多個值。

爲了做到這一點,你必須使用多重綁定。

<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 class YourConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, ...) 
    { 
     return values.Clone(); 
    } 
} 

然後,執行命令的邏輯:

public void OnExecute(object parameter) 
{ 
    var values = (object[])parameter; 
    var left = (double)values[0]; 
    var top = (double)values[1]; 
    var tag = values[2]; 
} 
+0

謝謝您的回答。多重綁定方式工作得很好。問題是畫布的矩形位置不能傳遞正確的值。它說'沒有定義'。也許是因爲我的數據模板有多個矩形? – Chpo7234

+0

這不是多個矩形的問題。你可以儘可能多地擁有它。我已經更新了答案中的綁定。 – Parag

+0

感謝您的更新。問題例如Canvas.Top返回我{DependencyProperty.UnsetValue}。代碼''沒有問題 – Chpo7234

1

你可以得到要傳遞的命令參數的命令,像這樣的RectangleCanvas.LeftCanvas.Top附加屬性的值:

double x = Canvas.GetLeft(rectangle); 
double y = Canvas.GetTop(rectangle); 

你知道如何得到這個職位在XAML方式?

使用MultiBinding與轉換器並綁定到Canvas.LeftCanvas.Top屬性:

<MultiBinding Converter="{StaticResource converter}"> 
      <Binding Path="(Canvas.Left)" ElementName="canvasDrawnMachines"/> 
      <Binding Path="(Canvas.Top)" ElementName="canvasDrawnMachines"/> 
      <Binding Path="Tag" ElementName="canvasDrawnMachines"/> 
</MultiBinding> 
+0

感謝您的支持。但我想要的是用MVVM模式在XAML上完成它。你知道如何以XAML的方式獲得職位嗎? – Chpo7234

+0

那麼,爲什麼你將Rectangle作爲參數傳遞給命令呢?我的觀點是,您可以訪問座標,就像您在命令的Execute方法中訪問Rectangle的任何其他屬性一樣,所以我不太瞭解您的問題? – mm8

+1

如果您使用的是多重綁定,您應該按照我編輯的答案綁定到Canvas.Left和Canvas.Top屬性,即屬性名爲「Canvas.Left」而不僅僅是「Left」。 – mm8