为wpf中的DataGrid控件添加鼠标单击事件
我们知道wpf中的DataGrid控件是没有鼠标单击事件的,使用MouseDown事件比较弱,要先鼠标单击一下,选中某个单元格,再鼠标单击才响应事件,这不是我们想要的
要响应鼠标单击事件的列,我们采用DataGridHyperlinkColumn超链接控件进行代替
下面提供3种解决方案:
方案一:最原始的方法,直接在xaml中写死,缺点是不灵活
方案二:可以动态的设置DataGridHyperlinkColumn显示和不显示(并且不显示时不占位置),灵活性很大,可以通过一个bool值进行显示或者不显示控制
方案三:代码动态创建DataGridHyperlinkColumn对象,并动态套上关联事件,灵活性更大
由于本人并没有系统地学过wpf,术语上如有写得不对的地方请多多指正!
方案一:
下面只贴出DataGrid对应的xaml代码,如下:
<Grid >
<DataGrid AutoGenerateColumns="False" Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" HeadersVisibility="None" Background="{x:Null}" Foreground="#FF3939EB" >
<DataGrid.Columns >
<DataGridHyperlinkColumn Header="新冠问卷" Width="479" Binding="{Binding Path=FullFileName}">
<DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<EventSetter Event="Hyperlink.Click" HandledEventsToo="False" Handler="OnHyperlinkClick" ></EventSetter>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
xaml对应的C#代码如下:
public partial class MainWindow : Window
{
DataTable dt = new DataTable();
DataColumn col = new DataColumn("FullFileName", typeof(string));
dt.Columns.Add(col);
DataRow dr = dt.NewRow();
dr[0] = "超链接1";
dt.Rows.Add(dr);
DataRow dr2 = dt.NewRow();
dr2[0] = "超链接2";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3[0] = "超链接3";
dt.Rows.Add(dr3);
dataGrid1.ItemsSource = dt.DefaultView;
}
//对应的鼠标单击事件
private void OnHyperlinkClick(object sender, RoutedEventArgs e)
{
if (dataGrid1.SelectedItem == null)
{
return;
}
MessageBox.Show((dataGrid1.SelectedValue as DataRowView)[0].ToString());
}
}
最终的效果图如下:
方案二:
在方案一的DataGridHyperlinkColumn标签上加上Name属性和Visibility(默认让它不显示),xaml代码如下:
<Grid >
<DataGrid AutoGenerateColumns="False" Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" HeadersVisibility="None" Background="{x:Null}" Foreground="#FF3939EB" >
<DataGrid.Columns >
<DataGridHyperlinkColumn x:Name="ColumnName1" Visibility="Collapsed" Header="新冠问卷" Width="479" Binding="{Binding Path=FullFileName}">
<DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<EventSetter Event="Hyperlink.Click" HandledEventsToo="False" Handler="OnHyperlinkClick" ></EventSetter>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
</DataGrid.Columns>
</DataGrid>
从上面的DataGridHyperlinkColumn标签,我们可见,新增了 x:Name="ColumnName1" 属性和Visibility="Collapsed"属性
xaml对应的后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataTable dt = new DataTable();
DataColumn col = new DataColumn("FullFileName", typeof(string));
dt.Columns.Add(col);
DataRow dr = dt.NewRow();
dr[0] = "超链接1";
dt.Rows.Add(dr);
DataRow dr2 = dt.NewRow();
dr2[0] = "超链接2";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3[0] = "超链接3";
dt.Rows.Add(dr3);
dataGrid1.ItemsSource = dt.DefaultView;
this.ColumnName1.Visibility = Visibility.Visible;
}
private void OnHyperlinkClick(object sender, RoutedEventArgs e)
{
if (dataGrid1.SelectedItem == null)
{
return;
}
MessageBox.Show((dataGrid1.SelectedValue as DataRowView)[0].ToString());
}
}
最终效果图就不贴出来了,和方案1 一样
方案三
利用Resources资源对样式进行管理,这样就可以动态为DataGrid添加一列和套上处理事件
xaml代码如下:
<Window.Resources>
<Style TargetType="TextBlock" x:Name="ContentStyle1">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<EventSetter Event="Hyperlink.Click" HandledEventsToo="False" Handler="OnHyperlinkClick" ></EventSetter>
</Style>
</Window.Resources>
<Grid >
<DataGrid AutoGenerateColumns="False" Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" HeadersVisibility="None" Background="{x:Null}" Foreground="#FF3939EB" >
</DataGrid>
</Grid>
从上面的xaml代码中我们可以看出,我们为<Style>样式起了个别名x:Name="ContentStyle1"
对应的后台代码如下:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataGridHyperlinkColumn newColumn1 = new DataGridHyperlinkColumn()
{
Header = "新冠问卷1",
Width = 100,
Binding = new Binding("ColumnName1"),
};
//为新增的第一列动态套用样式
newColumn1.ElementStyle = (Style)this.Resources["ContentStyle1"];
this.dataGrid1.Columns.Add(newColumn1);
DataGridHyperlinkColumn newColumn2 = new DataGridHyperlinkColumn()
{
Header = "新冠问卷2",
Width = 100,
Binding = new Binding("ColumnName2"),
};
//为新增的第二列动态套用样式
newColumn2.ElementStyle = (Style)this.Resources["ContentStyle1"];
this.dataGrid1.Columns.Add(newColumn2);
DataTable dt = new DataTable();
DataColumn column1 = new DataColumn("ColumnName1", typeof(string));
DataColumn column2 = new DataColumn("ColumnName2", typeof(string));
dt.Columns.Add(column1);
dt.Columns.Add(column2);
DataRow dr1 = dt.NewRow();
dr1["ColumnName1"] = "超链接11";
dr1["ColumnName2"] = "超链接12";
DataRow dr2 = dt.NewRow();
dr2["ColumnName1"] = "超链接21";
dr2["ColumnName2"] = "超链接22";
DataRow dr3 = dt.NewRow();
dr3["ColumnName1"] = "超链接31";
dr3["ColumnName2"] = "超链接32";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
dt.Rows.Add(dr3);
dataGrid1.ItemsSource = dt.DefaultView;
}
private void OnHyperlinkClick(object sender, RoutedEventArgs e)
{
if (dataGrid1.SelectedItem == null)
{
return;
}
MessageBox.Show((e.OriginalSource as Hyperlink).NavigateUri.OriginalString);
}
}
最终效果图:
本文地址:https://blog.csdn.net/zxy13826134783/article/details/110727611