2017-07-19 21 views
5

Có một tập hợp các danh mục có sản phẩm.DataGrid mất lựa chọn

Mỗi danh mục được thể hiện trong giao diện bằng tab AvalonDock, trong đó có một DataGrid với các sản phẩm.

Bây giờ, khi chuyển từ tab này sang tab khác, DataGrid sẽ cập nhật bộ sưu tập mỗi lần. Nếu bạn chọn một cặp hàng trong bảng trên tab đầu tiên, hãy chuyển sang tab thứ hai và quay lại tab đầu tiên, lựa chọn sẽ biến mất.

Điều gì có thể là vấn đề?

XAML:

<xcad:DockingManager DocumentsSource="{Binding Examples}"> 
    <xcad:DockingManager.LayoutItemTemplate> 
     <DataTemplate> 
      <ListBox ItemsSource="{Binding Content.Items}" 
        SelectionMode="Extended" /> 
     </DataTemplate> 
    </xcad:DockingManager.LayoutItemTemplate> 
    <xcad:LayoutRoot /> 
</xcad:DockingManager>> 

Mã-đằng sau:

public partial class MainWindow : Window 
{ 
    public class Example 
    { 
     public List<int> Items { get; } = new List<int>(); 

     public Example() 
     { 
      for (var i = 0; i < 10; i++) 
      { 
       Items.Add(i); 
      } 
     } 
    } 

    public List<Example> Examples { get; } = new List<Example>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
     Examples.Add(new Example()); 
     Examples.Add(new Example()); 
    } 
} 

enter image description here

+0

đã không thử nó nhưng IsSynchronizedWithCurrentItem có thể giúp đỡ. –

+0

@SushilMate, Nó không giúp được gì. – maxwell

+0

Có vẻ như chuyển đổi giữa các tab cập nhật bố cục mọi lúc, do đó, hãy xóa lựa chọn. Tại sao bạn không sử dụng 'TabControl' thay thế? –

Trả lời

2

Như @nobody đề nghị, chuyển đổi giữa các tab dường như cập nhật các bố trí và trạng thái lựa chọn sẽ bị mất. Nếu giao diện người dùng không thể duy trì trạng thái lựa chọn, thì bạn có thể sử dụng lớp tiếp theo tức là bản trình bày hoặc chế độ xem để thực hiện tương tự.

Trong trường hợp này, hãy thêm thuộc tính IsSelected vào mục kiểu xem và liên kết với ListViewItem sẽ thực hiện thủ thuật.

enter image description here

XAML:

<Grid> 
    <xcad:DockingManager DocumentsSource="{Binding Examples}"> 
     <xcad:DockingManager.DocumentHeaderTemplate> 
      <DataTemplate> 
       <TextBlock Text="Doc" /> 
      </DataTemplate> 
     </xcad:DockingManager.DocumentHeaderTemplate> 
     <xcad:DockingManager.LayoutItemTemplate> 
      <DataTemplate> 
       <ListBox 
        DisplayMemberPath="Value" 
        ItemsSource="{Binding Content.Items}" 
        SelectionMode="Extended"> 
        <ListBox.Resources> 
         <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="LightBlue" /> 
         <Style TargetType="{x:Type ListBoxItem}"> 
          <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
         </Style> 
        </ListBox.Resources> 
       </ListBox> 
      </DataTemplate> 
     </xcad:DockingManager.LayoutItemTemplate> 
     <xcad:LayoutRoot /> 
    </xcad:DockingManager> 
</Grid> 

Mã-đằng sau:

public partial class MainWindow : Window 
{ 
    public class ExampleItem 
    { 
     public int Value { get; set; } 
     public bool IsSelected { get; set; } 
    } 
    public class Example 
    { 
     public List<ExampleItem> Items { get; } = new List<ExampleItem>(); 

     public Example() 
     { 
      for (var i = 0; i < 10; i++) 
      { 
       Items.Add(new ExampleItem { Value = i }); 
      } 
     } 
    } 

    public List<Example> Examples { get; } = new List<Example>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
     Examples.Add(new Example()); 
     Examples.Add(new Example()); 
    } 
} 
Các vấn đề liên quan