2016-03-18 22 views
0

Bağlama sorunum var. Belki görmedim.Binding Troubles Komutla ViewModel

XAML Dosya

<ItemsControl Grid.Row="1" Grid.Column="1" x:Name="itemsControlTiles" ItemsSource="{Binding ProductCatalogLightList}" Margin="10"> 
      <ItemsControl.Template> 
       <ControlTemplate> 
        <WrapPanel Width="800" HorizontalAlignment="Left" 
         FlowDirection="LeftToRight" IsItemsHost="true"> 
        </WrapPanel> 
       </ControlTemplate> 
      </ItemsControl.Template> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Controls:Tile Title="{Binding Name}" 
          Margin="3" 
          Background="{Binding Background}" 
          Style="{StaticResource PrdukteTileStyle}" > 
        <i:Interaction.Triggers> 
         <i:EventTrigger EventName="Click"> 
           <i:InvokeCommandAction Command="{Binding Source={StaticResource productsTileViewModel}, Path=DoSomethingCommand}" 
               CommandParameter="{Binding ID,ElementName= ProductCatalogLightList}" /> 

          </i:EventTrigger> 
        </i:Interaction.Triggers> 
        </Controls:Tile> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

Yani benim Sorun Bağlama CommandParameter olduğunu.

Benim Modelview Ben bağlama kaynak bulamadığını söylüyor bir hata mesajı alıyorum

public class Product_Catalog_Light 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public int ID{ get; set; } 
    public System.Windows.Media.Brush Background { get; set; } 
} 

public class ProductsTileViewModel : INotifyPropertyChanged 
{ 
    private ObservableCollection<Product_Catalog_Light> _product_Catalog_LightList; 
    public ProductsTileViewModel() 
    { 
     ProductCatalogLightList = new ObservableCollection<Product_Catalog_Light>(); 
    } 
    ...... 
    public ObservableCollection<Product_Catalog_Light> ProductCatalogLightList 
    { 
     get { return _product_Catalog_LightList; } 
     set 
     { 
      _product_Catalog_LightList = value; 
      OnPropertyChanged("ProductCatalogLightList"); 
     } 
    } 

} 

public ICommand DoSomethingCommand 
    { 
     get 
     { 
      return _doSomethingCommand ?? 
        (_doSomethingCommand = new DelegateCommand<int>(ExecuteDoSomethingWithItem)); 
     } 
    } 

    private DelegateCommand<int> _doSomethingCommand; 


    private void ExecuteDoSomethingWithItem(int db_ID) 
    { 
     // Do something wir the _id 
     int i = 5; 
    } 

benziyor.

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=ProductCatalogLightList'. BindingExpression:Path=ID; DataItem=null; target element is 'InvokeCommandAction' (HashCode=11254959); target property is 'CommandParameter' (type 'Object')

cevap

1

Ayrıca tetikleyici sizin datacontext zaten bu yüzden sadece bunu bir Product_Catalog_Light olduğunu ProductCatalogLightList adında bir öğe yoktur: Bir denetimin bir özelliğine bağlanırken

CommandParameter="{Binding ID}" 

ElementName kullanılır xaml'inizde, örneğin Control'ü isimlendirdiyseniz: Tile, Title özelliğine sahip olabilirsiniz.

+0

Oh yes ... Çok teşekkürler! – Ebc

+0

@Ebc: [Snoop] 'u kullanmayı deneyin (https://snoopwpf.codeplex.com/). Özellikleri görmek/değiştirmek için görsel ağacı tersine çevirmenize izin verir. Bir kontrolün aktif DataContext'ini de görmenize yardımcı olabilir. – Anders