从Linq-To-SQL更新WPF窗口会出现什么问题? [英] What problems arise with this update a WPF Window from Linq-To-SQL?

查看:79
本文介绍了从Linq-To-SQL更新WPF窗口会出现什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将这种风格作为构建新应用程序的原型

主要关注的是我如何做我的 Partial Linq Class

到目前为止,这似乎工作正常......



(我担心随着来自未来子表数据的其他子总数会变得复杂由子窗口编辑。)



任何破坏性评论都表示赞赏!



Xaml主窗口

I'm considering this style as my prototype for building a new application
the main concern is how i'm doing my Partial Linq Class
This seems to work okay so far...

(I'm concerned it will become complicated with other sub-totals coming from future child table data that are edited by child windows.)

Any destructive comments are appreciated!

Xaml main window

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test Update" Height="350" Width="200" DataContext="{Binding}">
    <StackPanel Orientation="Vertical">
        <Label Content="Materials"/>
        <TextBox Text="{Binding Sale1.materials,StringFormat=C}"/>
        <Label Content="Labor"/>
        <TextBox Text="{Binding Sale1.labor,StringFormat=C}"/>
        <Label Content="Sub-total " Background="LightGray"/>
        <TextBox Text="{Binding Sale1.subtotal,Mode=OneWay,StringFormat=C}" removed="LightGray"/>
        <Label Content="Tax Rate"/>
        <TextBox Text="{Binding Sale1.taxrate}"/>
        <Label Content="Total" Background="LightBlue"/>
        <TextBox Text="{Binding Sale1.total,Mode=OneWay,StringFormat=C}" removed="LightBlue"/>
    </StackPanel>       
          
</Window>



我的主窗口代码背后


my mainwindow code behind

Class MainWindow 
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim vm As New MainViewModel
        Me.DataContext = vm
    End Sub
End Class



我的主窗口视图模型


my viewmodel for main window

Imports System.ComponentModel

Public Class MainViewModel
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Protected Sub OnPropertyChanged(ByVal name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub

    Private _Sale1 As Sale
    Public Property Sale1 As Sale
        Get
            If _Sale1 Is Nothing Then
                _Sale1 = New Sale  'some SAMPLE data to start
                With _Sale1
                    .gid = Guid.NewGuid
                    .labor = 1
                    .materials = 2
                    .taxrate = 8
                End With
            End If
            Return _Sale1
        End Get
        Set(value As Sale)
            _Sale1 = value
            OnPropertyChanged("Sale1")
        End Set
    End Property
End Class





我的部分Linq课程





My partial Linq class

Partial Class Sale
    Public ReadOnly Property total As Decimal
        Get
            Return Me.materials + (Me.materials * (Me.taxrate / 100)) + Me.labor
        End Get
    End Property

    Public ReadOnly Property subtotal As Decimal
        Get
            Return Me.materials + Me.labor
        End Get

    End Property
    Private Sub OnlaborChanged()
        UpdateCalculatedTotals()
    End Sub
    Private Sub OnmaterialsChanged()
        UpdateCalculatedTotals()
    End Sub
    Private Sub OntaxrateChanged()
        UpdateCalculatedTotals()
    End Sub

    Private Sub UpdateCalculatedTotals()
        SendPropertyChanged("subtotal")
        SendPropertyChanged("total")
    End Sub
End Class





Linq to SQL designer创建的类:





Linq to SQL designer created class:

Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Reflection


Partial Public Class linqSQLClassDataContext
	Inherits System.Data.Linq.DataContext
	
	Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource()
	
  #Region "Extensibility Method Definitions"
  Partial Private Sub OnCreated()
  End Sub
  Partial Private Sub InsertSale(instance As Sale)
    End Sub
  Partial Private Sub UpdateSale(instance As Sale)
    End Sub
  Partial Private Sub DeleteSale(instance As Sale)
    End Sub
  #End Region
	
	Public Sub New(ByVal connection As String)
		MyBase.New(connection, mappingSource)
		OnCreated
	End Sub
	
	Public Sub New(ByVal connection As System.Data.IDbConnection)
		MyBase.New(connection, mappingSource)
		OnCreated
	End Sub
	
	Public Sub New(ByVal connection As String, ByVal mappingSource As System.Data.Linq.Mapping.MappingSource)
		MyBase.New(connection, mappingSource)
		OnCreated
	End Sub
	
	Public Sub New(ByVal connection As System.Data.IDbConnection, ByVal mappingSource As System.Data.Linq.Mapping.MappingSource)
		MyBase.New(connection, mappingSource)
		OnCreated
	End Sub
	
	Public ReadOnly Property Sales() As System.Data.Linq.Table(Of Sale)
		Get
			Return Me.GetTable(Of Sale)
		End Get
	End Property
End Class

<Global.System.Data.Linq.Mapping.TableAttribute(Name:="")>  _
Partial Public Class Sale
	Implements System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
	
	Private Shared emptyChangingEventArgs As PropertyChangingEventArgs = New PropertyChangingEventArgs(String.Empty)
	
	Private _gid As System.Guid
	
	Private _materials As Decimal
	
	Private _labor As Decimal
	
	Private _taxrate As Decimal
	
    #Region "Extensibility Method Definitions"
    Partial Private Sub OnLoaded()
    End Sub
    Partial Private Sub OnValidate(action As System.Data.Linq.ChangeAction)
    End Sub
    Partial Private Sub OnCreated()
    End Sub
    Partial Private Sub OngidChanging(value As System.Guid)
    End Sub
    Partial Private Sub OngidChanged()
    End Sub
    Partial Private Sub OnmaterialsChanging(value As Decimal)
    End Sub
    Partial Private Sub OnmaterialsChanged()
    End Sub
    Partial Private Sub OnlaborChanging(value As Decimal)
    End Sub
    Partial Private Sub OnlaborChanged()
    End Sub
    Partial Private Sub OntaxrateChanging(value As Decimal)
    End Sub
    Partial Private Sub OntaxrateChanged()
    End Sub
    #End Region
	
	Public Sub New()
		MyBase.New
		OnCreated
	End Sub
	
	<Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_gid", DbType:="UniqueIdentifier NOT NULL", IsPrimaryKey:=true)>  _
	Public Property gid() As System.Guid
		Get
			Return Me._gid
		End Get
		Set
			If ((Me._gid = value)  _
						= false) Then
				Me.OngidChanging(value)
				Me.SendPropertyChanging
				Me._gid = value
				Me.SendPropertyChanged("gid")
				Me.OngidChanged
			End If
		End Set
	End Property
	
	<Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_materials")>  _
	Public Property materials() As Decimal
		Get
			Return Me._materials
		End Get
		Set
			If ((Me._materials = value)  _
						= false) Then
				Me.OnmaterialsChanging(value)
				Me.SendPropertyChanging
				Me._materials = value
				Me.SendPropertyChanged("materials")
				Me.OnmaterialsChanged
			End If
		End Set
	End Property
	
	<Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_labor")>  _
	Public Property labor() As Decimal
		Get
			Return Me._labor
		End Get
		Set
			If ((Me._labor = value)  _
						= false) Then
				Me.OnlaborChanging(value)
				Me.SendPropertyChanging
				Me._labor = value
				Me.SendPropertyChanged("labor")
				Me.OnlaborChanged
			End If
		End Set
	End Property
	
	<Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_taxrate")>  _
	Public Property taxrate() As Decimal
		Get
			Return Me._taxrate
		End Get
		Set
			If ((Me._taxrate = value)  _
						= false) Then
				Me.OntaxrateChanging(value)
				Me.SendPropertyChanging
				Me._taxrate = value
				Me.SendPropertyChanged("taxrate")
				Me.OntaxrateChanged
			End If
		End Set
	End Property
	
	Public Event PropertyChanging As PropertyChangingEventHandler Implements System.ComponentModel.INotifyPropertyChanging.PropertyChanging
	
	Public Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
	
	Protected Overridable Sub SendPropertyChanging()
		If ((Me.PropertyChangingEvent Is Nothing)  _
					= false) Then
			RaiseEvent PropertyChanging(Me, emptyChangingEventArgs)
		End If
	End Sub
	
	Protected Overridable Sub SendPropertyChanged(ByVal propertyName As [String])
		If ((Me.PropertyChangedEvent Is Nothing)  _
					= false) Then
			RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
		End If
	End Sub
End Class





-josh



-josh

推荐答案

这篇关于从Linq-To-SQL更新WPF窗口会出现什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆