2012-02-15 56 views
4

這種簡單的WPF的DataGrid中的DataGrid CURRENTITEM =的SelectedItem與製表鍵

<DataGrid AutoGenerateColumns="False" Height="300" HorizontalAlignment="Stretch" 
     VerticalAlignment="Stretch" Name="dgOriginal" Margin="4,12,0,0" 
     CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" IsSynchronizedWithCurrentItem="True" 
     CanUserSortColumns="False" SelectionMode="Single" SelectionUnit="FullRow"> 
<DataGrid.Columns> 
    <DataGridCheckBoxColumn x:Name="col2Checked"/> 
    <DataGridTextColumn x:Name="col2Name"/> 
    <DataGridTextColumn x:Name="col2Vorname"/> 
</DataGrid.Columns>    

這說明沒有問題綁定列表,表現在一種奇怪的方式獲得焦點時回折返後: 首先,用戶選擇一行,使數據網格以選定的方式顯示該行(SelectedItem和CurrentItem包含所選對象)。然後把重點放在另一個控制上。在這種狀態下 - 選擇仍然顯示 - SelectedItem仍然存在,而CurrentItem爲空!然後焦點通過使用TAB按鈕回來。這使得CurrentItem成爲SelectedItem未被更改時顯示的第一個對象。所以CurrentItem不會與SelectetItem一起在DataGrid中看到的狀態中。我認爲對自己有什麼好處...

我的疑問是:如何建議DataGrid具有相同的CurrentItem之前被選中的焦點丟失?以及如何同步CurrentItem和SelectedItem?

我希望能有一個簡單的解決方案!你會幫助我很多。在此先感謝...

回答

2

通常我將SelectedItem綁定到DataContext中的一個屬性,並將IsSynchronizedWithCurrentItem設置爲false。

<DataGrid ItemsSource="{Binding SomeCollection}" 
      SelectedItem="{Binding SelectedItem}" /> 

設置IsSyncrhonizedWithCurrentItem爲true,將使它這樣控制的SelectedItem與集合的CurrentItem財產同步,但我有問題,與此,因爲我並不總是知道如何CurrentItem獲取和維護它的價值。

+0

謝謝您的幫助!但是恐怕你的建議不能解決問題:在使用Tab按鈕重新登錄後,我仍然在第一列看到這個醜陋的小框標記CurrentItem行。並且,如果用戶使用鍵盤在DataGrid中導航,則所選行將更改爲CurrentItem行,但似乎SelectedItem始終跟隨CurrentItem ... – navigato 2012-02-15 23:27:40

0

兩種方式解決此問題:

  1. 記錄錯誤報告與Microsoft支持,指出當您使用TAB IsSynchronizedWithCurrentItem並不總是奏效。

  2. 綁定的SelectedItem到當前單元格的列,其存儲在CurrentCell的項目屬性:

    <DataGrid SelectedItem="{Binding RelativeSource={RelativeSource Self}, Path=CurrentCell.Item, Mode=OneWay}" /> 
    
相關問題