开发者

Do not add a event click

When you add a Click event throws 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>

without the event everything works fine

event code:

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

edit:

complete code in which the event in another element works well in right there.

<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="开发者_Go百科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>

is work

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

click not work

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


your click event should be protected or public. The XAML and the code behind are partial classes hence the XAML cant access the click event if its defined in the other partial class and declared private


I experienced the same thing. In the end I was having an outdated incompatible version of .NET Framework, is all. Updating from .NET 4.0 to 4.5.1 solved the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜