我有绑定问题。 也许我没有看到它。
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 Binding。
我的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:找不到绑定源 引用'ElementName = ProductCatalogLightList'。 BindingExpression:路径= ID;的DataItem = NULL;目标元素是 'InvokeCommandAction'(HashCode = 11254959);目标属性是 'CommandParameter'(类型'对象')
答案 0 :(得分:1)
您没有名为ProductCatalogLightList的元素,触发器中的datacontext也已经是Product_Catalog_Light,所以只需执行此操作:
CommandParameter="{Binding ID}"
绑定到xaml中控件的属性时使用ElementName,例如,如果您将Control命名为:Tile,则可以绑定到其Title属性