Silverlight 工具包图表:将超链接分配给轴

发布于 2024-10-27 01:43:36 字数 589 浏览 3 评论 0原文

我有一个简单的 Silverlight Toolkit 图表,它绑定到以下类型的集合:

public class ChartItemClass
{
    public string Name { get; set; }
    public double Value { get; set; }
    public string Url { get; set; }
}

我可以获得一个图表来正确显示名称(X 轴)和值(Y 轴),但我想要 X 上的标签-axis 是 Url 属性的 HyperlinkBut​​tons。 X 轴标签应该类似于以下内容:

<HyperlinkButton Content="*Name Property Here*" NavigateUri="*Url Property Here*" TargetName="_blank"></HyperlinkButton>

我找到了一个示例,它允许我为 X 轴设置 AxisLabelStyle,因此标签现在是 HyperlinkBut​​ton。问题是我无法将 Url 属性分配/绑定为 NavigateUri。有什么想法吗?

I have a simple Silverlight Toolkit Chart which is bound to a collection of the following type:

public class ChartItemClass
{
    public string Name { get; set; }
    public double Value { get; set; }
    public string Url { get; set; }
}

I can get a Chart to display the Name (X-axis) and Value (Y-axis) correctly, but I would like the labels on the X-axis to be HyperlinkButtons to the Url property. The X-axis label should be something like the following:

<HyperlinkButton Content="*Name Property Here*" NavigateUri="*Url Property Here*" TargetName="_blank"></HyperlinkButton>

I found an example which allowed me to set the AxisLabelStyle for the X-axis so the Labels are now HyperlinkButtons. The problem is I haven't been able to assign/bind the Url property as the NavigateUri. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有伤那来痛 2024-11-03 01:43:36

首先我将发布完整的代码,然后是解释。

   <UserControl.Resources>
    <Style x:Key="hyperlinkStyle" TargetType="charting:AxisLabel">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="charting:AxisLabel">
                    <HyperlinkButton Content="{Binding Name}" NavigateUri="{Binding Url}" TargetName="_blank"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<charting:Chart>
    <charting:Chart.Series>
        <charting:ColumnSeries ItemsSource="{Binding Items}" DependentValueBinding="{Binding Value}" IndependentValueBinding="{Binding}">
            <charting:ColumnSeries.IndependentAxis>
                <charting:CategoryAxis Orientation="X" AxisLabelStyle="{StaticResource hyperlinkStyle}" />
            </charting:ColumnSeries.IndependentAxis>
        </charting:ColumnSeries>
    </charting:Chart.Series>
</charting:Chart>

诀窍在于这一行:

IndependentValueBinding="{Binding}"

使用此绑定,您可以将整个对象传递到独立轴,而不仅仅是一个属性。
之后,您可以在标签的控件模板中获取绑定对象的属性:

Content="{Binding Name}" NavigateUri="{Binding Url}"

Binding 关键字而不是 TemplateBinding 看起来很奇怪,但它是允许的并且有效。
还有一点需要注意:Url 属性必须包含 http 前缀。它不适用于 www

At first I will post the complete code and after that the explanation.

   <UserControl.Resources>
    <Style x:Key="hyperlinkStyle" TargetType="charting:AxisLabel">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="charting:AxisLabel">
                    <HyperlinkButton Content="{Binding Name}" NavigateUri="{Binding Url}" TargetName="_blank"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<charting:Chart>
    <charting:Chart.Series>
        <charting:ColumnSeries ItemsSource="{Binding Items}" DependentValueBinding="{Binding Value}" IndependentValueBinding="{Binding}">
            <charting:ColumnSeries.IndependentAxis>
                <charting:CategoryAxis Orientation="X" AxisLabelStyle="{StaticResource hyperlinkStyle}" />
            </charting:ColumnSeries.IndependentAxis>
        </charting:ColumnSeries>
    </charting:Chart.Series>
</charting:Chart>

The trick is in this line:

IndependentValueBinding="{Binding}"

Using this binding you pass a whole object to the independent axis, not just a property.
And after that you can get properties of a bound object in the control template of label:

Content="{Binding Name}" NavigateUri="{Binding Url}"

The Binding keyword instead of the TemplateBinding looks strange, but it is permitted and it works.
And there is one remark: the Url property must contain the http prefix. It doesn't work with www.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文