有没有一种方法可以使ItemsControl中的Borders可单击?

时间:2019-10-31 09:18:28

标签: c# wpf

我创建了这样的表格视图:

table view

这些单元格彼此不同。我希望这些单元格是可点击的。但是我不知道该怎么做。在Android上,有一个名为
的监听器 setOnItemClickListener,但我不知道WPF上的等效项。我知道SelectedItem可以用于ListView,但是我需要这个确切的视图。我无法自定义ListView。

这是我为此表视图创建的代码:

<Grid>
    <ScrollViewer>
        <StackPanel>
            <ItemsControl x:Name="ListViewTableBorder" ItemsSource="{Binding TableName}" Selector.Selected="ListViewTableBorder_Selected">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border Style="{StaticResource sales_border}" MouseLeftButtonUp="Border_MouseLeftButtonUp">
                            <TextBlock Name="tablename" Text="{Binding tablename}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="20"/>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </StackPanel>
    </ScrollViewer>
</Grid>

这是sales_border样式:

  <Style x:Key="sales_border" TargetType="Border">
        <Setter Property="Height" Value="100"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="Background" Value="#0a7d38"/>
        <Setter Property="CornerRadius" Value="8"/>
        <Setter Property="Margin" Value="5,5,0,0"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#00d052"/>
            </Trigger>
        </Style.Triggers>
    </Style>

这是我用来创建这些单元格的表数据。

  public hall_layout()
    {
        InitializeComponent();
        var tablename = GetTableNames();
        if (tablename.Count > 0)
        {
            ListViewTableBorder.ItemsSource = tablename;
        }
    }

    private List<TableName> GetTableNames()
    {
        return new List<TableName>()
        {
            new TableName("Salon 1"),
            new TableName("Salon 2"),
            new TableName("Salon 3"),
            new TableName("Salon 4"),
            new TableName("Salon 5"),
            new TableName("Salon 6"),
            new TableName("Salon 7"),
            new TableName("Salon 8"),
            new TableName("Salon 9"),
            new TableName("Salon 10"),
            new TableName("Salon 11"),
            new TableName("Salon 12"),
            new TableName("Salon 13"),
            new TableName("Salon 14"),
            new TableName("Salon 15"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
            new TableName("Salon 16"),
        };
    }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

ItemsControl不处理SelectionChanged,并且不具有SelectedItem属性。相反,您可以将ListBox与{{1}一起用作ItemsPanel,然后使用WrapPanel中的SelectedItem并可以处理ListBox

SelectionChanged
相关问题