【问题标题】:WPF Itemcontrol and datatemplate do not show properlyWPF Itemscontrol n 数据模板未正确显示
【发布时间】:2015-04-19 12:58:27
【问题描述】:

我在使用 ItemControl、DataTemplate 和 Visifire 图表时遇到问题。

首先,XAML中的代码如下

<DataTemplate x:Key="markerChartTemplate">
    <vc:Chart Height="200" Theme="Theme1" Style="{StaticResource ChartStyle}">
        <vc:Chart.Series>
            <vc:DataSeries RenderAs="Line" LightWeight="true" ShadowEnabled="false" LightingEnabled="false" MarkerSize="4" LineThickness="1" DataPoints="{Binding _xAxisCollection}" />
            <vc:DataSeries RenderAs="Line" LightWeight="true" ShadowEnabled="false" LightingEnabled="false" MarkerSize="4" LineThickness="1" DataPoints="{Binding _yAxisCollection}" />
            <vc:DataSeries RenderAs="Line" LightWeight="true" ShadowEnabled="false" LightingEnabled="false" MarkerSize="4" LineThickness="1" DataPoints="{Binding _zAxisCollection}" />
        </vc:Chart.Series>
    </vc:Chart>
</DataTemplate>

<ScrollViewer HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto">              
    <ItemsControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=_markerChartsCollections}" ItemTemplate="{StaticResource markerChartTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</ScrollViewer>

_markerChartsCollections 定义为

public ObservableCollection<My3DLineChartObject> _markerChartsCollections

在 ViewModel 和 Model 中。

现在,在 Model 中,我初始化两个 My3DLineChartObject 对象并将其放入 _markerChartsCollections

结果是我可以看到两个,与 _markerChartsCollections 中的 My3DLineChartObject 相同。但My3DLineChartObject中的_xAxisCollection、_yAxisCollection、_zAxisCollection等属性无法显示。

谁能帮忙看看可能的原因是什么?

输出信息是

'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero2\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero2.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_32\Microsoft.Kinect\v4.0_2.0.0.0__31bf3856ad364e35\Microsoft.Kinect.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\Users\admin\Desktop\Hand\SkeletonMarkerCapture_Good_2\SkeletonMarkerCapture\bin\Debug\AForge.Imaging.dll'. Cannot find or open the PDB file.
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\Users\admin\Desktop\Hand\SkeletonMarkerCapture_Good_2\SkeletonMarkerCapture\bin\Debug\Emgu.CV.dll'. Module was built without symbols.
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\Users\admin\Desktop\Hand\SkeletonMarkerCapture_Good_2\SkeletonMarkerCapture\bin\Debug\Emgu.Util.dll'. Module was built without symbols.
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\Users\admin\Desktop\Hand\SkeletonMarkerCapture_Good_2\SkeletonMarkerCapture\bin\Debug\AForge.dll'. Cannot find or open the PDB file.
The thread 0x75d4 has exited with code 259 (0x103).
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXmlLinq\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXmlLinq.dll'. Cannot find or open the PDB file.
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXml\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXml.dll'. Cannot find or open the PDB file.
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\Users\admin\Desktop\Hand\SkeletonMarkerCapture_Good_2\SkeletonMarkerCapture\bin\Debug\GalaSoft.MvvmLight.dll'. Symbols loaded.
'SkeletonMarkerCapture.vshost.exe' (CLR v4.0.30319: SkeletonMarkerCapture.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.`enter code here`0__31bf3856ad364e35\UIAutomationTypes.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

【问题讨论】:

  • 确保它们是公开的属性而不是字段/成员
  • 检查输出窗口是否存在运行时发生的数据绑定错误,并在此处添加
  • 您好 gldraphael,输出窗口已附加。我看不到任何与数据绑定有关的错误。

标签: c# wpf datatemplate itemsource visifire


【解决方案1】:

我在下面包含了我的原始答案中的 cmets,但是在玩了一点 Visifire 之后,您实际上可能遇到了库设计的问题(我从几年前开始使用它就开始记住这一点)。图表上公开的许多 DependencyProperties 无法绑定。我制定了一个像你这样的例子并发布了here。具体来说,与 DataPoints 的绑定是在内部管理的。你真正想要的是绑定数据源。您还需要设置 YValue 映射即使您在 ViewModel 中使用它们的 DataPoint 类型

例如,您的 DataTemplate 将需要像这样绑定您的点集合

<DataTemplate x:Key="MarkerChartTemplate">
    <vc:Chart Height="200" Width="400">
       <vc:Chart.Series>
            <vc:DataSeries RenderAs="Line" DataSource="{Binding XAxisCollection}">
                <vc:DataSeries.DataMappings>
                    <vc:DataMapping MemberName="YValue" Path="YValue"/>
                </vc:DataSeries.DataMappings>
            </vc:DataSeries>
        </vc:Chart.Series>
    </vc:Chart>
</DataTemplate>

您应该查看Visifire docs 了解更多详情。不幸的是,文档并没有很明确地说明其他 DependencyProperty 更新在库中无效。


您应该检查的其他事项和一般良好做法:

您似乎正在尝试使用 Visifire 图表来显示 3 系列图表的 ItemsControl。有很多事情可能会出错。这是我建议看的内容。

首先,检查您的绑定是否正常工作。也许您忘记了公开某些内容。也许您的系列没有显示,因为它们没有通知属性,并且您在最初绑定时没有设置它们。拥有一个复杂的 DataTemplate 一开始只会让事情变得模糊,所以尝试一些更简单的东西,比如

<DataTemplate x:Key="SimplerTemplate">
    <UniformGrid Columns="3">
        <TextBlock Text="{Binding _xAxisCollection}"/>
        <TextBlock Text="{Binding _yAxisCollection}"/>
        <TextBlock Text="{Binding _zAxisCollection}"/>
    </UniformGrid>
</DataTemplate>

假设您这样做,并且您在每个 TextBlock 中看到一个字符串,该字符串对应于 Visifire.Charts.DataPointCollection。那太棒了。这意味着您的 DataBinding 将真正起作用。在这种情况下,我会发布更多关于您如何定义 DataSeries 的示例代码,以便更熟悉 Visifire 的人可以提供帮助(在这种情况下,您也应该标记 Visifire)。

但是,假设它不起作用。您可以从 Visual Studio(您的输出窗口)中检查一件有用的事情。还有一些方法可以让你的程序更容易调试。

  • 检查控制台输出是否存在 System.Windows.Data 绑定错误。如果有错字或您的字段不可访问,WPF 将记录一个错误。
  • 不要绑定到原始字段。将您的 ObservableCollections 设为私有且只读。用吸气剂包裹它们。您要确保没有任何其他代码,尤其是在 XAML 中定义的 DataBindings 在后台交换您的集合。使用 TwoWay 绑定,不难犯这个错误。
  • 在 My3DLineChartObject 上 - 同样,用 getter 属性包装您的三个系列并绑定到它们。
  • 在您的 ViewModel 和 My3DLineChartObject 上实施 INotifyPropertyChanged。每当您更改 My3DLineChartObject 中的系列字段之一时,您还需要引发 NotifyPropertyChanged("...AxisCollection")。如果您从不更改它们,则应该将它们设为只读并提供一个吸气剂。同样,这避免了它们被 TwoWay 绑定换出。
  • 在 AxisCollection getter 中放置断点。当您运行应用程序并显示此视图时,是否会调用这些 getter?数据绑定使用反射,它会调用你的 getter 属性。

希望对您有所帮助。

【讨论】:

  • 嗨 timmyl,感谢您的 cmets。我把SimplerTemplate放到我的代码中,得到了三个DataPointCollection对应的(Collection)(Collection)(Collection)。这是你预期的吗?谢谢。
  • 是的,听起来您的绑定正在工作。我认为问题很可能与 Visifire 库有关。它们不支持直接绑定 DataPoints 属性。看看我在这里举的例子:github.com/tlantz/stackoverflow-answers/tree/master/…
  • 您好 timmyl,感谢您的代码。我认为它现在正在工作。
猜你喜欢
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 2019-02-14
  • 2014-07-04
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
相关资源
最近更新 更多