在图像上添加点击事件

ogq8wdun  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(428)

我想添加一个可点击的事件,它会转到特定的查询操作
示例:我有这个3列表图像
如何添加一个动作,当我点击其中一个列表,它转到我选择的id和正在做的事情
mydb示例数据库
查询

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>

我的班级

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; }
}
m528fe3b

m528fe3b1#

我想这就是你要找的。每个项目都是一个按钮,其中包含一个图像和一个标签。单击按钮调用一个带有参数的命令(在窗口的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>
zphenhs4

zphenhs42#

把图像控件改为下面的代码,它会和按钮一起显示图像。请参见命令绑定到全局datacontext,因为在viewmodel中我将声明命令。我使用命令参数并将id传递给那里。

<Button Command="{Binding Path=DataContext.DoSomething, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" CommandParameter="{Binding id}">
     <StackPanel>
          <Image Width="100" Source="{Binding Path=cover}"/>
     </StackPanel>
</Button>

然后需要在viewmodel中声明命令

public ICommand DoSomething {get;set;}

并在构造函数中初始化它

DoSomething = new DoSomethingCommand(this);

为命令的定义添加另一个类

internal class DoSomethingCommand: ICommand
    {
        private VModel vm;

        public PlotPlane(VModel mainViewModel)
        {
            this.vm = mainViewModel;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {

            var id = (id)parameter;

            //do something, use vm to access your viewmodel

        }
    }

相关问题