【发布时间】:2016-03-18 18:46:01
【问题描述】:
我遇到了绑定问题。 可能我没看到。
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 错误:4:找不到绑定源 参考“元素名称 = ProductCatalogLightList”。 绑定表达式:路径=ID;数据项=空;目标元素是 'InvokeCommandAction' (HashCode=11254959);目标属性是 'CommandParameter'(类型'对象')
【问题讨论】:
标签: c# wpf xaml mvvm data-binding