【问题标题】:Winrt Xaml Toolkit bind to sub property of controlWinrt Xaml Toolkit 绑定到控件的子属性
【发布时间】:2015-10-05 18:41:03
【问题描述】:

我正在使用 Winrt Xaml 工具包进行自定义 PieChart,但我很难绑定到我的主要元素的属性。代码如下:

<Charting:Chart x:Name="PieChart" Title="Pie Title" Width="300" Height="300">
    <Charting:Chart.Series>
        <Charting:PieSeries Title="Population" IndependentValueBinding="{Binding Name}" DependentValueBinding="{Binding Amount}" IsSelectionEnabled="False" Width="125" Height="125" />
    </Charting:Chart.Series>
    <Charting:Chart.LegendStyle>
        <Style TargetType="datavis:Legend">
            <Setter Property="ItemContainerStyle" xmlns:series="using:WinRTXamlToolkit.Controls.DataVisualization.Charting">
                <Setter.Value>
                    <Style TargetType="series:LegendItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="series:LegendItem">
                                    <StackPanel Orientation="Horizontal">
                                        <datavis:Title Content="{TemplateBinding Content}" />
                                        <TextBlock Text="{Binding Amount}" />
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    </Charting:Chart.LegendStyle>
</Charting:Chart>

在我的LegendItem 中,我有一个TextBlock,我想将Charting:PieSeries 元素中的DependentValueBinding 属性绑定到它。我怎样才能做到这一点?我也尝试引用元素Self,但无法获取子属性。

【问题讨论】:

    标签: c# xaml windows-runtime winrt-xaml-toolkit


    【解决方案1】:

    您是否尝试设置PieSeriesName 并通过ElementName 绑定?

    您的代码示例片段如下:

      <Charting:Chart.Series>
        <Charting:PieSeries x:Name="MySeries" Title="Population" IndependentValueBinding="{Binding Name}" DependentValueBinding="{Binding Amount}" IsSelectionEnabled="False" Width="125" Height="125" />
      </Charting:Chart.Series>
    
    
      <ControlTemplate TargetType="series:LegendItem">
        <StackPanel Orientation="Horizontal">
            <datavis:Title Content="{TemplateBinding Content}" />
            <TextBlock Text="{Binding DependentValueBinding, ElementName=MySeries}" />
        </StackPanel>
      </ControlTemplate>
    

    【讨论】:

    • 我想它快到了,它会打印一个带有Windows.UI.Xaml.Data.Binding 的字符串。我正在尝试该元素的几个属性,但到目前为止我只设法获得了绑定名称,即Amount.
    • 抱歉,DependentValueBinding 是 Binding 类型,因此您不能通过 ElementName 执行此操作。您能告诉我如何设置 ItemsSource 或将项目添加到您的饼系列吗?你的图表是否显示系列?您当前进度的屏幕截图将很有用。
    • 你可以在这里看看你问了什么:http://pasteboard.co/19QoBKn7.png
    【解决方案2】:

    如果您调试 PieSeries 的可视化树 - 您会看到 LegendItemDataContextPieDataPoint 而不是您的项目,因此它不会有 Amount 属性。 Title 元素也有PieDataPoint 作为DataContext,但Content="{TemplateBinding Content}" 部分使其ContentPresenterDataContext 成为您的项目。也许你可以这样做:

    <StackPanel Orientation="Horizontal">
        <datavis:Title Content="{TemplateBinding Content}" />
        <TextBlock DataContext="{TemplateBinding Content}" Text="{Binding Amount}" />
    </StackPanel>
    

    【讨论】:

    • 有道理,但不起作用。 Amount 未找到属性。
    • 如果我这样做:DataContext="{TemplateBinding Content}" Text="{Binding DependentValue}" 我得到了值0,所以我想我只是错过了正确的DataContext
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多