2016-08-22 129 views
0

我在RowDetail內部有一個Datagrid。我想綁定一個位於我的MainViewModel內部的命令。我不知道該怎麼做。我嘗試了一些解決方案在網絡上找到,但它不工作...xaml無法正確綁定命令

我不知道是什麼的MouseBinding標籤中添加...

<Window x:Class="ListeCommandes.View.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:view="clr-namespace:ListeCommandes.View" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="900" 
    DataContext="{StaticResource MainViewModel}"> 
<Window.Resources> 
    <view:TypeToBrushConverter x:Key="TypeToBrushConverter" /> 
    <view:StatutToBrushConverter x:Key="StatutToBrushConverter" /> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="Auto"></RowDefinition> 
    </Grid.RowDefinitions> 
    <StackPanel Grid.Row="0" Orientation="Horizontal"> 
     <Button Name="ExpandAll" Content="++" VerticalAlignment="Bottom" Width="30" Click="ExpandAll_OnClick"/> 
     <Button Name="CollapseAll" Content="--" VerticalAlignment="Bottom" Width="30" Margin="0" Click="CollapseAll_OnClick"/> 
    </StackPanel> 
    <DataGrid x:Name="GrdLignes" HorizontalAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="0,0,0,0" 
       Grid.Row="1" VerticalAlignment="Top" AutoGenerateColumns="False" CanUserAddRows="False" 
       CanUserDeleteRows="False" ItemsSource="{Binding Lignes}" IsReadOnly="True" 
       RowDetailsVisibilityMode="VisibleWhenSelected" RowHeaderWidth="25"> 
     <DataGrid.GroupStyle> 
      <GroupStyle> 
       <GroupStyle.ContainerStyle> 
        <Style TargetType="{x:Type GroupItem}"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type GroupItem}"> 
            <Expander Background="Lavender"> 
             <Expander.Header> 
              <StackPanel Orientation="Horizontal"> 
               <TextBlock Text="{Binding Path=Name}" Padding="0,0,5,0" FontWeight="Bold" /> 
               <TextBlock Text="{Binding Path=ItemCount}" Padding="0,0,5,0"/> 
               <TextBlock Text="Commandes"/> 
              </StackPanel> 
             </Expander.Header> 
             <ItemsPresenter /> 
            </Expander> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </GroupStyle.ContainerStyle> 
      </GroupStyle> 
     </DataGrid.GroupStyle> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Pièce Achat" Binding="{Binding Path=Piece}" FontWeight="Bold"/> 
      <DataGridTextColumn Header="Type" Binding="{Binding Path=TypeLabel}"> 
       <DataGridTextColumn.ElementStyle> 
        <Style TargetType="{x:Type TextBlock}"> 
         <Setter Property="Background" Value="{Binding Path=Type, Converter={StaticResource TypeToBrushConverter}}" /> 
        </Style> 
       </DataGridTextColumn.ElementStyle> 
      </DataGridTextColumn> 
      <DataGridTextColumn Header="Statut" Binding="{Binding Path=StatutLabel}"> 
       <DataGridTextColumn.ElementStyle> 
        <Style TargetType="{x:Type TextBlock}"> 
         <Setter Property="Background" Value="{Binding Path=Statut, Converter={StaticResource StatutToBrushConverter}}" /> 
        </Style> 
       </DataGridTextColumn.ElementStyle> 
      </DataGridTextColumn> 
     </DataGrid.Columns> 
     <DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <DataGrid Name="grdVBC" RowHeaderWidth="25" ItemsSource="{Binding Path=Lignes}" AutoGenerateColumns="False" Margin="0" CanUserAddRows="False" 
          CanUserDeleteRows="False" IsReadOnly="True"> 
        <DataGrid.InputBindings> 
         <MouseBinding MouseAction="LeftDoubleClick" Command="????" CommandParameter="{Binding ElementName=grdVBC, Path=SelectedItem}"/> 
        </DataGrid.InputBindings> 
        <DataGrid.Columns> 
         <DataGridTextColumn Header="Acheteur" Binding="{Binding Path=Acheteur}"/> 
         <DataGridTextColumn Header="Pièce" Binding="{Binding Path=Piece}"/> 
         <DataGridTextColumn Header="Client" Binding="{Binding Path=Client}"/> 
         <DataGridTextColumn Header="Ref" Binding="{Binding Path=ArRef}"/> 
         <DataGridTextColumn Header="Ref Fourn" Binding="{Binding Path=RefFourn}"/> 
         <DataGridTextColumn Header="Designation" Binding="{Binding Path=Designation}"/> 
         <DataGridTextColumn Header="Qte" Binding="{Binding Path=CmQte}"/> 
         <DataGridTextColumn Header="Vendeur" Binding="{Binding Path=Vendeur}"/> 
        </DataGrid.Columns> 
       </DataGrid> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 
    </DataGrid> 
</Grid> 

我的視圖模型:

public ICommand DoubleCLick { get; set; } 

    public MainViewModel() 
    { 
     DoubleCLick = new Command(DoubleClickAction); 
    } 

    private void DoubleClickAction(object parameters) 
    { 
     Debug.Print("OK"); 
    } 
+0

我認爲你需要更換?與DoubleClick –

+0

是的,但我只是找到了一個可行的解決方案,所以我會發布爲答​​案 – ebelair

回答

0

我找到了一個工作的解決方案:

<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=DataContext.DoubleCLick}" CommandParameter="{Binding ElementName=grdVBC, Path=SelectedItem}"/>