Boot750
Boot750

Reputation: 891

Binding Datagrid Item to Textboxes

I try to bind a selected item in my datagrid to some textboxes. Sadly the textboxes wont be updated on change.

If you need more Informations fell free to ask.

In my view i try to bind the the data of the selected Item of the Datagrid to the Textboxes. in the Textboxes there can be a new employee you want to add or one of the Datagrid that you want to edit.

    <TextBox Name="TxtName" Text="{Binding Employee.LastName}" Grid.Column="1" Grid.Row="0"></TextBox>
    <TextBox Name="TxtFirstName" Text="{Binding Employee.FirstName}"  Grid.Column="3" Grid.Row="0"></TextBox>
    <TextBox Name="TxtDateOfBirth" Text="{Binding Employee.DateOfBirth, StringFormat=d}" Grid.Column="1" Grid.Row="1"></TextBox>
    <ComboBox Name="CmbGender" SelectedItem="{Binding Employee.Gender}" ItemsSource="{Binding Genders}" DisplayMemberPath="Short" Grid.Column="3" Grid.Row="1"/>


    <DataGrid Name="GrdAllEmployees" ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedEmployee}" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="4" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding SelectionChanged}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>

In my ViewModel i set the Value of the Selected Employee to the Employee that is shown in the Textboxes and raise the Events for both.

        public Employee SelectedEmployee
        {
            get { return _selectedEmployee; }
            set
            {
                _selectedEmployee = value;
                NotifyPropertyChanged("SelectedEmployee");
                _employee = _selectedEmployee;
                NotifyPropertyChanged("Employee");
            }
        }

After this the values are correct on debugging. but the view will not be updated.

Upvotes: 0

Views: 1475

Answers (2)

Boot750
Boot750

Reputation: 891

It worked for me after i replaced my own classes for the RelayCommand and ViewModelBase with the ones of MVVMlight, that i downloaded from nuget.

https://mvvmlight.codeplex.com/

I just had to change the NotifyPropertyChanged with RaisePropertyChanged.

But also thanks to @Sheridan for his advice.

Upvotes: 0

Sheridan
Sheridan

Reputation: 69959

There is a much simpler way of displaying values from the selected item of the DataGrid, or in fact any collection control in WPF. That is to use the Selector.IsSynchronizedWithCurrentItem property. When you set this to true, then you can reference the selected item from that collection using the / notation, which means the current item. Try something like this:

<StackPanel>
    <DataGrid ItemsSource="{Binding Employees}" IsSynchronizedWithCurrentItem="True" />
    <TextBlock Text="{Binding Employees/Name}" />
</StackPanel>

This would display the Name property value from the currently selected item in the DataGrid. Further information... from the Binding.Path Property page on MSDN:

When the source is a collection view, the current item can be specified with a slash (/). For example, the clause Path=/ sets the binding to the current item in the view. When the source is a collection, this syntax specifies the current item of the default collection view.

Property names and slashes can be combined to traverse properties that are collections. For example, Path=/Offices/ManagerName specifies the current item of the source collection, which contains an Offices property that is also a collection. Its current item is an object that contains a ManagerName property.

Upvotes: 1

Related Questions