如何在C#中传递参数rowIndex

时间:2018-11-23 09:55:09

标签: c# mysql wpf xaml parameter-passing

对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]时,它说:

  

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

1 个答案:

答案 0 :(得分:0)

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

df.iloc[:,1:] = df.iloc[:,1:].replace(0, np.nan).ffill().astype(int)

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

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

您可以用表movie_list的其他列名称替换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()); } }

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