2016-03-18 10 views
0

バインディングに問題があります。 多分私はそれを見ませんでした。コマンドでのトラブルシューティングViewModel

XAMLファイル

<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> 

だから私の問題は、CommandParameterバインディングです。

私のModelViewは、私はそれがバインディングソースを見つけることができないというエラーメッセージが出ていている

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; 
    } 

のように見えます。

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')

答えて

1

また、トリガーであなたのDataContextのはまだこれだけやるProduct_Catalog_Lightで、ProductCatalogLightListという名前の要素を持っていない:コントロールのプロパティにバインドする場合

CommandParameter="{Binding ID}" 

のElementNameが使用されているがあなたのコントロールに名前を付けた場合など、あなたのTitleプロパティにバインドすることができます。

+0

Oh yes ... ありがとうございます!! – Ebc

+0

@Ebc:[Snoop](https://snoopwpf.codeplex.com/)を試してみてください。これは、視覚ツリーを横断してプロパティを表示/変更することを可能にします。また、コントロールのアクティブなDataContextを表示するのに役立ちます。 – Anders

関連する問題