问题描述:

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");

}

}

网友答案:

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>
相关阅读:
Top