如何在DataGrid中更改Hyperlink的字体颜色

时间:2014-12-25 14:50:06

标签: c# wpf xaml wpf-controls wpfdatagrid

如何在WPF(C#)中更改DataGrid中的超链接链接(字体)的颜色?

目前,我的Hyperlink列如下所示:(XAML)

<DataGridHyperlinkColumn Width="300" Binding="{Binding link}" ContentBinding="{Binding link}" 
                         Header="Link" HeaderStringFormat="Link">
    <DataGridHyperlinkColumn.ElementStyle>
        <Style>                            
            <EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/>                            
        </Style>                        
    </DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>

1 个答案:

答案 0 :(得分:3)

试试这个

<强> XAML

 <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridHyperlinkColumn  Header="Email" Binding="{Binding Email}">
            <DataGridHyperlinkColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="Background" Value="Transparent"></Setter>
                    <Setter Property="BorderThickness" Value="0"></Setter>
                    <Style.Resources>
                        <Style TargetType="Hyperlink">
                            <Setter Property="Foreground" Value="Chocolate"></Setter>
                            <Style.Triggers>
                                <EventTrigger RoutedEvent="Hyperlink.Click">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetProperty="Foreground.Color" From="Chocolate" To="BlueViolet" Duration="0:0:0.1"></ColorAnimation>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Style.Triggers>
                        </Style>
                    </Style.Resources>
                </Style>
            </DataGridHyperlinkColumn.CellStyle>
        </DataGridHyperlinkColumn>
    </DataGrid.Columns>
</DataGrid>

c#c​​ode

   public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        ObservableCollection<Customer> custdata = new ObservableCollection<Customer>();
        custdata.Add(new Customer() { Email = new Uri("http://stackoverflow.com/") });
        DG1.ItemsSource = custdata;
    }
}
public class Customer
{
    public Uri Email { get; set; }
}

<强>结果

enter image description here

enter image description here