Xaml-绑定listviewitem源对象

本文关键字:对象 listviewitem 绑定 Xaml- | 更新日期: 2023-09-27 17:59:42

我在VM中使用了一个observableCollecion。此集合绑定到ListView中的我的视图中。在我的项目中,我尝试获取创建此项目的元素。把它添加到我的commandParameter中,然后做这件事。

这是我的虚拟机:

public RelayCommand<SelectionCommandParameter> CmdRemoveFromQuiz { get; set; } 
public ObservableCollection<Question> SelectedQuiz
{
   get { return _selectedQuiz; }
   set
   {
      _selectedQuiz = value;
      RaisePropertyChanged("SelectedQuiz");
   }
}
private void RemoveFromQuiz(SelectionCommandParameter selection)
{
    if (selection.Parameter is Question)
    {
        ObservableCollection<Question> tempQuiz = SelectedQuiz; 
        Question _question = (Question)selection.Parameter;
        tempQuiz.Remove(_question);
        SelectedQuiz = tempQuiz;
    }
}

问题从RemoveBtn开始我启动命令,而selection保持null我想在我的ListViewitem 中使用ObservableCollection<Question>对象

现在我的视图:

        <userControls:CharmFlyout
        x:Name="cfoQuizList"
        x:Uid="QuizListCreatingPageFlyout"
        Heading="Question Multiple"
        HorizontalAlignment="Left"
        Grid.Column="0"
        Grid.RowSpan="2"
        Style="{StaticResource stlAddRecipientFlyout}">
            <tut:TutorialAwareListView x:Name="gvQuizItem"
                          ItemsSource="{Binding SelectedQuiz}"
                          IsItemClickEnabled="True"
                          CanReorderItems="True"
                          SelectionMode="None"
                          ManipulationMode="TranslateRailsX">
                <ListView.ItemTemplate>
                    <DataTemplate x:Name="DTQuizItem">
                        <Grid HorizontalAlignment="Left" Width="{StaticResource RectangleTileWidth}" Height="{StaticResource RectangleTileHeight}" 
                              Margin="0 0 0 0" Background="{StaticResource OrangeBackgroundThemeBrush}">
                            <Grid Grid.Column="1">
                                <Button x:Name="RemoveBtn" Content="X" HorizontalAlignment="Right" VerticalAlignment="Top" Width="40" Height="40" 
                                        BorderThickness="0" Command="{Binding DataContext.CmdRemoveFromQuiz, ElementName=gvQuizItem}" CommandParameter="{Binding Question}"/>
                                <maxCtrls:MaxAutoScrollingContentPresenter VerticalAlignment="Center"
                                        ScrollingDuration="{Binding Name, Converter={StaticResource TextToTimeToReadShortFormatConverter}}"
                                        ScrollingBeginTime="0:0:2">
                                    <TextBlock Text="{Binding Name}" FontWeight="SemiBold" 
                                               Foreground="{StaticResource WhiteBackground}"
                                               Margin="20,5,10,5" VerticalAlignment="Center" TextWrapping="Wrap"/>
                                </maxCtrls:MaxAutoScrollingContentPresenter>
                            </Grid>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </tut:TutorialAwareListView>
        </userControls:CharmFlyout>

教程AwareListView的工作方式与ListView完全相同。我使用它只是在教程运行时指向元素。

这是一个Windows应用商店应用程序!!我不能做我们想做的所有事情。

Xaml-绑定listviewitem源对象

您应该将SelectionMode="None"更改为Single或Multiply。并为所选项目制作新的ObservableCollection。您只绑定ItemsSource="{Binding SelectedQuiz}"。绑定SelectedItems="{…}"