Chris Klepeis
Chris Klepeis

Reputation: 9983

IntegerUpDown Binding Issue

In my WPF app, I have a datagrid with a IntegerUpDown control displayed in a column and its bound to a property in my viewmodel:

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

It appears to be only 1 way binding. The value thats initially set in my viewmodel is shown in the control, but when I change the value of the control it doesn't update the viewmodel. I've made sure to change the value of the control then press tab or enter to lose focus and trigger the update but still no luck. My viewmodel is pretty simple, and here's the SelectedQuantity property

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

It may have something to do with Automapper? I use Automapper to map my model to my viewmodel. I thought initially that I maybe needed to use Automappers "Ignore" on this property, but after trying that the same problem persisted. The SelectedQuantity is not a property in my model object. Maybe it has something to do with DataGridTemplateColumns?

Edit It appears nothing is actually bound correctly with the datagrid (although other controls in the View are bound correctly) :( This is how I'm populating my ObservableCollection<ProductViewModel> object

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

Edit 2:

My DataGrid is bound to the ObservableCollection<ProductViewModel> object like so ItemsSource="{Binding SearchResults}" I am seeing the items properly in the DataGrid, changes just aren't being reflected in the viewmodel. SelectedQuantity is a property in ProductViewModel.

Upvotes: 3

Views: 4039

Answers (1)

Josh
Josh

Reputation: 10604

I think you are mixing bindings. The datagrid is probably bound to an ObservableCollection (if it isn't, I would start there). The item context at the row level is the individual object, which is the object that should have SelectedQuantity property in order for your XAML to work. If it is on your encompassing viewmodel, this won't work as multiple rows will be bound to the same property and I don't think it will give you the effect your looking for.

Try adding some code to see if the value is being sent once you exit the row:

<b:Interaction.Triggers>
    <b:EventTrigger EventName="RowEditEnding">
        <b:InvokeCommandAction  Command="{Binding RowEditEndingCommand}" CommandParameter="{Binding }"/>
    </b:EventTrigger>
 </b:Interaction.Triggers>

Upvotes: 3

Related Questions