WPF中沒有類似LinkButton,所以只有運用Button及樣式來實現LinkButton。
DataGrid 操作列 實現 多個類似LinkButton按鈕:
具體實現代碼如下:
<DataGrid Name="dgData" IsReadOnly="true" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="操作"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Name="btnInput" Content="填單" Cursor="Hand" Click="btnInput_Click" Margin="5,0,5,0"> <Button.Template> <ControlTemplate TargetType="Button"> <TextBlock TextDecorations="Underline" Name="btnInput" > <ContentPresenter /> </TextBlock> </ControlTemplate> </Button.Template> <Button.Style> <Style TargetType="Button"> <Setter Property="Foreground" Value="Blue" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Red" /> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> <Button Name="btnCancel" Content="取消" Cursor="Hand" Click="btnCancel_Click" Margin="5,0,5,0"> <Button.Template> <ControlTemplate TargetType="Button"> <TextBlock TextDecorations="Underline" Name="btnCancel" > <ContentPresenter /> </TextBlock> </ControlTemplate> </Button.Template> <Button.Style> <Style TargetType="Button"> <Setter Property="Foreground" Value="Blue" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Red" /> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 前台代碼 private void btnInput_Click(object sender, RoutedEventArgs e) { DataRowView drv = this.dgData.CurrentItem as DataRowView; if (drv != null) { string ID = drv.Row["ID"].ToString(); } } private void btnCancel_Click(object sender, RoutedEventArgs e) { DataRowView drv = this.dgData.CurrentItem as DataRowView; if (drv != null) { string ID = drv.Row["ID"].ToString(); } } 後台代碼剛開始接觸WPF,如有不妥之處望指正。