如何在ItemsControl中单击按钮时调用子窗口?

时间:2012-08-03 12:45:02

标签: c# silverlight-4.0 itemscontrol childwindow

我正在开发silverlight应用程序。我正在使用silverlight中的itemscontrols来根据需要透露UI。

<ItemsControl x:Name="CertificationsAndLicensesItemsControl">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"></RowDefinition>                               
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>

                        <Grid Grid.Row="0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="Auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Grid Grid.Row="0" Grid.Column="0" Width="500">
                                <TextBlock TextWrapping="Wrap" Foreground="Green" Text="{Binding Path=CertificationsAndLicensesTitle}" FontWeight="Bold" Margin="5"></TextBlock>
                            </Grid>
                            <Grid Grid.Row="0" Grid.Column="1">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock FontWeight="Bold" Text="Valid from:" HorizontalAlignment="Left" Margin="5"></TextBlock>
                                    <TextBlock Text="{Binding Path=ValidFrom}" Margin="5" ></TextBlock>
                                    <TextBlock Text="Until" Margin="5"></TextBlock>
                                    <TextBlock Text="{Binding Until}" Margin="5"></TextBlock>
                                </StackPanel>
                            </Grid>
                            <Grid Grid.Row="1" Grid.ColumnSpan="2">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
                                    <Button x:Name="EditButton" Tag="{Binding Path=ID}" Content="Edit" Click="EditButton_Click"></Button>
                                    <Button x:Name="DeleteButton" Tag="{Binding Path=ID}" Content="Delete" Click="DeleteButton_Click"></Button>
                                </StackPanel>
                            </Grid>
                        </Grid>

                    </Grid>

                </DataTemplate>

            </ItemsControl.ItemTemplate>
        </ItemsControl>

背后的代码如下

private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            //int a = 10;
            //CertificationsAndLicensesChildWindow obj = new CertificationsAndLicensesChildWindow();
            //obj.Show();
            ChildWindow1 obj1 = new ChildWindow1();
            obj1.Show();
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            CertificationsAndLicensesChildWindow obj = new CertificationsAndLicensesChildWindow();
            obj.Show();
        }

单击按钮时不会显示子窗口。单击按钮后,整个UI消失。如果我删除以下句子

ChildWindow1 obj1 = new ChildWindow1();
                obj1.Show();

然后至少断点被击中。如果我保持上述判断,那么断点就不会被击中。如何在点击按钮时显示子窗口?能否请您提供我可以解决上述问题的任何代码或链接

1 个答案:

答案 0 :(得分:0)

消失的UI几乎总是意味着未处理的异常 - 可能在ChildWindow1创建/显示期间出现了一些错误。您可以尝试在App类中为未处理的异常附加处理程序,如果它没有附加(在App构造函数中执行),并在未处理的异常处理程序中设置断点。这可以帮助您跟踪导致UI消失的异常。

默认情况下,在创建Silverlight项目时,会有Hnder并报告DOM的异常 - 您应该能够在JavaScript控制台中看到异常。