【问题标题】:UWP access controls in ListViewItem from colletion集合中 ListView 项中的 UWP 访问控制
【发布时间】:2019-04-28 22:52:02
【问题描述】:

我有一个 ListView,其中包含用于项目和标题的两个 DataTemplate。 ListView 中的项目绑定到 CollectionViewSource,如下所示:

<CollectionViewSource
            x:Name="groupedItemsViewSource3"
            Source="{Binding Groups2}"
            IsSourceGrouped="true"
            ItemsPath="Items"
            d:Source="{Binding Groups, Source={d:DesignData Source=/DataModel/SampleData.json, Type=data:SampleDataSource}}"/>

我可以设法获得 ListViewItem 但我无法控制它的子控件。 我的 ListView 如下所示:

<ListView
                                            Margin="0,40,0,0"
                                            Width="580"
                                            HorizontalAlignment="Right"
                                            x:Name="itemGridView1"
                                            AutomationProperties.AutomationId="ItemGridView"
                                            AutomationProperties.Name="Grouped Items"
                                            ItemsSource="{Binding Source={StaticResource groupedItemsViewSource2}}"
                                            SelectionMode="None"
                                            IsSwipeEnabled="false"
                                            IsItemClickEnabled="True"
                                            ItemClick="ItemView_ItemClick" Background="White">
                                            <ListView.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid HorizontalAlignment="Left" Background="LightGray" Width="2500" Height="25">
                                                        <Border HorizontalAlignment="Stretch" BorderThickness="0,0,0,1" BorderBrush="Black">
                                                            <StackPanel Orientation="Horizontal">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <TextBlock Text="{Binding Time}" Margin="10,0,0,0" Width="50" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    <TextBlock Text="{Binding LiveTime}" Foreground="{Binding LiveTimeBGColor}" Margin="10,0,0,0" Width="40" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    <TextBlock Text="{Binding TeamOne}" Margin="0,0,10,0" HorizontalTextAlignment="Right" Width="150" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    <Border Background="DarkGray" Width="35" Margin="0,0,2,0" Padding="15,0,0,0">
                                                                    <TextBlock Text="{Binding ScoreTeamOne}"  Width="30" Foreground="White" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    </Border>
                                                                    <Border Background="DarkGray" Width="35" Padding="15,0,0,0" Margin="2,0,0,0">
                                                                    <TextBlock Text="{Binding ScoreTeamTwo}" Foreground="White" Width="30" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    </Border>
                                                                    <TextBlock Text="{Binding TeamTwo}" Margin="10,0,0,0" HorizontalAlignment="Left" Width="150" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" /> 
                                                                </StackPanel>
                                                            </StackPanel>
                                                        </Border>
                                                    </Grid>
                                                </DataTemplate>
                                            </ListView.ItemTemplate>

                                            <ListView.GroupStyle>
                                                <GroupStyle>
                                                    <GroupStyle.HeaderTemplate>
                                                        <DataTemplate>
                                                            <Grid Margin="0,0,0,2" Width="2500" Background="{Binding HeaderLiveBGColor}">
                                                                <Button Foreground="{ThemeResource ApplicationHeaderForegroundThemeBrush}"
                                                                AutomationProperties.Name="Group Title"
                                                                Click="Header_Click"
                                                                Style="{StaticResource TextBlockButtonStyle}" Width="2500">
                                                                    <StackPanel Orientation="Horizontal" Width="2500">
                                                                        <TextBlock Text="{Binding LeagueTitle}" Margin="10,0,0,0" Width="441.9" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    </StackPanel>
                                                                </Button>
                                                            </Grid>
                                                        </DataTemplate>
                                                    </GroupStyle.HeaderTemplate>
                                                </GroupStyle>
                                            </ListView.GroupStyle>
                                            <ListView.ItemsPanel>
                                                <ItemsPanelTemplate>
                                                    <ItemsWrapGrid GroupPadding="0,0,20,0" Orientation="Horizontal"/>
                                                </ItemsPanelTemplate>
                                            </ListView.ItemsPanel>
                                        </ListView>

知道如何检查权限子控件是否被单击? 我最终要实现的是根据单击的 ListViewItem 的控件来处理单击。

【问题讨论】:

  • 为什么要获取 ListViewItem 中的子控件?
  • 从ListViewItem中获取点击的项目

标签: c# windows xaml uwp


【解决方案1】:

从 ListViewItem 中获取被点击的项目

如果你添加断点来调试你的代码,你可以知道在ItemClickEventArgs 类对象中有一个ClickedItemClickedItem 应该是你想要的。

另一种方法是在 SelectedItem 上使用 TwoWay 绑定。

这两种方式都包含在以下代码示例中:

<Page.Resources>
    <CollectionViewSource
        x:Name="groupedItemsViewSource3"
        Source="{Binding Groups2}"
        IsSourceGrouped="true"
        ItemsPath="Items" />
</Page.Resources>
<Grid>
    <ListView
                                        Margin="0,40,0,0"
                                        Width="580"
                                        HorizontalAlignment="Right"
                                        x:Name="itemGridView1"
                                        AutomationProperties.AutomationId="ItemGridView"
                                        AutomationProperties.Name="Grouped Items"
                                        ItemsSource="{Binding Source={StaticResource groupedItemsViewSource3}}"
                                        SelectedItem="{Binding SelectedSong,Mode=TwoWay}"
                                        SelectionMode="Single"
                                        IsSwipeEnabled="false"
                                        IsItemClickEnabled="True"

                                        ItemClick="ItemGridView_ItemClick" Background="White">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Left" Background="LightGray" Width="2500" Height="25">
                    <Border HorizontalAlignment="Stretch" BorderThickness="0,0,0,1" BorderBrush="Black">
                        <StackPanel Orientation="Horizontal">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Title}" Margin="10,0,0,0" Width="50" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>

        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid Margin="0,0,0,2" Width="2500" Background="{Binding HeaderLiveBGColor}">
                            <Button Foreground="{ThemeResource ApplicationHeaderForegroundThemeBrush}"
                                                            AutomationProperties.Name="Group Title"

                                                            Style="{StaticResource TextBlockButtonStyle}" Width="2500">
                                <StackPanel Orientation="Horizontal" Width="2500">
                                    <TextBlock Text="{Binding Key}" Margin="10,0,0,0" Width="441.9" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                </StackPanel>
                            </Button>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsWrapGrid GroupPadding="0,0,20,0" Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</Grid>
public sealed partial class MainPage : Page
{
    public ObservableCollection<SongGroup> Groups2 { get; set; }

    private Song _SelectedSong;

    public Song SelectedSong
    {
        get { return _SelectedSong; }
        set
        {
            _SelectedSong = value;
        }
    }

    public MainPage()
    {
        this.InitializeComponent();
        Groups2 = GenerateData();
        this.DataContext = this;
    }

    private ObservableCollection<SongGroup> GenerateData()
    {
        ObservableCollection<SongGroup> songGroups = new ObservableCollection<SongGroup>();

        ObservableCollection<Song> songs = new ObservableCollection<Song>();
        songs.Add(new Song() { Title = "Song1" });
        songs.Add(new Song() { Title = "Song2" });

        songGroups.Add(new SongGroup() { Key = "A", Items = songs });

        ObservableCollection<Song> songs2 = new ObservableCollection<Song>();
        songs2.Add(new Song() { Title = "Song2_1" });
        songs2.Add(new Song() { Title = "Song2_2" });
        songGroups.Add(new SongGroup() { Key = "B", Items = songs2 });

        return songGroups;
    }

    private void ItemGridView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var song = e.ClickedItem;
    }
}

public class Song
{
    public string Title { get; set; }
}

public class SongGroup
{
    public string Key { get; set; }

    public ObservableCollection<Song> Items { get; set; }
}

【讨论】:

  • e.ClickedItem 将返回 ListViewItem 而不是在 ListViewItem 中单击的子控件。例如,如果您尝试将其转换为 TextBlock,则无论如何都会引发 Null 异常
  • 我认为您并没有真正理解我在寻找什么。我想控制我的 ListViewItem 的子控件
  • @GeorgeH。你想用文本块做什么?获取其文本值?
  • @GeorgeH。为了在DataTemplate中获取控件的属性值,大部分开发者会在之前尝试找到该控件。但这不是一个好习惯。使用 TwoWay 绑定来绑定 SelectedItem 会更好。
  • 我认为这里不可能进行双向绑定,因为我无法绑定选定项,因为有不同类型的组要绑定到列表视图。但是你给了我解决方案。我只需要将 e.ClickedItem 转换为我的 ObservableCollection,然后询问信息。干杯
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
相关资源
最近更新 更多