不要添加事件单击

时间:2010-10-19 07:15:23

标签: wpf xaml binding datatemplate

添加Click事件时会抛出System.NullReferenceException。

<ListBox DockPanel.Dock="Top" DataContext="{Binding idPerson, Converter={StaticResource ConvertTrainersFromClientsKey}}" ItemsSource="{Binding}" >
 <ListBox.ItemTemplate>
  <DataTemplate>
   <DockPanel>
    <Button Content="{Binding name_trainer}" Tag="{Binding idPersonTrainer}" DockPanel.Dock="Top" Click="Button_Click"/>//this Button_Click
    <DockPanel>
     <TextBlock Text="Окончание заявки: "/>
     <TextBox DockPanel.Dock="Top" Style="{StaticResource TextBoxIsReadOnly}" Text="{Binding date_payment}"/>
    </DockPanel>
   </DockPanel>
  </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

没有事件一切正常

事件代码:

private void Button_Click(object sender, RoutedEventArgs e)
{
}

编辑:

完整的代码,其中另一个元素中的事件在那里正常工作。

<ListView DockPanel.Dock="Top" Grid.Row="0" x:Name="datagrid" Loaded="datagrid_Loaded" ItemsSource="{Binding Client}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <DockPanel LastChildFill="False">
                <Border Margin="10" Width="100" BorderThickness="3" BorderBrush="LightBlue" CornerRadius="2"  >
                    <DockPanel>
                        <Image Source="{Binding photo, Converter={StaticResource ConvertNullImageKey}}"/>
                    </DockPanel>
                </Border>
                <DockPanel Margin="10" Width="{Binding BindingGroupName=dockwidth}">
                    <TextBox DockPanel.Dock="Top" Style="{StaticResource TextBoxIsReadOnly}" Text="{Binding name_client}"/>
                    <Label Content="Сопровождающий" DockPanel.Dock="Top"/>
                    <ListBox DockPanel.Dock="Top" x:Name="listbox" DataContext="{Binding idPerson, Converter={StaticResource ConvertTrainersFromClientsKey}}" ItemsSource="{Binding}" >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <Button Content="{Binding name_trainer}" Tag="{Binding idPersonTrainer}" DockPanel.Dock="Top" />
                                    <DockPanel>
                                        <TextBlock Text="Окончание заявки: "/>
                                        <TextBox DockPanel.Dock="Top" Style="{StaticResource TextBoxIsReadOnly}" Text="{Binding date_payment}"/>
                                    </DockPanel>

                                </DockPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <Label DockPanel.Dock="Top" Content="Нету" Visibility="{Binding ElementName=listbox, Converter={StaticResource ConvertTrainersFromClientsVisibilityKey}}"/>
                </DockPanel>
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="Дата рождения" Grid.Column="0" Grid.Row="0" Margin="5"/>
                    <TextBlock Text="Адрес" Grid.Column="0" Grid.Row="1" Margin="5"/>
                    <TextBlock Text="Телефон" Grid.Column="0" Grid.Row="2" Margin="5"/>
                    <TextBlock Text="Email" Grid.Column="0" Grid.Row="3" Margin="5"/>
                    <TextBlock Text="Партнер" Grid.Column="0" Grid.Row="4" Margin="5"/>
                    <TextBox Text="{Binding date_reg}" Grid.Column="1" Grid.Row="0"  Margin="5"/>
                    <TextBox Text="{Binding address}" Grid.Column="1" Grid.Row="1"  Margin="5"/>
                    <TextBox Text="{Binding phone}" Grid.Column="1" Grid.Row="2"  Margin="5"/>
                    <TextBox Text="{Binding mail}" Grid.Column="1" Grid.Row="3" Margin="5"/>
                    <Button Content="{Binding namePartner}" Click="Button_Click_1" Grid.Column="1" Grid.Row="4" Margin="5"/>
                </Grid>
                <DockPanel LastChildFill="False"  Margin="10">
                    <Button Content="Редактировать" DockPanel.Dock="Top" Margin="10" x:Name="p_editStudent" Click="p_editStudent_Click"  VerticalAlignment="Center" />
                    <Button Tag="{Binding idPerson}" Content="Переписка" Margin="10" x:Name="p_allMessages" Click="p_allMessages_Click" DockPanel.Dock="Top" VerticalAlignment="Center" />
                    <Button Content="Написать письмо" Margin="10" x:Name="p_sendMessageForStudent" Click="p_sendMessageForStudent_Click" DockPanel.Dock="Top" VerticalAlignment="Center" />
                </DockPanel>
            </DockPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

是工作

<Button Content="{Binding namePartner}" Click="Button_Click_1" Grid.Column="1" Grid.Row="4" Margin="5"/>

点击无法使用

<Button Content="{Binding name_trainer}" Tag="{Binding idPersonTrainer}" Click="Button_Click" DockPanel.Dock="Top" />

2 个答案:

答案 0 :(得分:0)

您的点击事件应受到保护或公开。 XAML和后面的代码是部分类,因此XAML无法访问click事件,如果它在其他分部类中定义并声明为私有

答案 1 :(得分:0)

我经历过同样的事情。最后,我有一个过时的不兼容的.NET Framework版本,就是这样。从.NET 4.0更新到4.5.1解决了这个问题。