来自WPF的初学者沮丧,尤其是在数据绑定上

时间:2013-01-09 06:26:14

标签: wpf vb.net data-binding

首先,我真的是初学者。我主要使用WinForm,所以我从来没有听说过WPF上的数据绑定。

试图从博客和MSDN中学习,但我无法掌握。数据绑定只是我对WPF的困惑之一,但这是我现在需要了解的主要内容。

说我得到了这些课程:     CustomerDL.vb(数据访问层)     CustomerBL.vb(业务层)     FormCustomer.xaml(表示层)

我现在所做的,是我学到的唯一概念:DL - > BL - > PL。

这是我的PL:

Public Class FrmEmployee2
    Public Sub New()
        InitializeComponent()

        MasterEmployeeBL = New MasterEmployeeBL
        Employees = MasterEmployeeBL.FetchAllEmployee()

        MainGrid.DataContext = Employees
    End Sub

    Private _employees As List(Of Employee)
    Public Property Employees() As List(Of Employee)
        Get
            Return _employees
        End Get
        Set(ByVal value As List(Of Employee))
            _employees = value
        End Set
    End Property

    Public MasterEmployeeBL As MasterEmployeeBL
End Class

这是我的WPF:

<dx:DXWindow x:Class="FrmFindEmployee2"
    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:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
    xmlns:dxd="http://schemas.devexpress.com/winfx/2008/xaml/docking"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
    xmlns:local="clr-namespace:BMT_WPF"
    dx:ThemeManager.ThemeName="MetropolisDark"
    Title="Find Employee" Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}" Width="658"
    WindowStartupLocation="CenterScreen" SizeToContent="Width">

    <Window.Resources>
        <ResourceDictionary>
            <local:BooleanToStatusConverter x:Key="BoolToStatusConv" />
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/BMT-WPF;component/Helpers/EditStyles.xaml" />    
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid x:Name="MainGrid" DataContext="{Binding Source=Employees}">
        <DockPanel>
            <dxg:GridControl ItemsSource="{Binding}" AutoPopulateColumns="True">

            </dxg:GridControl>
        </DockPanel>
    </Grid>
</dx:DXWindow>

我认为这足以将Employees List绑定到我的GridControl,但它没有显示任何内容。

任何人都可以从初学者的角度帮助指出我可以从哪些方面了解WPF吗?

对不起,很长的帖子。干杯! :)

1 个答案:

答案 0 :(得分:1)

您已将MainGrid的DataContext设置两次,一次在代码中,一次在XAML中。我建议删除此元素上的XAML绑定。

关于数据绑定的基础教程,试试我写的这篇博文:

http://www.scottlogic.co.uk/blog/colin/2012/04/everything-you-wanted-to-know-about-databinding-in-wpf-silverlight-and-wp7-part-one/