我是C#和MySQL的新手。
在我之前的关于如何显示包含SQL数据的messageBox的问题的answer中,他提到:


  调用Execute方法时,将参数rowIndex和stringToSearch传入。


而且我仍然不明白它是如何工作的。
下面是我的ViewModel

public class VModel
{
    public ICommand Clicked { get; set; }
    public DataView Library { get; private set; }
    public VModel()
    {
        DataTable dt = new DataTable();
        using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("Select * from movie_list where id_movie = @id_movie", connection);
            adapter.Fill(dt);

        }

        var Library = dt.DefaultView;
        var Clicked = new ClickedCommand(this);
    }

}


请点击

 internal class ClickedCommand : ICommand
{
    private VModel _vModel;
    public ClickedCommand(VModel vModel)
    {
        _vModel = vModel;
    }
    public event EventHandler CanExecuteChanged { add { } remove { } }
    public bool CanExecute(object parameter)
    {
            return true;
    }
    public void Execute(object parameter)
    {
        var rowIndex = (string)parameter[0];
        var stringToSearch = (string)parameter[1];
        MessageBox.Show(_vModel.Library.[rowIndex][stringToSearch]);
    }

}


Xaml

<ScrollViewer>
        <ItemsControl Background="#191919" ItemsSource="{Binding Path=Library}" Margin="130,0,2,0" BorderThickness="0">
            <ItemsControl.ItemTemplate>
            <DataTemplate DataType="viewModels:Card">
                <Button Margin="0,2,0,0" Command="{Binding Path=DataContext.Clicked, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
                    <StackPanel Margin="0,0,0,0">
                            <Grid Margin="5,5,5,5">
                                <Rectangle RadiusX="5" RadiusY="5" Width="150" Height="230">
                                    <Rectangle.Fill>
                                        <ImageBrush  x:Name="myImage" ImageSource="{Binding Path=cover}"/>
                                    </Rectangle.Fill>
                                </Rectangle>
                                <Label Background="#CC000000" Margin="107,10,0,193" Content="{Binding Path=year}" HorizontalContentAlignment="right" Foreground="White"/>
                                <Label Width="150" Background="#CC000000" Margin="0,203,0,0" Content="{Binding Path=title}" HorizontalContentAlignment="Center" Foreground="White"/>
                            </Grid>

                        <Label Visibility="Visible"  Name="lbl" HorizontalContentAlignment="Center" Tag="labl"/>
                    </StackPanel>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
    </ItemsControl>
    </ScrollViewer>


var rowIndex = (string)parameter[0];var stringToSearch = (string)parameter[1];上显示以下内容:


  无法将带有[]的索引应用于类型为“对象”的表达式



当我将[rowIndex]更改为[0]时,它说:


  无法从“对象”转换为“字符串”

最佳答案

我认为您需要在DataView中加载所有电影:

public class VModel
{
    public ICommand Clicked { get; set; }
    public DataView Library { get; private set; }
    public VModel()
    {
        DataTable dt = new DataTable();
        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);

        }

        var Library = dt.DefaultView;
        var Clicked = new ClickedCommand(this);
    }
}


当您单击时,将显示标题:

internal class ClickedCommand : ICommand
{
    private VModel _vModel;
    public ClickedCommand(VModel vModel)
    {
        _vModel = vModel;
    }
    public event EventHandler CanExecuteChanged { add { } remove { } }
    public bool CanExecute(object parameter)
    {
            return true;
    }
    public void Execute(object parameter)
    {
        var id_movie = (int)parameter;
        var rowIndex = id_movie - 1;
        MessageBox.Show(_vModel.Library[rowIndex]["title"].ToString());
    }
}


您可以用表movie_list的其他列名替换"title"

此解决方案不使用Sql参数。不确定是否很好。

关于c# - 如何在C#中传递参数rowIndex,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53444329/

10-10 06:26