Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing adding,updating and deleting operations for <ListView> in WPF using by MVVM.
My question is that How can I give two binding for the same <TextBox>(one of these is for adding element to <ListView>and the other is for updating(selectedItem of <ListView>))

What I have tried:

When I write binding for adding it works successfully.And also When I write binding for updating the selected row is written to <TextBox>.But I need these work together.I tried <MultiBinding> but result weren't be successfull.Hope your helps..

For adding:
<TextBox Grid.Column="1" x:Name="txtSurname" Text="{Binding Path=Student.Surname, Mode=TwoWay}">
    <TextBox.Effect>
        <DropShadowEffect  ShadowDepth="3" Color="#75B07A"/>
    </TextBox.Effect>
</TextBox>-->

For updating:
<TextBox Grid.Column="1" x:Name="txtSurname" Text="{Binding ElementName=lstStuds, Path=SelectedItem.Surname}">
    <TextBox.Effect>
        <DropShadowEffect  ShadowDepth="3" Color="#75B07A"/>
    </TextBox.Effect>
</TextBox>

This is my <listview> :
<ListView x:Name="lstStuds" ItemsSource="{Binding Path=Students}" SelectedItem="{Binding SelectedStudent}"  Grid.Column="1" Grid.Row="1" Grid.RowSpan="3" Margin="0,10,20,20" FontSize="16">
           <ListView.Effect>
               <DropShadowEffect  ShadowDepth="3" Color="#75B07A"/>
           </ListView.Effect>
           <ListView.View>
               <GridView>
                   <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="150"/>
                   <GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Surname}" Width="150"/>
               </GridView>
           </ListView.View>


And my buttons:
 <Button Content="Add" Command="{Binding AddCommand}" Width="120" Height="auto" FontSize="18" Margin="0 0 0 10">
                    <Button.Effect>
                        <DropShadowEffect ShadowDepth="3" Color="#75B07A"/>
                    </Button.Effect>
                </Button>

<Button Content="Update" Command="{Binding EditCommand}" CommandParameter="{Binding ElementName=lstStuds, Path=SelectedItem}" Width="120" Height="auto" FontSize="18" Margin="0 0 0 10">
                  <Button.Effect>
                        <DropShadowEffect ShadowDepth="3" Color="#75B07A"/>
                    </Button.Effect>
                </Button>
Posted
Updated 25-Apr-19 18:28pm
v4

1 solution

You bind to an "entity" (model), not the "selected item" (a view part); which is null when the data source is empty.

When adding, the entity is a new object (to be added to the data source); otherwise, it's an existing item from the data source.

Your "MVVM" is bleeding; so don't call it that ...
 
Share this answer
 
Comments
Member 14141512 26-Apr-19 0:32am    
I'm new in "MVVM" pattern, and this is my first "MVVM" project.I try to learn and I need to help, that's why I'll glad if you help me.How can I use two binding for the same element? Or is there any way for this?
Member 14721302 27-Jan-20 4:55am    
I'm facing same problem did you find any solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900