[英]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.