Xaml - 绑定listviewitem源对象

时间:2015-06-25 08:07:14

标签: c# xaml listview binding windows-store-apps

我在我的VM中使用了observableCollecion。此Collection在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>

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

它是 Windows应用商店应用 !!我不能做我们想做的所有事情。

1 个答案:

答案 0 :(得分:0)

您应该更改您的SelectionMode =&#34;无&#34;单或乘。 并为所选项目创建新的ObservableCollection。你只绑定ItemsSource =&#34; {Binding SelectedQuiz}&#34;。绑定SelectedItems =&#34; {...}&#34;