选择listview项时填充文本框

时间:2014-02-09 14:28:43

标签: c# wpf listview mvvm

我有场景。在我看来,我有两个文本框,分别是Name,Address,它们分别绑定viewModels proeperty名称,地址。还有一个按钮,它将文本框值添加到列表视图中。 我的问题是当我选择listview中的项目时,我想填充文本框(我在普通的wpf应用程序中的selection_changed事件中这样做了,但是这里我使用MVVM模式对我来说是新的)

修改

我的观点:

<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>

这是我的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");
        }
    }

1 个答案:

答案 0 :(得分:0)

从评论中链接的示例开始,您需要在绑定中命名ListView(即x:Name="listView")和特定的名称,以及SelectedItem属性:

<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>