【问题标题】:Devexpress DXCharts (refresh, update, clear) new instance?Devexpress DXCharts(刷新、更新、清除)新实例?
【发布时间】:2011-11-11 18:05:24
【问题描述】:

我将 wpf 和 c# 与第三方 DevExpress 库一起使用。我在使用 DXCharts 时遇到问题。我尝试了一些不同的方法来清除或更新图表,但无济于事。我正在将数据绑定到具有数据源依赖属性的数据表(动态构建)。

当新数据设置为支持属性时,图表的依赖属性似乎没有被覆盖。这给了我图表上的重叠点。正如您在下面的示例中所见。

第一组数据

第二组数据

我还尝试创建 Chart 控件的新实例,它仍然显示旧的绑定依赖属性。 DXchart 用户控件嵌入到内容控件中。我通过内容属性绑定图表。所有这些都嵌套在 DevExpress 选项卡控件下。

下面是一些代码:

依赖属性

public static readonly DependencyProperty DataTableChartProperty = DependencyProperty.Register
        ("DataTableChart", typeof(DataTable), typeof(MainWindowViewModel));

public static readonly DependencyProperty ContentElementProperty = DependencyProperty.Register
        ("ContentElement", typeof(FrameworkElement), typeof(MainWindowViewModel));

支持属性

public DataTable DataTableChart
    {
        get { return (DataTable)this.GetValue(DataTableChartProperty); }
        set { this.SetValue(DataTableChartProperty, value); }

public FrameworkElement ContentElement
    {
        get { return (FrameworkElement)this.GetValue(ContentElementProperty); }
        set { this.SetValue(ContentElementProperty, value); }
    }

用户控制

<UserControl x:Class="Reporting_DIMS.UI.ChartControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
         xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
         xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"
         xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
         xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
         mc:Ignorable="d"
         d:DesignHeight="700" d:DesignWidth="1100">
<Grid>
    <Border Padding="3">
        <dxc:ChartControl Margin="0" Name="chartControl" DataSource="{Binding DataTableChart}">
            <dxc:ChartControl.Diagram>
                <dxc:XYDiagram2D SeriesDataMember="DIMS User">
                    <dxc:XYDiagram2D.SeriesTemplate>
                        <dxc:BarSideBySideSeries2D ValueDataMember="Count" ArgumentDataMember="Entry DateTime" />
                    </dxc:XYDiagram2D.SeriesTemplate>
                </dxc:XYDiagram2D>
            </dxc:ChartControl.Diagram>
            <dxc:ChartControl.Legend>
                <dxc:Legend x:Name="legend"/>
            </dxc:ChartControl.Legend>
        </dxc:ChartControl>
    </Border>
</Grid>

小主窗口部分

<dx:DXTabItem Header="Log Charts" Name="dXTabItem2">
                    <ContentControl x:Name="contentControl" Content="{Binding     ContentElement}"/>
                </dx:DXTabItem>

如果有人有任何想法,我将不胜感激。提前致谢!

【问题讨论】:

  • 我发现问题与依赖属性完全无关。我在正在迁移到新数据表的数据集中有过时的数据。我刚刚创建了一个新实例,一切都很好。

标签: c# wpf charts devexpress dependency-properties


【解决方案1】:

reeves 是正确的。对我来说一个简单的解决方法是将我的 DXChartControl 包装在第二个 ContentControl 中。不是直接绑定到 ChartControl,而是让内部的 ContentControl 将绑定交给 ChartControl,这样,当内容更改时,ContentControl 会通过 ContentTemplate 生成一个新实例。

    <DataTemplate x:Key="chartTemplate">
        <dex:ChartControl DataSource="{Binding}" DataContextChanged="chartControl_DataContextChanged_1"/>
    </DataTemplate>     
    <ContentControl  Grid.Row="1" ContentTemplate="{StaticResource ResourceKey=chartTemplate}" Content="{Binding 'YOUR ITEMSSOURCE'}"/>

然后你可以在 DataContextChanged 中重新构建图表。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我最终删除了旧的图表对象并创建了一个新对象。这是每个 DevExpress 图表的预期行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      相关资源
      最近更新 更多