2

I have scenario .In my view I have 2 textboxes say Name,Address which bind viewModels proeperty Name,Address respectively.And One button which add the textbox value to a listview. My problem is I want to populate the textboxes when an item in listview is selected how to do this (I did this in selection_changed event in normal wpf applicaton ,but here I am using MVVM pattern its new to me )

Edit

My View :

<TextBox Height="23"
                 HorizontalAlignment="Stretch"
                 Margin="0,6,0,0"
                 Name="txtbxAddress"
                 Text="{Binding Path=Name}"
                 VerticalAlignment="Top"
                 Grid.Column="1"
                 Grid.Row="1" />
<TextBox Height="23"
                 HorizontalAlignment="Stretch"
                 Margin="0,6,0,0"
                 Name="txtbxAddress"
                 Text="{Binding Path=Address}"
                 VerticalAlignment="Top"
                 Grid.Column="1"
                 Grid.Row="1" />

 <ListView ItemsSource="{Binding Path=ManufactureRecords}"
                  SelectedItem="{Binding Path=SelectedManufacture}"
                  Margin="7"
                  Name="lstvw_Manufature_Master"
                 >
            <ListView.View>
                <GridView>

                    <GridViewColumn DisplayMemberBinding="{Binding Path=Manufacture_Name}"
                                    Header="Manufacture Name"
                                    Width="338" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Address}"
                                    Header="Address"
                                    Width="0" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Location}"
                                    Header="Location"
                                    Width="200" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Phone_No}"
                                    Header="Phone_No"
                                    Width="120" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Is_Active}"
                                    Header="Is_Active"
                                    Width="0" />
                </GridView>
            </ListView.View>
        </ListView>

Here is my ViewModel

 private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }

    private string _address;
    public string Address
    {
        get { return _address; }
        set
        {
            _address = value;
            RaisePropertyChanged("Address");
        }
    }
 private ObservableCollection<Tbl_Manufacture_Master> _manufactureRecords;
    public ObservableCollection<Tbl_Manufacture_Master> ManufactureRecords
    {
        get { return _manufactureRecords; }
        set
        {
            _manufactureRecords = value;
            RaisePropertyChanged("ManufactureRecords");
        }
    }

    private Tbl_Manufacture_Master _selectedManufacture;
    public Tbl_Manufacture_Master SelectedManufacture
    {
        get { return _selectedManufacture; }
        set
        {
            _selectedManufacture = value;
            RaisePropertyChanged("SelectedManufacture");
        }
    }
Mussammil
  • 864
  • 4
  • 16
  • 38
  • possible duplicate of [How do I bind a Listview SelectedItem to a Textbox using the TwoWay mode?](http://stackoverflow.com/questions/4529242/how-do-i-bind-a-listview-selecteditem-to-a-textbox-using-the-twoway-mode) – Matt Feb 09 '14 at 14:53
  • @tencntraze , but I already bind my TextBox.Text Property to my ViewModels property for saving using Icommand,then How do I bind Text={Binding ElementName=listview1,Path=SelectedItem.ManufactureName} see my Edit – Mussammil Feb 09 '14 at 15:08

1 Answers1

1

Going from the example linked in the comments, you need to name your ListView (i.e. x:Name="listView") and specific that name in the binding, along with the SelectedItem property:

<TextBox Height="23"
         HorizontalAlignment="Stretch"
         Margin="0,6,0,0"
         Name="txtbxAddress"
         Text="{Binding Path=SelectedItem.Name, ElementName=listView}"
         VerticalAlignment="Top"
         Grid.Column="1"
         Grid.Row="1" />
<TextBox Height="23"
         HorizontalAlignment="Stretch"
         Margin="0,6,0,0"
         Name="txtbxAddress"
         Text="{Binding Path=SelectedItem.Address, ElementName=listView}"
         VerticalAlignment="Top"
         Grid.Column="1"
         Grid.Row="1" />

<ListView ItemsSource="{Binding Path=ManufactureRecords}"
          SelectedItem="{Binding Path=SelectedManufacture}"
          Margin="7"
          x:Name="listView"
          Name="lstvw_Manufature_Master">
    ...
</ListView>
Matt
  • 2,682
  • 1
  • 17
  • 24
  • ,so thats fine.but What happens when I want to save the TextBox values to a database table,For this I bind the TextBox fields to Properties on my view Model on the save command I pick the values as this.Name,this.Address and insert into database – Mussammil Feb 09 '14 at 15:34
  • I'm not sure what you're asking. Do you mean when do you commit to the database and how do you know what's changed? I'd say that's a separate question to post, but generally you could keep a flag for each object to say if something has changed, which is set in your property setter, then when a save command is executed you pull a list of objects with this flag. – Matt Feb 09 '14 at 15:51
  • this working fine .Textboxes get populated when the listview selected, because I Bind My TextBox's TextProperty like 'Text="{Binding Path=SelectedItem.Name, ElementName=listView}" .But how do I get the textbox value in my ViewModel's command property(saveCommand) .I think I dont get the textbox value in my viewModel because I dont bind it to any ViewModel property instead I used Element binding . – Mussammil Feb 11 '14 at 12:26
  • Because your ListView is bound to `SelectedManufacture`, so you have a reference to which object is currently being edited. @Mussammil – Matt Feb 11 '14 at 16:50