将SelectedItem绑定到Textbox的问题

时间:2018-02-13 13:19:38

标签: c# wpf xaml

View.xaml

<DataGrid ItemsSource="{Binding RentItems}" SelectedItem="{Binding ID.EquipmentID,  Mode=TwoWay}"  x:Name="datagrid1">
<DataGrid.Columns>
    <DataGridTextColumn Header="ID" Binding="{Binding EquipmentID, Mode=OneWay}" IsReadOnly="True"/>

</DataGrid.Columns>

视图模型

    public class ViewModelReturnData : ViewModelBase
{
    public ICommand MyCommand { get; set; }

    private ObservableCollection<Equipment> rentitems = new ObservableCollection<Equipment>();

    public ObservableCollection<Equipment> RentItems
    {
        get { return rentitems; }
        set { rentitems = value; }
    }

    private Equipment id;
    public Equipment ID
    {
        get { return id; }
        set { id = value;  ; OnPropertyChanged("ID"); }
    }

    public ViewModelReturnData()
    {
        MyCommand = new RelayCommand(ExecuteMyMethod, CanExecuteMyMethod);
    }


    private bool CanExecuteMyMethod(object parameter)
    {
        return true;
    }

    private void ExecuteMyMethod(object parameter)
    {
        using (rentalEntities context = new rentalEntities())
        {
            var query = ...
            foreach (var item in query)
            {
                RentItems.Add(item);
            }

        }
    }
}

在ViewModelBase中,我得到了INotifyPropertyChanged。 我想将EquipmentID的值绑定到我的属性ID,但我不知道为什么它不起作用。好像我在绑定中错过了一些东西。绑定像Text="{Binding ElementName=datagrid1, Path=SelectedItem.EquipmentID}"这样的值有效,但我必须绑定到属性。

0 个答案:

没有答案