Visual Basic WPF

Status
Niet open voor verdere reacties.

Ophetwerk

Gebruiker
Lid geworden
12 mrt 2014
Berichten
15
Hallo Allemaal,

Dit mijn eerste vraag op dit forum. Ik heb nogal wat problemen met WPF.

Dus mijn eerste vraag is, is er iemand die kennis heeft van Visual Studio 2013 WPF (vb.net)
Ik hoop dat er iemand is..

Alvast bedankt
 
Welkom op Helpmij :)

Ik heb je vraag verplaatst van "Overige programmeertalen" naar "VB.net"
 
Wat zijn je problemen met WPF?

(ik doe niets met vb.net maar er had toch nog niemand gereageerd)
 
Toch vriendelijk van je dat reageert. We gebruiken hier een datagrid waarin alle mutaties worden verwerkt. Ik vroeg me af hoe je datgrid terug zet naar een Dataset. Om internet is dit moeilijk terug te vinden.
 
Het idee is dat je de datasource waar je gegevens in zitten databind op de controls die je gebruikt en niets gaat opbouwen met de gegevens die in je UI controls zitten. (je datagrid in dit geval)
De wijzigingen die je maakt zullen meteen in je source ook wijzigen zodat je niets terug hoeft te halen.

Heb je wat code/xaml die je nu gebruikt?
 
Dit komt eraan. Moet helaas even over naar Windows 8.1.
En dat zijn een updates je wordt er moe van.
 
Ik heb een voorbeeld gemaakt:


Code:
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication4"
        
    Title="MainWindow" Height="300" Width="650">

    <Canvas>
        <DataGrid x:Name="DG" Canvas.Left="10" Canvas.Top="10"
            
            CanUserDeleteRows="False"
            CanUserAddRows="False"     
                  
            CellEditEnding="DG_CellEditEnding" 
            RowEditEnding="DG_RowEditEnding"
   
                 
            ItemsSource="{Binding }" AutoGenerateColumns="False" >

            <DataGrid.RowValidationRules>
                <local:PersonValidationRule ValidationStep="UpdatedValue" />
            </DataGrid.RowValidationRules>

            <DataGrid.RowValidationErrorTemplate>
                <ControlTemplate>
                    <Grid Margin="0,-2,0,-2"
                          ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
                          Path=(Validation.Errors)[0].ErrorContent}">
                        <Ellipse StrokeThickness="0" Fill="RED" 
                            Width="{TemplateBinding FontSize}" 
                            Height="{TemplateBinding FontSize}" />
                        <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" 
                            FontWeight="Bold" Foreground="White" 
                            HorizontalAlignment="Center"  />
                    </Grid>
                </ControlTemplate>
            </DataGrid.RowValidationErrorTemplate>

            <DataGrid.Columns>
                <DataGridTextColumn x:Name="Status" Header="Status" Width="100" Binding="{Binding Status}"/>
                <DataGridTextColumn Header="PersonId" Width="175" Binding="{Binding ID}"/>
                <DataGridTextColumn Header="Name" Width="175" Binding="{Binding Name}"/>
                <DataGridComboBoxColumn Header="Country" x:Name="DataColumnCountry" 
                                        
                    SelectedValueBinding="{Binding Path=CountryId}"
                    SelectedValuePath="CountryId"  
                    DisplayMemberPath="Country ">
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Button" Height="32" Canvas.Left="550" Canvas.Top="193" Width="49" Click="Button_Click_1"/>
        <Button Content="New" Height="29" Canvas.Left="550" Canvas.Top="71" Width="49" Click="Button_Click_2"/>
    </Canvas>
</Window>

I
Code:
mports System.Data

Class MainWindow

    Dim dtGrid As DataTable

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Dim dsPerson As New DataSet
        Dim dsCountry As New DataSet
        Dim dt As DataTable

        dt = MakePerson()
        dsPerson.Tables.Add(dt)

        dtGrid = dsPerson.Tables(0)

        PersonFill(dsPerson)

        dt = MakeCountry()
        dsCountry.Tables.Add(dt)
        CountryFill(dsCountry)

        DG.ItemsSource = dtGrid.DefaultView
        DataColumnCountry.ItemsSource = dsCountry.Tables(0).DefaultView
    End Sub

    Public Sub CountryFill(_dsCountry As DataSet)
        Dim dt As New DataTable
        Dim dr As DataRow

        dr = _dsCountry.Tables(0).NewRow()
        dr("CountryId") = 100
        dr("Country") = "Nederland"
        _dsCountry.Tables(0).Rows.Add(dr)

        dr = _dsCountry.Tables(0).NewRow()
        dr("CountryId") = 200
        dr("Country") = "België"
        _dsCountry.Tables(0).Rows.Add(dr)

        dr = _dsCountry.Tables(0).NewRow()
        dr("CountryId") = 300
        dr("Country") = "Duitsland"
        _dsCountry.Tables(0).Rows.Add(dr)

    End Sub

    Public Sub PersonFill(_dsPerson As DataSet)
        Dim dt As New DataTable
        Dim dr As DataRow

        dr = _dsPerson.Tables(0).NewRow()
        dr("Status") = 0
        dr("ID") = 1
        dr("Name") = "Name1"
        dr("CountryId") = 100
        _dsPerson.Tables(0).Rows.Add(dr)

        dr = _dsPerson.Tables(0).NewRow()
        dr("Status") = 0
        dr("ID") = 2
        dr("Name") = "Name2"
        dr("CountryId") = 200
        _dsPerson.Tables(0).Rows.Add(dr)

        dr = _dsPerson.Tables(0).NewRow()
        dr("Status") = 0
        dr("ID") = 3
        dr("Name") = "Name3"
        dr("CountryId") = 100
        _dsPerson.Tables(0).Rows.Add(dr)

        dr = _dsPerson.Tables(0).NewRow()
        dr("Status") = 0
        dr("ID") = 4
        dr("Name") = "Name4"
        dr("CountryId") = 300
        _dsPerson.Tables(0).Rows.Add(dr)

    End Sub

    Public Function MakePerson()
        Dim dt As New DataTable
        Dim dcolID As DataColumn
        Dim dcolName As DataColumn
        Dim dcolCountry As DataColumn
        Dim dcolStatus As DataColumn

        dt = New DataTable()
        dcolStatus = New DataColumn("Status", Type.GetType("System.Int32"))
        dcolID = New DataColumn("ID", Type.GetType("System.Int32"))
        dcolName = New DataColumn("Name", Type.GetType("System.String"))
        dcolCountry = New DataColumn("CountryId", Type.GetType("System.Int32"))

        dt.Columns.Add(dcolStatus)
        dt.Columns.Add(dcolID)
        dt.Columns.Add(dcolName)
        dt.Columns.Add(dcolCountry)

        MakePerson = dt
    End Function

    Public Function MakeCountry()
        Dim dt As New DataTable
        Dim dcolStatus As DataColumn
        Dim dcolCountryId As DataColumn
        Dim dcolCountry As DataColumn

        dt = New DataTable()
        dcolStatus = New DataColumn("Status", Type.GetType("System.Int32"))
        dcolCountryId = New DataColumn("CountryId", Type.GetType("System.Int32"))
        dcolCountry = New DataColumn("Country", Type.GetType("System.String"))

        dt.Columns.Add(dcolStatus)
        dt.Columns.Add(dcolCountryId)
        dt.Columns.Add(dcolCountry)

        MakeCountry = dt
    End Function

    Public Sub DG_CellEditEnding(sender As Object, e As DataGridCellEditEndingEventArgs)
        Dim dgRow As DataGridRow = e.Row

        'dgRow.Item(1) = "Jippie Cell"
        'Dim dv = TryCast(DG.SelectedItem, DataRowView)

    End Sub

    Public Sub DG_RowEditEnding(sender As Object, e As DataGridRowEditEndingEventArgs)
        Dim dgRow As DataGridRow = e.Row

        dgRow.Item(3) = "Jippie Row"
    End Sub

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        MsgBox("Push the button")
    End Sub

    Private Sub ShowMe()
        Dim drv As DataRowView = Nothing

        Dim ds As New DataSet
        Dim dt As New DataTable

        dt = dtGrid

        For Each dr In dtGrid.Rows
            Debug.Print(dr.item("Id"))
            Debug.Print(dr.item("Name"))
        Next

    End Sub

    Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        ShowMe()
    End Sub

    Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
        Dim dr As DataRow
        Dim _index As Integer
        Dim item As Object

        dr = dtGrid.NewRow()
        dr("Status") = 4
        dr("ID") = "0"
        dr("Name") = ""
        dr("CountryId") = "0"
        dtGrid.Rows.Add(dr)

        _index = dtGrid.Rows.IndexOf(dr)

        item = DG.Items(_index)

        DG.SelectedItem = item
        DG.ScrollIntoView(item, DG.Columns(1))

        DG.BeginEdit()
        DG.Focus()

    End Sub

    Public Sub Shosho(sender As Object, e As System.Windows.Controls.AddingNewItemEventArgs)
        MsgBox("****")
    End Sub

    Private Sub DG_DataContextChanged(sender As Object, e As DependencyPropertyChangedEventArgs) Handles DG.DataContextChanged
        Debug.Print("Allo")
    End Sub

    Private Sub DG_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles DG.SelectionChanged
        'Dim dv = TryCast(sender.SelectedItems, DataRowView)

        'Debug.Print(dv.Item("Status"))
    End Sub

End Class

Dit is een voorbeeld zoals ik het heb. Als je dit bij jou ook zo kunt dan heb ik wat vragen.
 
Laatst bewerkt door een moderator:
Het voorbeeldje werkt. (op PersonValidationRule na die er niet bij zit)

Wat zijn je problemen nu precies?
 
De problemen die beginnen als ik een afbeelding van een vlag wil hebben bij een Land/country. Ook bij validation van de gegevens heb ik wat problemen.
 
Om te beginnen zou ik eerst eens classes maken voor person/country.

Je kun vlaggen een naam geven zoals het id zoals in de database of de afkorting zoals NLD.jpg or GER.jpg

Een afbeelding toevoegen is geen probleem, dit kun je gewoon in een datatemplate in je xaml doen.
Ik zal morgen eens een voorbeeldje maken. (al weet ik niet of het in een datagrid ook zo makkelijk is, dit control gebruik ik nooit)
 
Had gisteren geen tijd en nu even geen tijd voor de uitleg maar hier is alvast een snel voorbeeldje xaml/code vertaald vanuit C#. Kun je misschien al een beetje mee testen, later volgt wat uitleg :P


MainWindow XAML
Code:
<Window x:Class="PersonCountryExample.MainWindow" x:Name="Window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Main Window" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <ListView ItemsSource="{Binding People}" Margin="12,12,12,0">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="ID" Width="25" DisplayMemberBinding="{Binding ID}" />
                    <GridViewColumn Header="Name" Width="200">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Country" Width="200">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding Countries, ElementName=Window}" SelectedItem="{Binding Country, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                    <ComboBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <Image Source="{Binding Flag, Mode=TwoWay}" Margin="6,0"/>
                                                <TextBlock Text="{Binding Name, Mode=TwoWay}" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </ComboBox.ItemTemplate>
                                </ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="1">
            <StackPanel.Resources>
                <Style TargetType="Button">
                    <Setter Property="Margin" Value="0,12,12,12" />
                    <Setter Property="MinWidth" Value="75" />
                    <Setter Property="MinHeight" Value="25" />
                </Style>
            </StackPanel.Resources>

            <Button Content="Add" Click="Button_Click" />
        </StackPanel>
        
        
    </Grid>
</Window>

Country Class
Code:
Public Class Country
	Implements INotifyPropertyChanged
	Private _id As Integer
	Private _name As String
	Private _flag As String

	Public Sub New(id__1 As Integer, name__2 As String, flag__3 As String)
		ID = id__1
		Name = name__2
		Flag = flag__3
	End Sub

	Public Event PropertyChanged As PropertyChangedEventHandler

	Public Property ID() As Integer
		Get
			Return _id
		End Get
		Set
			_id = value
			OnPropertyChanged("ID")
		End Set
	End Property
	Public Property Name() As String
		Get
			Return _name
		End Get
		Set
			_name = value
			OnPropertyChanged("Name")
		End Set
	End Property
	Public Property Flag() As String
		Get
			Return _flag
		End Get
		Set
			_flag = value
			OnPropertyChanged("Flag")
		End Set
	End Property

	Protected Overridable Sub OnPropertyChanged(propertyName As String)
		Dim handler = PropertyChanged
		RaiseEvent handler(Me, New PropertyChangedEventArgs(propertyName))
	End Sub

	Public Overrides Function ToString() As String
		Return Name
	End Function
End Class

Person Class
Code:
Public Class Person
	Implements INotifyPropertyChanged
	Private _id As Integer
	Private _name As String
	Private _country As Country

	Public Sub New(id__1 As Integer, name__2 As String, country__3 As Country)
		ID = id__1
		Name = name__2
		Country = country__3
	End Sub

	Public Event PropertyChanged As PropertyChangedEventHandler

	Public Property ID() As Integer
		Get
			Return _id
		End Get
		Set
			_id = value
			OnPropertyChanged("ID")
		End Set
	End Property
	Public Property Name() As String
		Get
			Return _name
		End Get
		Set
			_name = value
			OnPropertyChanged("Name")
		End Set
	End Property
	Public Property Country() As Country
		Get
			Return _country
		End Get
		Set
			_country = value
			OnPropertyChanged("Country")
		End Set
	End Property

	Protected Overridable Sub OnPropertyChanged(propertyName As String)
		Dim handler = PropertyChanged
		RaiseEvent handler(Me, New PropertyChangedEventArgs(propertyName))
	End Sub

	Public Overrides Function ToString() As String
		Return Name
	End Function
End Class

MainWindow CodeBehind
Code:
Public Partial Class MainWindow
	Inherits Window
	Public Sub New()
		InitializeComponent()
		InitializeCollections()

		Me.DataContext = Me
	End Sub

	Public Property People() As ObservableCollection(Of Person)
		Get
			Return m_People
		End Get
		Set
			m_People = Value
		End Set
	End Property
	Private m_People As ObservableCollection(Of Person)
	Public Property Countries() As ObservableCollection(Of Country)
		Get
			Return m_Countries
		End Get
		Set
			m_Countries = Value
		End Set
	End Property
	Private m_Countries As ObservableCollection(Of Country)

	Private Sub InitializeCollections()
		Countries = New ObservableCollection(Of Country)() From { _
			New Country(1, "Nederland", "images/nl.png"), _
			New Country(2, "Duitsland", "images/de.png"), _
			New Country(3, "Noorwegen", "images/no.png") _
		}

		People = New ObservableCollection(Of Person)() From { _
			New Person(1, "Person A", Nothing), _
			New Person(2, "Person B", Nothing), _
			New Person(3, "Person C", Nothing) _
		}
	End Sub

	Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
		Dim newId = People.Last().ID + 1

		People.Add(New Person(newId, "new person " + newId, Nothing))
	End Sub
End Class

Ik had afbeeldingen van vlaggen in een project folder images zoals je bij InitializeCollections() ziet.


'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================
 
Status
Niet open voor verdere reacties.

Nieuwste berichten

Terug
Bovenaan Onderaan