【问题标题】:Silverlight HyperlinkButton styles in custom control自定义控件中的 Silverlight HyperlinkBut​​ton 样式
【发布时间】:2013-01-10 10:20:34
【问题描述】:

我有一个奇怪的样式问题,我无法弄清楚。 我想要实现的是,当鼠标悬停链接时,datagrid 单元格中的 HyperlinkBut​​tons 的样式带有下划线。

我有一列在 XAML 中声明 HyperlinkBut​​ton 元素,如下所示:

<HyperlinkButton Style="{StaticResource HyperlinkButtonStyle}" DataContext="{Binding}" FontSize="11" Content="{Binding DguNr}" Click="DgunrHyperlinkButtonClick" />

这很好用——链接的样式是我想要的。 在另一列中,我需要根据绑定元素中的一些信息显示 n 个 HyperlinkBut​​tons。因此,我创建了一个将呈现 0..n 超链接按钮的用户控件。控件在 XAML 中声明如下:

<sdk:DataGridTemplateColumn IsReadOnly="True" CanUserSort="True">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <bc:BoreholePlantGridColumn Plants="{Binding Plants, Mode=OneWay}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>

控件背后的代码如下所示:

public partial class BoreholePlantGridColumn : UserControl, INotifyPropertyChanged
{
    public BoreholePlantGridColumn()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(BoreholePlantGridColumn_Loaded);
    }

    void BoreholePlantGridColumn_Loaded(object sender, RoutedEventArgs e)
    {
        var borehole = (SelectableBoring)this.DataContext;
        foreach(var p in borehole.Plants)
        {
            // <HyperlinkButton HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" DataContext="{Binding}" Foreground="Black" FontSize="11" Content="{Binding DguNr}" Click="DgunrHyperlinkButtonClick" />
            var button = new HyperlinkButton();
            button.Content = p.PlantId;
            button.Style = (Style)App.Current.Resources["HyperlinkButtonStyle"];
            button.VerticalContentAlignment = VerticalAlignment.Center;
            var url = String.Format(Common.Constants.Url.GeusPlantLinkTemplate, p.PlantId);
            button.NavigateUri = new Uri(url, UriKind.RelativeOrAbsolute);
            button.TargetName = "_blank";
            LayoutRoot.Children.Add(button);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public static readonly DependencyProperty PlantsProperty = DependencyProperty.Register("Value", typeof(Anlaeg), typeof(BoreholePlantGridColumn), new PropertyMetadata(new PropertyChangedCallback(ValueChanged)));

    public IList<Anlaeg> Plants
    {
        get { return (IList<Anlaeg>)this.GetValue(PlantsProperty); }
        set { this.SetValue(PlantsProperty, value); }
    }

    private static void ValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var myUC = (BoreholePlantGridColumn)obj;
        var newValue = (IList<Anlaeg>)e.NewValue;


    }
}

这几乎可以按预期工作;链接按钮以正确的颜色呈现 - 但是当鼠标悬停在链接上时没有显示下划线文本。 我不明白为什么下划线显示在直接在 XAML 中声明的超链接按钮中,而不是在代码隐藏中呈现的超链接中。有人可以帮我解决这个问题吗?

我使用了这个线程中的超链接按钮样式:1

【问题讨论】:

    标签: silverlight xaml


    【解决方案1】:

    我只是使用以下代码

    <ListBox Grid.Column="1" ItemsSource="{Binding EncyclopediaList}" HorizontalContentAlignment ="Stretch" Margin="5,0" >
        <ListBox.ItemTemplate>
          <DataTemplate>
             <HyperlinkButton Content="{Binding Name}" Foreground="Black" Command="{Binding ViewArticlePageCommand, Source={StaticResource EncyclopediaViewModel}}" CommandParameter="{Binding ServerEncyclopediaID}" FontFamily="Arial" FontSize="18" />
          </DataTemplate>
       </ListBox.ItemTemplate>
    </ListBox>
    

    无需在我的案例中完美地设置 HyperlinkBut​​ton 的样式添加下划线并在鼠标移过它时使其变为粗体。

    【讨论】:

    • 是的 - 当在 XAML 中声明但不是从代码隐藏中添加时,它对我来说也很好。
    • 动态创建超链接按钮时,删除刚刚赋予超链接按钮的样式。超链接按钮会自动获取样式。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多