2010-06-15 43 views
6

Có cách nào cho sự kiện nhấp chuột phải để chọn một hàng trong datagrid bộ công cụ không?Silverlight Datagrid chọn trên nhấp chuột phải

Tôi đang sử dụng menu ngữ cảnh công cụ hoạt động độc đáo, nhưng vấn đề là, chỉ nhấp chuột trái có thể chọn hàng và tôi cần nhấp chuột phải để có thể thực hiện điều đó nếu tôi muốn menu ngữ cảnh hoạt động chính xác.

Bất kỳ trợ giúp nào được đánh giá cao

Trả lời

5

Bạn có thể tìm thấy giải pháp here.

Về cơ bản nó đi như thế này:

private void dg_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
    e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown); 
} 
void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    dg.SelectedItem = ((sender) as DataGridRow).DataContext; 
} 
+0

Nó quá xấu chúng ta phải sử dụng hack như thế này, nhưng tôi cho rằng nó tốt hơn là không có gì. Cảm ơn –

+0

Sự kiện MouseRightButtonDown chỉ khả dụng từ SL 4.0. Có giải pháp nào khác cho nhấp chuột phải trong SL 3.0 không? – vijaysylvester

1

Cảm ơn ý tưởng tốt. Nhưng sự kiện UnloadingRow có thể đã được xác định hiệu quả hơn.

private void dg_UnloadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e) 
{ 
    e.Row.MouseRightButtonDown -= Row_MouseRightButtonDown; 
} 
0

Tôi đã thử một cách tiếp cận hơi khác nhau bằng cách sử dụng sự kiện LoadingRow trong DataGrid. Tôi không thích sử dụng sự kiện đặc biệt đó nếu tôi không phải làm vậy, nhưng vì tôi không làm việc với số lượng lớn dữ liệu, nó hoạt động khá tốt. Điều duy nhất tôi không có trong mẫu này là lệnh để sử dụng để thực hiện hành động. Bạn có thể sử dụng một lệnh trên đối tượng DataContext hoặc một số cơ chế khác.

private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) 
    { 
     var contextMenu = new ContextMenu(); 

     var deleteMenuItem = new MenuItem {Header = "Delete User"}; 

     contextMenu.Items.Add(deleteMenuItem); 

     ContextMenuService.SetContextMenu(e.Row, contextMenu); 

    } 
3

Anh ấy là một hành vi mà sẽ làm các trick cho bạn (lấy cảm hứng từ blog post này):

public class SelectRowOnRightClickBehavior : Behavior<DataGrid> 
{ 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     AssociatedObject.MouseRightButtonDown += HandleRightButtonClick; 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 

     AssociatedObject.MouseRightButtonDown += HandleRightButtonClick; 
    } 

    private void HandleRightButtonClick(object sender, MouseButtonEventArgs e) 
    { 
     var elementsUnderMouse = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), AssociatedObject); 

     var row = elementsUnderMouse 
      .OfType<DataGridRow>() 
      .FirstOrDefault(); 

     if (row != null) 
      AssociatedObject.SelectedItem = row.DataContext; 
    } 
} 

Sử dụng nó như thế này:

<sdk:DataGrid x:Name="DataGrid" Grid.Row="4" 
        IsReadOnly="True" 
        ItemsSource="{Binding MyItems}"> 
     <i:Interaction.Behaviors> 
      <b:SelectRowOnRightClickBehavior/> 
     </i:Interaction.Behaviors> 
</sdk:DataGrid> 
Các vấn đề liên quan