gpt4 book ai didi

c# - 在图片上添加点击事件

转载 作者:行者123 更新时间:2023-11-29 09:58:35 26 4
gpt4 key购买 nike

我想添加一个可点击的事件,它会转到特定的查询操作
示例:我有这 3 个列表 Image
当我单击该列表之一时如何添加操作,它会转到我选择的 id 并执行某些操作

Mydb示例Database
查询

select * from library where id_movie = (the one that i clicked)


Xaml

<ItemsControl ItemsSource="{Binding Path=Library}" Margin="0,0,2,0">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="viewModels:Card">
<UniformGrid Rows="1" Columns="1">
<StackPanel Orientation="Horizontal" Width="100" Height="150" Margin="10,25,0,0" >
<Image Width="100" Source="{Binding Path=cover}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="10,0,0,0">
<Label Content="{Binding Path=title}"/>
</StackPanel>
</UniformGrid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>


我的类(class)

public class VModel
{
public VModel()
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = new MySqlCommand("Select * from movie_list", connection);
adapter.Fill(dt);
}

Library = dt.DefaultView;
}

public DataView Library { get; private set; }
}

最佳答案

我想这就是您要找的。每个项目都是一个按钮,其中包含一个图像和一个标签。单击该按钮将调用带有参数的命令(在窗口的 DataContext 上)。

<ItemsControl.ItemTemplate>
<DataTemplate DataType="viewModels:Card">
<Button Command="{Binding DataContext.QueryCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
CommandParameter="{Binding Path=id}">
<StackPanel>
<Image Width="100" Source="{Binding Path=cover}"/>
<Label Content="{Binding Path=title}" HorizontalContentAlignment="Center"/>
<StackPanel>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>

关于c# - 在图片上添加点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53426745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com