Quantcast
Channel: VBForums - WPF, WCF, WF
Viewing all articles
Browse latest Browse all 256

Bind Textbox to Combobox in WPF (MVVM)

$
0
0
Hey,

I am working with WPF and the MVVM pattern for a few days now and it's kinda difficult compared to window forms. I tried to bind the selectedvalue from a combobox to a textbox. Sounds simple but I think I am lost :ehh:
This is my code:

MainWindow.xaml
Code:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="150" Width="150">
    <Window.Resources>
        <XmlDataProvider x:Key="PersonalData" Source="Model\Mitarbeiter.xml" XPath="Mitarbeiter" />
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <ComboBox Grid.Row="0" x:Name="cmbMitarbeiter" HorizontalAlignment="Left" Margin="10,10,0,0"
                  VerticalAlignment="Top" Width="120" DisplayMemberPath="@PersonalID"
                  ItemsSource="{Binding Source={StaticResource PersonalData}, XPath=./Person}"
                  SelectedValuePath="@Vorname"
                  SelectedValue="{Binding SelectedVornameValue}"
                  />
        <Grid Grid.Row="1" DataContext="{Binding SelectedItem, ElementName=cmbMitarbeiter}">
            <TextBox Text="{Binding Vorname}"/>
        </Grid>
    </Grid>
</Window>

MainViewModel.vb
Code:

Imports System.ComponentModel
Imports System.Xml
Public Class MainViewModel : Implements INotifyPropertyChanged

    Public Sub New()

    End Sub

    Public Sub New(Vorname As String)
        Me.SelectedVornameValue = _SelectedVornameValue
    End Sub

    Private _SelectedVornameValue As String
    Public Property SelectedVornameValue As String
        Get
            Return _SelectedVornameValue
        End Get
        Set(ByVal value As String)
            _SelectedVornameValue = value
            RaiseProp("SelectedVornameValue")
        End Set
    End Property

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged


    Public Sub RaiseProp(ByVal Propertie As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Propertie))
    End Sub


End Class

Mitarbeiter.xml
Code:

<?xml version="1.0" encoding="utf-8" ?>
<Mitarbeiter>
  <Person PersonalID="1" Vorname="Fritz" Nachname="Meier" />
  <Person PersonalID="2" Vorname="Max" Nachname="Mustermann" />
  <Person PersonalID="3" Vorname="Heinz" Nachname="Müller" />
</Mitarbeiter>

Where did I made a fault?

Viewing all articles
Browse latest Browse all 256

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>