【问题标题】:Styling hyperlink button inside a data template in Silverlight在 Silverlight 中的数据模板内设置超链接按钮的样式
【发布时间】:2010-05-13 17:49:47
【问题描述】:

如何更改数据模板中超链接按钮的视觉状态?基本上我正在尝试遍历超链接按钮并根据当前 url 将它们的视觉状态设置为活动或非活动。超链接按钮位于项目控件的数据模板内。 itemsource 绑定到一个 List,其中 link 是我的自定义链接类,它只包含 uri 和标题的一些属性。

在silverlight 导航应用程序中设置活动超链接的样式是否有最佳做法?我使用的是silverlight nav app项目模板的样板代码的方法。

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    我认为实现这一点的最佳方法是将 HyperlinkBut​​tons 的 IsEnabled 属性绑定到您的 URL 并在两者之间放置一个转换器。

    我假设你有这样的东西:

    <ListBox
      ItemsSource="{Binding Path=Links}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <HyperlinkButton
            NavigateUri="{Binding Path=LinkUrl}"
            Content="{Binding Path=LinkUrl}" />
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    

    所以你可以像这样添加 IsEnabled 绑定:

    <ListBox
      ItemsSource="{Binding Path=Links}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <HyperlinkButton
            IsEnabled="{Binding Path=LinkUrl, Converter={StaticResource LinkUrlToIsEnabledConverter}}"
            NavigateUri="{Binding Path=LinkUrl}"
            Content="{Binding Path=LinkUrl}" />
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    

    然后在转换器中执行例如:

    public object Convert(...)
    {
      var url = (Uri)value;
      if (url.AbsolutePath.EndsWith(".html"))
        return true;
      return false;
    }
    

    干杯,亚历克斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      相关资源
      最近更新 更多