2015-02-05 202 views
0

我有使用WPF和vb.net进行数据绑定的问题。其实,我尝试使用WPF(xaml)和C#,它工作得很好。但是,当我想使用vb.net构建它时,我遇到了一个问题,即数据是空的,没有显示任何内容。因此,我非常肯定,它与WPF和VB.net之间的数据绑定有关。如果有人能够发现我在编码过程中发生的错误,我将非常感激。提前致谢。 的VB.net代码:数据绑定WPF和vb

Imports System.Collections.ObjectModel 
Imports System.Windows 
Imports System.Windows.Controls 
Imports System.IO 


Namespace chartTest 
    Public Class MainWindow 
     Public Sub New() 
      InitializeComponent() 

      Dim Errors As New TestPageViewModel() 
      Errors = Me.DataContext 
     End Sub 

     Public Class TestPageViewModel 
      Public m_Errors As ObservableCollection(Of DataChart) 
      Public Property Errors() As ObservableCollection(Of DataChart) 
       Get 
        Return m_Errors 
       End Get 
       Private Set(value As ObservableCollection(Of DataChart)) 
        m_Errors = value 
       End Set 
      End Property 



      Public Sub addDataChart() 
       Errors = New ObservableCollection(Of DataChart)() 
       Errors.Add(New DataChart() With { _ 
        .Category = "Globalization", _ 
        .Number = 75 _ 
       }) 

       Errors.Add(New DataChart() With { _ 
        .Category = "Features", _ 
        .Number = 2 _ 
       }) 

       Errors.Add(New DataChart() With { _ 
        .Category = "ContentTypes", _ 
        .Number = 12 _ 
       }) 

       Errors.Add(New DataChart() With { _ 
        .Category = "Correctness", _ 
        .Number = 83 _ 
       }) 

       Errors.Add(New DataChart() With { _ 
        .Category = "Best Practices", _ 
        .Number = 29 _ 
       }) 

      End Sub 

      Private m_selectedItem As Object = Nothing 
      Public Property SelectedItem() As Object 
       Get 
        Return m_selectedItem 
       End Get 
       Set(value As Object) 
        m_selectedItem = value 
       End Set 
      End Property 
     End Class 

     ' class which represent a data point in the chart 
     Public Class DataChart 
      Private m_Category As String 
      Public Property Category() As String 
       Get 
        Return m_Category 
       End Get 
       Set(value As String) 
        m_Category = value 
       End Set 
      End Property 

      Private m_Number As Integer 
      Public Property Number() As Integer 
       Get 
        Return m_Number 
       End Get 
       Set(value As Integer) 
        m_Number = value 
       End Set 
      End Property 

     End Class 
    End Class 
End Namespace 

的XAML代码如下:

<Window x:Class="chartTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:chart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart" 
    xmlns:local="clr-namespace:chartTest.chartTest" 
     > 
    <Window.Resources> 

     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Generic.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Window.Resources> 


    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="73*"/> 
      <RowDefinition Height="7*"/> 
     </Grid.RowDefinitions> 

     <chart:PieChart 

     ChartTitle="Minimal Pie Chart" 
     ChartSubTitle="Chart with fixed width and height" 
     SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" Grid.RowSpan="2" > 
      <chart:PieChart.Series> 
       <chart:ChartSeries 
       SeriesTitle="Errors" 
       DisplayMember="Category" 
       ValueMember="Number" 
       Width="400" 
       Height="400" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       ItemsSource="{Binding Path=Errors}"/> 

      </chart:PieChart.Series> 
     </chart:PieChart> 

    </Grid> 
</Window> 

回答

0
Errors = Me.DataContext 

这应该是:

Me.DataContext = Errors 

您设置错误到的DataContext这是程序启动时没有任何

+0

我改变了它,但我仍然有一个空的图表。这意味着数据绑定不能从xaml正确调用。任何其他建议! – Mahmoud 2015-02-05 15:49:37