1

我知道这个问题已经发布了大约一千次,但我没有找到解决我问题的解决方案。

我有一个 LongListSelector ItemTemplate

<DataTemplate x:Key="AddrBookItemTemplate">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock FontWeight="Bold" Text="{Binding Name}" HorizontalAlignment="Stretch" VerticalAlignment="Center"  Grid.Column="0" Grid.Row="0" />
    <Button x:Name="itembutton" CommandParameter="{Binding ItemID}" Content="{Binding ButtonCaption}" Width="150" HorizontalAlignment="Right" Grid.Column="1" Grid.Row="0" Click="ItemButtonClick"/>
  </Grid>
</DataTemplate>

所以发生的事情只是我收到了我在标题中发布的这个漂亮的错误消息。我不知道,为什么?!

private void ItemButtonClick(object sender, RoutedEventArgs e)
{
  if (sender == this.itemButton) {
    ....
4

1 回答 1

0

DataTemplate它看起来你正在一个容器中显示这个按钮,如 aListBoxLongListSelector

这意味着没有范围itembuttonthis(大概thisUserControlor 或PhoneApplicationPage):实际上您的每个ListBoxItems!

你必须找到另一种方法来找到你的按钮。

更新:您应该发现DataContext您的按钮与它所包含的列表项匹配。这可能是最简单的处理方式:但是如果所有其他方法都失败,您可以Tag按照您的建议使用该属性。

喜欢:

private void ItemButtonClick(object sender, RoutedEventArgs e)
{
    var theButton = (Button) sender;
    var myItem = (TypeOfAnObjectInMyList) theButton.DataContext;
    doSomethingWithMyItem(myItem);
于 2013-03-05T16:59:09.933 回答