wpf datagrid selection + expander的问题

时间:2011-09-18 19:59:28

标签: c# .net wpf datagrid expander

我有一个包含带有rowdetailstemplate的数据网格的扩展器。

当我单击数据网格行以查看行详细信息模板时,扩展程序将关闭!

任何人都知道为什么以及如何解决这个问题?

更新

原来发生了其他事情:

扩展器都包含在TabItem中。当我点击网格行时,TabControls.SelectionChanged会触发(为什么!!!?!?!!?!?!?!)然后重新计算并反弹数据。

所以真正的问题是为什么单击一个DataGrid行会导致父TabControl的SelectionChanged事件被触发?这是XAML:

<Window xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="BPPDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:BPPDemo"
    Title="BPP Demo" Height="600" Width="700" Loaded="Window_Loaded">

<Window.Resources>
    <local:AddrStatusColorConverter x:Key="addressStatusColorConverter"/>

    <local:BoolToVisConverter x:Key="boolToVisConverter"/>
</Window.Resources>

<DockPanel>
    <Menu DockPanel.Dock="Top">
        <Menu.Items>
            <MenuItem Header="File">
                <MenuItem Header="Open Assessment" Command="{Binding SelectAssessmentCommand}"></MenuItem>
                <MenuItem Header="Import Properties" Command="{Binding ImportPropertiesCommand}"></MenuItem>
            </MenuItem>
            <MenuItem Header="Properties" Visibility="{Binding IsSelected, ElementName=tabProperties,Converter={StaticResource boolToVisConverter}}">
                <MenuItem Header="Delete All Properties" Command="{Binding DeleteAllPropertiesCommand}"></MenuItem>
                <MenuItem Header="Resolve Invalid Addresses" Command="{Binding ValidateAddressesCommand}"></MenuItem>
                <MenuItem Header="Select Situs" Command="{Binding SelectSitusCommand}"/>
            </MenuItem>
        </Menu.Items>
    </Menu>

    <DockPanel Margin="10">
        <TextBlock Text="{Binding Assessment.Name}" FontWeight="Normal" FontSize="24" Foreground="#FF5C9EB7" DockPanel.Dock="Top" Margin="0,0,0,10"/>
        <TabControl SelectionChanged="TabControl_SelectionChanged">
            <TabItem x:Name="tabProperties" Header="Properties">
                <DataGrid ItemsSource="{Binding Properties}" AutoGenerateColumns="False" IsReadOnly="True" SelectedItem="{Binding SelectedProperty,Mode=TwoWay}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
                        <DataGridTextColumn Header="Cost" Binding="{Binding Cost,StringFormat='{}{0:c}'}"/>
                        <DataGridTextColumn Header="Year Acquired" Binding="{Binding YearAcquired}"/>
                        <DataGridTextColumn Header="Life In Years" Binding="{Binding LifeInYears}"/>
                        <DataGridTextColumn Header="Schedule" Binding="{Binding Schedule}"/>
                        <DataGridTextColumn Header="Current Value" Binding="{Binding CurrentValue,StringFormat='{}{0:c}'}" />
                        <DataGridTextColumn Header="Tax" Binding="{Binding Tax,StringFormat='{}{0:c}'}" />

                        <DataGridTemplateColumn Header="Address">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Address}" Foreground="{Binding AddressStatus, Converter={StaticResource addressStatusColorConverter}}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTextColumn Header="Address Status" Binding="{Binding AddressStatus}"/>
                        <DataGridTextColumn Header="Situs" Binding="{Binding Situs}"/>
                    </DataGrid.Columns>

                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <TextBlock>THIS ONE WORKS! WHY DOESN'T THE OTHER ONE????</TextBlock>
                        </DataTemplate>
                    </DataGrid.RowDetailsTemplate>


                </DataGrid>
            </TabItem>
            <TabItem Header="Listings" Name="tabListings">

                <DataGrid x:Name="SitusGroups" AutoGenerateColumns="false" SelectionMode="Single" SelectionUnit="FullRow">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Situs" Binding="{Binding Situs,StringFormat='{}{0:c}',TargetNullValue='none'}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Cost" Binding="{Binding Cost,StringFormat='{}{0:c}'}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Value" Binding="{Binding Value,StringFormat='{}{0:c}'}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Tax" Binding="{Binding Tax,StringFormat='{}{0:c}'}"></DataGridTextColumn>
                    </DataGrid.Columns>

                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <TextBlock>I JUST WANT TO SEE THIS WHEN I CLICK ON A ROW!</TextBlock>
                        </DataTemplate>
                    </DataGrid.RowDetailsTemplate>

                </DataGrid>



            </TabItem>
        </TabControl>

    </DockPanel>

</DockPanel>

1 个答案:

答案 0 :(得分:1)

问题与路由事件的方式有关。 TabControl和DataGrid派生自同一个类,因此当DataGrid SelectionChanged时,我的TabControl SelectionChanged处理程序被触发。

正如http://www.trentfguidry.net/post/2009/06/13/WPF-TabControl-SelectionChanged.aspx上的最后一条评论所示,我检查了事件处理程序中的原始源代码,如果引用不是我的tabcontrol则会返回。