在我的WPF應用程序,我有一列顯示的IntegerUpDown控制與其綁定的屬性在我的視圖模型一個DataGrid:IntegerUpDown綁定問題
<DataGridTemplateColumn Header="Qty">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<toolkit:IntegerUpDown Minimum="1" Maximum="999" Value="{Binding SelectedQuantity, Mode=TwoWay}" Increment="1" HorizontalAlignment="Left" Margin="0,0,0,0" Name="integerUpDown1" VerticalAlignment="Top" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
這似乎是隻有1個雙向綁定。最初在我的視圖模型中設置的值顯示在控件中,但是當我更改控件的值時,它不會更新視圖模型。我已經確保改變控制的價值,然後按下標籤或輸入失去焦點,並觸發更新,但仍然沒有運氣。我的視圖模型是相當簡單的,這裏的SelectedQuantity財產
public int SelectedQuantity
{
get
{
return _selectedQuantity;
}
set
{
if (value != _selectedQuantity)
{
_selectedQuantity = value;
OnPropertyChanged("SelectedQuantity");
}
}
}
private int _selectedQuantity;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
它可能有一些做Automapper?我使用Automapper將我的模型映射到viewmodel。我最初以爲我可能需要在這個屬性上使用Automappers「Ignore」,但在嘗試後仍然存在相同的問題。 SelectedQuantity不是我模型對象中的屬性。也許它與DataGridTemplateColumns有關?
編輯 看起來沒有什麼實際使用DataGrid正確綁定(雖然在查看其他控件正確綁定):(這是我如何填充我的ObservableCollection<ProductViewModel>
對象
public void Search()
{
_viewModel.SearchResults.Clear();
List<ProductViewModel> searchResults = Mapper.Map<List<Product>, List<ProductViewModel>>(_productService.SearchProducts(_viewModel.SearchValue));
foreach (ProductViewModel pvm in searchResults)
{
_viewModel.SearchResults.Add(pvm);
}
}
編輯2:
我的DataGrid綁定到ObservableCollection<ProductViewModel>
對象像這樣ItemsSource="{Binding SearchResults}"
我看到DataGrid中正確的項目,更改只是沒有反映在視圖模型中。 SelectedQuantity是ProductViewModel中的一個屬性。
有沒有被綁定到DataGrid的集合?如果是的話,SelectedQuantity是綁定集合中的對象的成員,還是在託管DataGrid的窗口/控件的視圖模型中? – Josh 2012-03-21 16:45:23
@Josh請參閱編輯2 – 2012-03-21 16:51:17