DataTemplate中的按钮单击事件无法加载

时间:2012-10-23 07:02:21

标签: c# wpf

我添加ContentPresenter后,Click="btnAdd_Click"中的GridViewColumn给出了null异常错误,并且无法加载窗口。

WPF:

<ContentPresenter Content="{Binding}">
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
               <Grid>
                    <ListView Name="productList" ItemsSource="{Binding Path=ProductList}" Height="200">
                        <ListView.View>
                            <GridView>
                                <GridView.ColumnHeaderContainerStyle>
    <GridViewColumn.CellTemplate>
                                                                <DataTemplate>
                                                                    <StackPanel Orientation ="Horizontal">
                                                                        <Button x:Name="btnOfflineAdd" 
                                                                    Tag="{Binding Index}"
                                                                    HorizontalAlignment="Left"
                                                                    VerticalAlignment="Top" 
                                                                    FontFamily="Segoe WP" 
                                                                    Content="+"
                                                                    Height="30" Width="25"
                                                                    Click="btnAdd_Click" />
                                                                    </StackPanel>
                                                                </DataTemplate>
                                                            </GridViewColumn.CellTemplate>
                                                        </GridViewColumn>
                    ...
                </Grid>
            </DatTemplate>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=ProductList.Count}" Value="0">
                     <Setter TargetName="productList" Property="Visibility" Value="Collapsed"/>
                    <Setter TargetName="productListEmpty" Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>

1 个答案:

答案 0 :(得分:0)

我认为ContentPresenter内的控件不会在鼠标事件中被点击。请参阅this answer