0

I have the following problem with my calculator app which I'm doing in the MVVM pattern.

I'm redoing the Windows 10 Calculator in Standard Mode. I made an ObservableCollection of MemoryItem. MemoryItem is a class that contains an int for the Index, a double for the value and a RelayCommand for the MemoryButtons. Basically it looks like this and is connected to my ViewModel:

public class MemoryItem
    {
        public double MemoryItemValue { get; set; }

        public int SelectedMemoryItemIndex { get; set; }

        public RelayCommand MemoryItemChange { get; set; }

    } 

So I've binded the SelectedMemoryItemIndex Property to the SelectedItemIndex in WPF. My ListBox looks like this:

<ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Style="{StaticResource MemoryListBoxStyle}"
                     Visibility="{Binding MemoryVisibility}" ItemsSource="{Binding MemoryCollection}"
                     SelectedItem="{Binding SelectedMemoryItem}" SelectionMode="Extended" SelectedIndex="{Binding SelectedMemoryItemIndex}"
                     HorizontalContentAlignment="Right"/>

While the style of it looks like this:

<Style x:Key="MemoryListBoxStyle" TargetType="ListBox">
           <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <UniformGrid Rows="2" Margin="5">
                            <TextBlock Style="{StaticResource DisplayStyle}" Text="{Binding MemoryItemValue}" FontSize="20"/>
                            <DockPanel LastChildFill="False">
                                <Button Content="MC" Style="{StaticResource MemoryItemButton}"
                                        Command="{Binding MemoryItemChange}" CommandParameter="{x:Static md:MemoryUsage.Clear}"/>

                                <Button Content="M+" Style="{StaticResource MemoryItemButton}"
                                        Command="{Binding MemoryItemChange}" CommandParameter="{x:Static md:MemoryUsage.Add}"/>

                                <Button Content="M-" Style="{StaticResource MemoryItemButton}"
                                        Command="{Binding MemoryItemChange}" CommandParameter="{x:Static md:MemoryUsage.Substract}"/>
                            </DockPanel>
                        </UniformGrid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

The bindings work BUT I don't know how to have the new MemoryItem selected after Inserting the new MemoryItem and deleting the new one. Is there a better of way inserting the new item? ObservableCollection doesn't include a method to update a specific item (as far as I know).

This is the method I'm using to add the value to the MemoryItemValue and insert it in my Collection:

case MemoryUsage.Add:
                    if (SelectedMemoryItemIndex == -1)
                    {
                        SelectedMemoryItemIndex = 0;
                    }
                    MemoryItemValue += Eingabe1;
                    MemoryCollection.Insert(SelectedMemoryItemIndex +1, MItem);
                    MemoryCollection.RemoveAt(SelectedMemoryItemIndex);
                    break;

This way it worked but I always have to select the new inserted MemoryItem. I'm thankful for ANY help provided by you.

Please keep in mind that I'm a beginner in programming and this is my first SO question ever.

Papriker
  • 77
  • 6

1 Answers1

0

Here is a post that helps answer this question. But basically:

Create an IsSelected property on your MemoryItem class and bind ListBoxItem.IsSelected to that property.

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
</ListBox.ItemContainerStyle>

When you want your new item selected, just set IsSelected to true.

IsSelected = true;

And shazam! It should work.

Here is code copied from another answer that may give you more information.

<ListBox ItemsSource="{Binding Items, Source={StaticResource ViewModel}}"
         SelectionMode="Extended">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding IsItemSelected}"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ItemText}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Forgive me for leaving that example exactly as I found it.

Scott H
  • 16
  • 4