初学者对WPF的失望,特别是在数据绑定方面



首先,我真的是一个初学者。我主要是在WinForm上工作,所以我从来没有听说过像WPF这样的数据绑定。

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

假设我有这些类:vb(数据访问层)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 ?

很抱歉写了这么久。干杯!:)

您已经设置了两次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/

最新更新