2015-12-23 3 views
1

le code XAML est:problème avec DataGrid.RowDetailsTemplate d'utiliser la méthode de commande d'accès

<DockPanel> 
    <DataGrid x:Name="Quote" AutoGenerateColumns="False"> 
     <DataGrid.Columns > 
      <DataGridTextColumn  Header="Code" Binding="{Binding Code}"/> 
      <DataGridTextColumn  Header="Name" Binding="{Binding Name}"/> 
      <DataGridTextColumn  Header="Quantities" Binding="{Binding Quantities}"/> 
      <DataGridTextColumn  Header="Price" Binding="{Binding Price}"/> 
      <DataGridTextColumn  Header="Cost" Binding="{Binding Cost}"/> 
      <DataGridCheckBoxColumn Header="Done" Binding="{Binding Done}"/> 
     </DataGrid.Columns> 
     <DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <Border BorderThickness="0" Background="Beige" Padding="10"> 
        <StackPanel Orientation="Horizontal"> 
         <Button Command="{Binding Path=DataContext.Renew1Command, RelativeSource= {RelativeSource FindAncestor,AncestorType=DataGrid}}">Call Command</Button>       
        </StackPanel> 
       </Border> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 
    </DataGrid> 
</DockPanel> 

le modèle est:

public class StockModel 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public void propChanged(string propName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 
    private string code; 
    public string Code 
    { 
     get { return code; } 
     set 
     { 
      code = value; 
      this.propChanged("Code"); 
     } 
    }      
    ....   

    private bool done; 
    public bool Done 
    { 
     get { return done; } 
     set 
     { 
      done = value; 
      this.propChanged("Done"); 
     } 
    } 
    public DelegateCommand Renew1Command; 

    public void Renew1(object obj) 
    { 
     MessageBox.Show("Hello Command"); 
     System.Console.WriteLine("Hello Command"); 
    } 
    public StockModel() 
    { 
     Renew1Command = new DelegateCommand(); 
     this.Renew1Command.ExecuteAction += this.Renew1; 
    }    
} 

Enfin j'initialiser le point d'émission par:

List<StockModel> stockList = new List<StockModel>(); 
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "200" }); 
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = false, Price = "16.1", Quantities = "100" }); 
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "100" }); 
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "200" }); 
this.Quote.ItemsSource = stockList; 

le DataGridTextColumn peut afficher les données de la source de liaison, mais je peux ' t cliquez sur le bouton de DataTemplate pour appeler l'action correspondante du modèle. comment réparer la liaison? Commande d'appel

Répondre

0

Déplacer vers la classe StockModel, remplacer "public DelegateCommand Renew1Command;" avec "public DelegateCommand Renew1Command {get; set;}". le problème sera résolu.

+0

il doit être une propriété –