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}"
这样的值有效,但我必须绑定到属性。