2

我在 ItemRepeater 中放置操作按钮时遇到问题。我按照与 ListView 中相同的方式进行操作:

<muxc:ItemsRepeater Margin="0,24,0,0" ItemsSource="{x:Bind ViewModel.Team.Events, Mode=OneWay}">
        <muxc:ItemsRepeater.ItemTemplate>
              <DataTemplate x:DataType="models:Event">
              <controls:Expander
                                        HorizontalContentAlignment="Left"
                                        Header="{x:Bind Name, Mode=OneWay}"
                                        ExpandDirection="Down"
                                    >
                                        <StackPanel HorizontalAlignment="Left" Padding="24">
                                            <TextBlock Text="{x:Bind Name}" FontSize="36" />
                                            <TextBlock Text="{x:Bind Description}" />
                                            <StackPanel Spacing="4" Orientation="Horizontal">
                                                <TextBlock Text="{x:Bind Start}" />
                                                <TextBlock Text="-" />
                                                <TextBlock Text="{x:Bind End}" />
                                            </StackPanel>

                                            <TextBlock Text="{x:Bind Type}" />
                                            <Grid Width="1000" Margin="0,24,0,0">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="13*" />
                                                    <ColumnDefinition Width="*" />
                                                    <ColumnDefinition Width="13*" />
                                                </Grid.ColumnDefinitions>
                                                <StackPanel Grid.Column="0">
                                                    <TextBlock FontSize="24" Text="Participations" />
                                                    <ListView ItemsSource="{x:Bind Participations}">
                                                        <ListView.ItemContainerStyle>
                                                            <Style TargetType="ListViewItem">
                                                                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                                            </Style>
                                                        </ListView.ItemContainerStyle>
                                                        <ListView.ItemTemplate>
                                                            <DataTemplate x:DataType="models:Participation">
                                                                <Grid>
                                                                    <Grid.ColumnDefinitions>
                                                                        <ColumnDefinition Width="9*" />
                                                                        <ColumnDefinition Width="1*" />
                                                                    </Grid.ColumnDefinitions>
                                                                    <TextBlock Text="{x:Bind Username}" />
                                                                    <FontIcon Grid.Column="1" Glyph="{x:Bind Confirmed, Converter={StaticResource BooleanIconConverter}}"></FontIcon>

                                                                </Grid>
                                                            </DataTemplate>
                                                        </ListView.ItemTemplate>
                                                    </ListView>
                                                </StackPanel>

                                                <StackPanel Grid.Column="2">
                                                    <TextBlock FontSize="24" Text="Discrepancies" />
                                                    <Grid Margin="8">
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="1*" />
                                                            <ColumnDefinition Width="1*" />
                                                            <ColumnDefinition Width="2*" />
                                                            <ColumnDefinition Width="1*" />
                                                            <ColumnDefinition Width="1*" />
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock Text="Player" />
                                                        <TextBlock Text="Type" Grid.Column="1"/>
                                                        <TextBlock Text="Reason" Grid.Column="2"/>
                                                        <TextBlock Text="Delay" Grid.Column="3"/>
                                                    </Grid>
                                                    <ListView ItemsSource="{x:Bind Discrepancies}">
                                                        <ListView.ItemContainerStyle>
                                                            <Style TargetType="ListViewItem">
                                                                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                                            </Style>
                                                        </ListView.ItemContainerStyle>
                                                        <ListView.ItemTemplate>
                                                            <DataTemplate x:DataType="models:Discrepancy">
                                                                <Grid>
                                                                    <Grid.ColumnDefinitions>
                                                                        <ColumnDefinition Width="1*" />
                                                                        <ColumnDefinition Width="1*" />
                                                                        <ColumnDefinition Width="2*" />
                                                                        <ColumnDefinition Width="1*" />
                                                                        <ColumnDefinition Width="1*" />
                                                                    </Grid.ColumnDefinitions>
                                                                    <TextBlock Text="{x:Bind Username}" />
                                                                    <TextBlock Text="{x:Bind Type}" Grid.Column="1"/>
                                                                    <TextBlock Text="{x:Bind Reason}" Grid.Column="2"/>
                                                                    <TextBlock Text="{x:Bind DelayLength}" Grid.Column="3"/>
                                                                    <Button Grid.Column="4" HorizontalAlignment="Right">
                                                                        <Button.Flyout>
                                                                            <Flyout>
                                                                                <StackPanel>
                                                                                    <TextBlock Style="{ThemeResource BaseTextBlockStyle}" Text="Are you sure ?" Margin="0,0,0,12" />
                                                                                    <Button Click="DeleteDiscrepancy" Content="Yes" />
                                                                                </StackPanel>
                                                                            </Flyout>
                                                                        </Button.Flyout>

                                                                        <StackPanel Spacing="8" Orientation="Horizontal">
                                                                            <FontIcon Glyph="&#xE74D;" />
                                                                        </StackPanel>
                                                                    </Button>
                                                                </Grid>
                                                            </DataTemplate>
                                                        </ListView.ItemTemplate>
                                                    </ListView>
                                                </StackPanel>
                                            </Grid>
                                            <StackPanel Spacing="16" Orientation="Horizontal" FlowDirection="RightToLeft">
                                                <Button Click="HandleClickAddDiscrepancy">
                                                    <StackPanel Spacing="8" Orientation="Horizontal">
                                                        <TextBlock Text="Add Discrepancy"></TextBlock>
                                                        <FontIcon Glyph="&#xE710;"/>
                                                    </StackPanel>
                                                </Button>
                                                <Button>
                                                    <StackPanel Spacing="8" Orientation="Horizontal">
                                                        <TextBlock Text="Edit Event"></TextBlock>
                                                        <FontIcon Glyph="&#xE70F;"/>
                                                    </StackPanel>
                                                </Button>
                                                <Button>
                                                    <StackPanel Spacing="8" Orientation="Horizontal">
                                                        <TextBlock Text="Delete Event"></TextBlock>
                                                        <FontIcon Glyph="&#xE74D;"/>
                                                    </StackPanel>
                                                </Button>
                                            </StackPanel>
                                        </StackPanel>

                                    </controls:Expander>
              </DataTemplate>
         </muxc:ItemsRepeater.ItemTemplate>
</muxc:ItemsRepeater>

在我的代码隐藏中:

        private void HandleClickAddDiscrepancy(object sender, RoutedEventArgs e) {
            var evt = (sender as Button)?.DataContext as Event;
            if(ViewModel.AddDiscrepancyCommand.CanExecute(evt)) {
                ViewModel.AddDiscrepancyCommand.Execute(evt);
            }
        }

但是在这个代码隐藏方法中,我的按钮的 DataContext 为空。不应该是列表的当前元素,我点击的按钮在哪个部分?我应该如何着手实现这样的功能?提前致谢 !

4

0 回答 0