若要重現此問題,請添加一個用戶控件,粘貼到下面的xaml中,然後將實例添加到窗口。最後,將窗口的datacontext設置爲ADummyDataContext的一個實例(也在下面)Wpf DataGrid問題
當您第一次運行應用程序時,您應該得到一個包含三個類別(每個包含一個cat)的網格。如果您點擊最下面兩個類別中的任意一個,並點擊一個貓的名字,藍色的一行就會出現,只顯示貓的名字。
但是,如果您單擊第一行並單擊貓的行,藍色的行將不會出現。 注意:這隻發生在您第一次運行應用程序。只要你點擊任何其他的貓,第一類的貓會按預期工作。
<UserControl x:Class="WpfUserControls.SimpleGridControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="#FFE46400">
<Grid Margin="2,2,2,2">
<Grid.RowDefinitions>
<RowDefinition Height="26" MaxHeight="26" MinHeight="26" />
<RowDefinition />
<RowDefinition Height="26" MaxHeight="26" MinHeight="26" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToolBar Grid.Row="0">
<Button Content="Button" Name="button1" VerticalAlignment="Center" Width="75" />
<Button Content="Button" Name="button2" VerticalAlignment="Center" Width="75" />
</ToolBar>
<DataGrid CanUserAddRows="False" ItemsSource="{Binding Path=KittensView}" AutoGenerateColumns="True" Grid.Row="1" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Path=ItemCount}"/>
<TextBlock Text=" Items"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Background="LightBlue" Orientation="Horizontal" >
<!-- <Image Height="32" Width="32" Source="/WpfUserControls;component/cat.png"></Image> -->
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Height ="20" Text="{Binding Path=Name}"/>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
<StatusBar Grid.Row="2"></StatusBar>
</Grid>
</UserControl>
這裏是數據上下文類和一個Kitten類。
public class ADummyDataContext
{
public List<Kitten> Kittens { get; set; }
public ADummyDataContext()
{
Kittens = new List<Kitten>
{
new Kitten {Color = "Orange", Name = "Alfie", Weight=6, Sex="Male"},
new Kitten {Color = "Black and White", Name = "Smudge", Weight = 4, Sex="Female"},
new Kitten {Color = "Grey", Name = "Charlotte", Weight = 5, Sex="Female"}
};
KittensView = new ListCollectionView(Kittens);
KittensView.GroupDescriptions.Add(new PropertyGroupDescription("Weight"));
}
public ListCollectionView KittensView { get; set; }
}
public class Kitten
{
public string Name { get; set; }
public string Color { get; set; }
public int Weight { get; set; }
public string Sex { get; set; }
}
我會特別感興趣的知道你是如何去弄清楚問題出在哪裏的。
感謝
這許多可能不會解決您的問題,但在過去,我有使用列表時遇到問題,也許嘗試將列表更改爲ObservableCollection? – Purplegoldfish
也是一個很好的觀點。謝謝:) – Ian