从主窗口传递数据> tabitem>滚动条>框架>内容

时间:2017-07-15 17:15:12

标签: wpf vb.net

我已经问了这个问题,但现在我对我尝试做的事情有了更好的理解。我有一个mainwindow类的WPF应用程序。从那里,可以创建tabitems。例如,我有一个account标签。我的问题是,我想将account.xaml用于"编辑帐户' "添加帐户"按钮。如何判断account.xaml.vb是处于编辑模式还是添加模式?同样 - 如何在{#1}编辑帐户"时显示的对话框中告诉account.vb正在编辑的帐户?点击了吗?

以下是在mainwindow.xaml.vb

中创建的标签
    Private Sub btn_AddAccount_Click(sender As Object, e As   
    RoutedEventArgs) Handles btn_AddAccount.Click

    Dim tab_NewAccount As New C1TabItem()
    Dim frame_NewAccount As New Frame()
    Dim scroller_NewAccount As New ScrollViewer()
    Dim str_Name As String = "Add Account"
    Dim str_NavigationLink As String = "PM_AddAccount.xaml"
    Dim account As New PM_AddAccount
    account.mode = 1

    'Add and name new tab
    tab_NewAccount.Header = tabcontrol.Items.Count + 1 & ". " & str_Name
    tab_NewAccount.CanUserClose = True
    tabcontrol.Items.Add(tab_NewAccount)

    'Add frame to the tab and include new page
    With frame_NewAccount
        .NavigationService.Navigate(New Uri(str_NavigationLink, UriKind.Relative))
        .HorizontalAlignment = HorizontalAlignment.Stretch
        .VerticalAlignment = VerticalAlignment.Top
        .Margin = New Thickness(0, 0, 0, 0)
    End With

    With scroller_NewAccount
        .CanContentScroll = vbTrue
        .VerticalScrollBarVisibility = ScrollBarVisibility.Auto
        .HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
        .Content = frame_NewAccount
    End With

    tab_NewAccount.Content = scroller_NewAccount
    ' Set new tab as active tab
    tabcontrol.SelectedIndex = tabcontrol.Items.IndexOf(TAB)

End Sub

编辑: 所以我尝试给Account.xaml.vb一个公共属性,然后在mainwindow.xaml.vb中设置它但是,值没有设置 - 我意识到account中的mainwindow.xaml.vb变量除了frame.content = account之外,其他任何东西都没有连接。

Class Account

Public Property mode As AccountMode
    Get
        Return mode
    End Get
    Set(value As AccountMode)
        value = mode
    End Set
End Property

Public Enum AccountMode
    None = 0
    Add = 1
    Edit = 2
End Enum

End Class

1 个答案:

答案 0 :(得分:0)

定义一个名为AccountMode

的枚举
Public Enum AccountMode
    None = 0
    Add = 1
    Edit = 0
End Enum

将属性添加到名为Mode的account.xaml.vb并根据需要进行适当设置。

关于如何告诉它正在编辑哪个帐户,该问题含糊不清。通常,您将控件绑定到数据源,并且该数据源将具有某种键或标识符,使其与所有其他记录不同。例如,如果要从数据库绑定数据,则会有主键和/或标识字段。

您的数据将绑定到这样的模型类:

Public Class Account
    [Key]
    Public Property Id As Integer
    Public Property FirstName As String
    Public Property LastName As String
End Class

同样,如果Account.Id = 0,您可以合并逻辑来检测您正在添加新帐户,否则如果Account.Id> 0,那么您正在编辑现有帐户。