简体   繁体   English

自动插入后如何在 C# WPF 中选择新的 ListBoxItem

[英]How do I select a new ListBoxItem in C# WPF after I just inserted it automatically

I have the following problem with my calculator app which I'm doing in the MVVM pattern.我在 MVVM 模式中使用的计算器应用程序存在以下问题。

I'm redoing the Windows 10 Calculator in Standard Mode.我正在标准模式下重做 Windows 10 计算器。 I made an ObservableCollection of MemoryItem.我做了一个 MemoryItem 的 ObservableCollection。 MemoryItem is a class that contains an int for the Index, a double for the value and a RelayCommand for the MemoryButtons. MemoryItem 是一个类,其中包含用于索引的 int、用于值的双精度值和用于 MemoryButton 的 RelayCommand。 Basically it looks like this and is connected to my ViewModel:基本上它看起来像这样并连接到我的 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.所以我已经将 SelectedMemoryItemIndex 属性绑定到 WPF 中的 SelectedItemIndex。 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.绑定工作我不知道如何在插入新的 MemoryItem 并删除新的 MemoryItem 后选择新的 MemoryItem。 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). ObservableCollection 不包括更新特定项目的方法(据我所知)。

This is the method I'm using to add the value to the MemoryItemValue and insert it in my Collection:这是我用来将值添加到 MemoryItemValue 并将其插入我的集合中的方法:

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.这种方式有效,但我总是必须选择新插入的 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.请记住,我是编程的初学者,这是我有史以来的第一个 SO 问题。

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.在 MemoryItem 类上创建一个 IsSelected 属性并将 ListBoxItem.IsSelected 绑定到该属性。

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

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.请原谅我完全按照我发现的方式留下那个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ORMLite和c#仅选择几列? - How do I select just few columns with ORMLite and c#? WPF - 如何获取绑定到ListBoxItem的对象 - WPF - How do I get an object that is bound to a ListBoxItem back 如何从选定的ListBox控件的listboxitem获取属性? C# - How do i get a property from a selected ListBox Control 's listboxitem ? C# 将值插入 C# 中的 SQL 服务器表后,如何将值加 1? - How do I add 1 to a value after it's inserted into SQL Server table in C#? 为什么我要在我的C#组合框中插入3个值而不是1个? - Why do I get 3 values inserted into my C# combo box instead of just 1? 如何从C#中的Winforms Listview控件中的select列中获取内容? - How do i get just the contents out of a select column in a Winforms Listview control in C#? 如何通过 C# 获取我刚刚插入到文档 (MS Word) 中的图片的名称和路径? - How to get the name and the path of the picture I've just inserted to document (MS Word) by C#? 用户升级到新版本时,如何自动关闭旧版本的C#程序? - How do I automatically close the old version of a C# program when a user upgrades to the new? 如何为ListBoxItem创建标头? - How do I make a header for a ListBoxItem? 在WPF C#中完成处理后如何更改按钮中的图像? - How do I change image in button after process completion in WPF C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM