2011-09-14 15 views
7

この問題を再現するには、ユーザーコントロールを追加し、下のxamlに貼り付けて、インスタンスをウィンドウに追加します。最後に、ウィンドウのdatacontextをADummyDataContextのインスタンスに設定します(下記も参照)Wpf DataGridの問題

初めてアプリケーションを実行するときは、1つのネコを含む3つのカテゴリを持つグリッドを取得する必要があります。下の2つのカテゴリのいずれかをクリックして猫の名前をクリックすると、青い行が表示され、猫の名前だけが表示されます。

ただし、最初の行をクリックして猫の行をクリックすると、青色の行は表示されません。 注::これは初めてアプリケーションを実行するときにのみ発生します。他の猫をクリックすると、最初のカテゴリの猫は期待通りに機能します。

<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> 

ここにデータコンテキストクラスと子猫クラスがあります。

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; } 

} 

ここで問題がどのようになっているかを把握するにはどうすればよいでしょうか。

おかげ

+0

をしたいので、これはおそらく文句を言わないあなたの問題を解決しますが、過去に私が持っていたとして、多くのデータをリフレッシュすることができますリストを使用しているときに問題が発生しました。リストをObservableCollectionに変更しようとしましたか? – Purplegoldfish

+0

また、非常に良い点。ありがとう:) – Ian

答えて

5

問題は、それが最初にロードされるときDataGridの最初の項目がすでに選択されていることです。ただし、実際には選択されていません。選択されていないと表示され、グループは展開されません。最初の項目をクリックすると、DataGridSelectedIndexが既に0だったので、その違いを知ることができません。これは実際には厄介なことです。回避策として

、あなたはDataGrid

<DataGrid Loaded="dataGrid1_Loaded" 
      ...> 

イベントハンドラのLoadedイベントの最初の項目を選択解除することができますSelectedIndexを0

private void dataGrid1_Loaded(object sender, RoutedEventArgs e) 
{ 
    DataGrid dataGrid = sender as DataGrid; 
    dataGrid.SelectedItem = null; 
} 
+0

これは私のためにそれを修正します。奇妙なことに、私はそれを期待していませんでした。なぜなら、私がKittensViewを別の方法でソートすると、それはグリッドの最後であったにもかかわらず、「Alfie」と同じ問題を見たからです! –

+0

好奇心。変更イベントでのみ「描かれる」か評価されるのは奇妙に思えます。私はそれがそうでないことを意味します、これはwinformsの土地に戻っていれば私が期待していたものとまったく同じですが、私はもっと魔法を期待しました:) Meleak、週末の幸せなボーナスをありがとう:) – Ian

+0

@Ian:私は同意する、これは誰が期待するものではない.. –

0

おかげでたくさんあることに注意してください@ Fredrik ここに私のコメントに続くコードです:

XAML:

<DataGrid SelectionChanged="DataGrid_SelectionChanged"> 

codebehind.cs:

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    DataGrid dataGrid = sender as DataGrid; 
    dataGrid.SelectionChanged -= DataGrid_SelectionChanged; 
    dataGrid.SelectedItem = null; 
    dataGrid.SelectionChanged += DataGrid_SelectionChanged; 
} 

このコードも、あなたが