我正在尝试根据是否在项目绑定到的类中使用对象来更改绑定列表框中项目的选定状态,我似乎找不到这样做的方法,它必须保留动态的,因为对象可能会改变并因此在类的不同实例中使用:
<Popup x:Name="ContextMenuPopup" Height="250" Width="300" Margin="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Canvas Name="popupholder" Background="DarkSeaGreen" Height="250" Width="300" HorizontalAlignment="Center">
<StackPanel Orientation="Vertical">
<TextBlock Name="popupTitle" Text="Select Investments" Margin="20,0,0,0" FontFamily="Courier New" FontSize="22" HorizontalAlignment="Center" Foreground="Black" />
<ListBox x:Name="investPicker" SelectionChanged="ListBox_SelectionChanged" LayoutUpdated="investPicker_LayoutUpdated" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="10,20,0,0" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Width="100"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="executeSelection" Content="Ok" Click="executeSelection_Click" Margin="40,5,0,0" VerticalAlignment="Bottom"/>
</StackPanel>
</Canvas>
</Popup>
后面的代码是:
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
this.investPicker.ItemsSource = storedInvestments;
foreach (Investment investment in investPicker.Items)
{
foreach (CardDataSet card in investment.attachedCards)
if (card.ID == activeCard.ID)
VisualStateManager.GoToState((ListBoxItem) investPicker.Items[investment.ID -1], "Selected", true);
}
ContextMenuPopup.IsOpen = true;
}
现在显然代码不起作用,因为它是从我的类到 ListBoxItem 的无效转换,有人知道我该怎么做吗?