2013-02-26 87 views
3

我想知道如何防止在DataGrid中顯示詳細信息行,當有人點擊單元格內的超鏈接或按鈕時。當你嘗試點擊超鏈接和細節顯示而不是鏈接時,真的很煩人。防止單擊超鏈接時顯示詳細信息

另一個問題是,我在一列中有一些操作按鈕,所以當細節被摺疊時,您必須先點擊行來顯示細節,然後點擊例如編輯按鈕。

樣品:

MainWindow.xaml.cs

namespace WpfApplication1 
{ 
    public class Item 
    { 
     public string Column0 { get; set; } 
     public string Mail { get; set; } 
    } 

    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = this; 
     } 

     public ObservableCollection<Item> Items 
     { 
      get 
      { 
       ObservableCollection<Item> i = new ObservableCollection<Item>(); 
       i.Add(new Item() { Column0 = "dsaads", Mail = "[email protected]" }); 
       i.Add(new Item() { Column0 = "wdads", Mail = "[email protected]" }); 
       return i; 
      } 
     } 

     public void HyperlinkClick(object sender, RoutedEventArgs e) 
     { 
      MessageBox.Show("Clicked"); 
     } 
    } 
} 

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <DataGrid RowDetailsVisibilityMode="VisibleWhenSelected" ItemsSource="{Binding Items}" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Column0" Binding="{Binding Column0}" /> 
       <DataGridHyperlinkColumn Header="Mail" Width="*" Binding="{Binding Mail}" > 
        <DataGridHyperlinkColumn.ElementStyle> 
         <Style TargetType="TextBlock"> 
          <Setter Property="Padding" Value="2,0,2,0" /> 
          <EventSetter Event="Hyperlink.Click" Handler="HyperlinkClick" /> 
         </Style> 
        </DataGridHyperlinkColumn.ElementStyle> 
       </DataGridHyperlinkColumn> 
      </DataGrid.Columns> 

      <DataGrid.RowDetailsTemplate> 
       <DataTemplate> 
        <Grid Height="100"> 

        </Grid> 
       </DataTemplate> 
      </DataGrid.RowDetailsTemplate> 
     </DataGrid> 
    </Grid> 
</Window> 

要看到問題:選擇第一行,然後試着點擊超鏈接在第二行。

+1

如果你想更好的幫助,速度更快;總是提供代碼:) – christopher 2013-02-26 13:13:24

+0

由於蘊含和「datagrid」是多餘的,因爲它已經在標籤之中,所以沒有必要在問題標題中既不添加「如何」或「數據網格」,因爲「如何」是冗餘的。 – Default 2013-02-26 13:14:11

+0

朋友,你很難得到這個問題的答案。提供一些代碼和一些你已經嘗試過的東西。 – 2013-02-26 13:15:41

回答

2

您可以在超鏈接「OnPreviewMouseDown」上處理隧道事件,這將阻止事件到達顯示RowDetailsTemplate的DataGrid。

private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) 
     { 
      var hyperlink = (Hyperlink)sender; 
     Process.Start(hyperlink.NavigateUri.AbsoluteUri); 
     e.Handled = true; 
     } 

完整的例子:

<Window x:Class="DummyTree.DataGridTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="DataGridTest" Height="300" Width="300"> 
    <Grid> 
     <DataGrid ItemsSource="{Binding Customers}"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="First Name"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock> 
           <Hyperlink PreviewMouseDown="OnPreviewMouseDown" NavigateUri="http://www.google.com"> 
            <TextBlock Text="{Binding Name}" /> 
           </Hyperlink> 
          </TextBlock> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
      <DataGrid.RowDetailsTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding Name}" /> 
         <TextBlock Text=" details here" /> 
        </StackPanel> 
       </DataTemplate> 
      </DataGrid.RowDetailsTemplate> 
     </DataGrid> 
    </Grid> 
</Window> 

代碼背後:

using System.Collections.ObjectModel; 
using System.Windows; 
using System.Windows.Input; 

namespace DummyTree 
{ 
    public partial class DataGridTest : Window 
    { 
     public DataGridTest() 
     { 
      DataContext = new CustomerVM(); 
      InitializeComponent(); 
     } 

     private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) 
     { 
      var hyperlink = (Hyperlink)sender; 
     Process.Start(hyperlink.NavigateUri.AbsoluteUri); 
     e.Handled = true; 
     } 
    } 

    public class CustomerVM 
    { 
     public ObservableCollection<Customer> Customers { get; set; } 
     public CustomerVM() 
     { 
      Customers = new ObservableCollection<Customer> { new Customer { Name = "Leo" }, new Customer { Name = "Om" } }; 
     } 
    } 

    public class Customer 
    { 
     public string Name { get; set; } 
    } 
} 
+0

謝謝,這就是我需要的:-)。 – 2013-02-26 13:57:07

+1

請在上面的代碼示例中查看更新的事件處理程序 – Shrinand 2013-02-26 14:07:24