我的XAML中有一个名为VMContainer的视图模型容器
<Window.DataContext>
<local:VMContainer/>
</Window.DataContext>
如果我从容器中访问它,该按钮将不起作用,并且什么也不会发生。但是如果我直接将其访问到我的MOVIE
ViewModel,它就可以工作。而且我不知道这怎么会发生。
这是我的容器
public class VMContainer
{
public Movie Movie { get; set; } = new Movie();
public TV TV { get; set; } = new TV();
}
我的一个ViewModel
public class Movie
{
public ICommand Clicked { get; private set; }
public DataView Library { get; private set; }
public Movie()
{
DataTable data = new DataTable();
using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = new MySqlCommand("Select * from index_movie_list", connection);
adapter.Fill(data);
}
Library = data.DefaultView;
Clicked = new MovieClicked(this);
}
}
点击方法
Pages.MovieDetail
是新窗口的目标,但是即使我将Execute更改为简单的MessageBox,它仍然无济于事
partial class MovieClicked : ICommand
{
private Movie __vModel;
public MovieClicked(Movie vModel)
{
__vModel = vModel;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
public bool CanExecute(object parameter)
{
return true;
}
private static int a;
private static string b;
private static string c;
public static DataTable d = new DataTable();
public void Execute(object parameter)
{
var id_movie = (int)parameter;
var rowIndexx = id_movie - 1;
a = (int)(__vModel.Library[rowIndexx]["id_movie"]);
b = (string)(__vModel.Library[rowIndexx]["target"]);
c = (string)(__vModel.Library[rowIndexx]["title"]);
using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
connection.Open();
adapter.SelectCommand = new MySqlCommand("Select * from index_movie_list where id_movie ='" + a + "'", connection);
adapter.Fill(d);
connection.Close();
}
z = a;
x = b;
y = c;
Pages.MovieDetail subWindow = new Pages.MovieDetail();
subWindow.Show();
}
public static int z;
public static string x;
public static string y;
}
最后是我的XAML
我的xaml包含id_movie
,用于在单击按钮的位置获取ID
<ItemsControl Background="#191919" ItemsSource="{Binding Path=Movie.Library}" BorderThickness="0">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="viewModels:Card">
<Button Style="{StaticResource OrangeButton}" Margin="0,2,0,0" Command="{Binding Path=DataContext.Clicked, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" CommandParameter="{Binding Path=id_movie}">
<StackPanel Margin="0,0,0,0">
<Grid Margin="5,5,5,5">
<Rectangle RadiusX="0" RadiusY="0" Width="150" Height="230">
<Rectangle.Fill>
<ImageBrush x:Name="myImage" ImageSource="{Binding Path=cover}"/>
</Rectangle.Fill>
</Rectangle>
<Label FontFamily="Bebas Neue" Background="#CC000000" Margin="115,10,0,193" Content="{Binding Path=year}" HorizontalContentAlignment="right" Foreground="White"/>
<Label FontFamily="Bebas Neue" Width="150" Background="#CC000000" Margin="0,187,0,16" Content="{Binding Path=title}" HorizontalContentAlignment="Center" Foreground="White"/>
</Grid>
</StackPanel>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
注意:如果我在没有容器的情况下直接使用Movie
,则我的按钮可以使用,但不能将其用于其他ViewModel。
答案 0 :(得分:2)
Binding
命令的Clicked
路径中存在错误。
<Button Margin="0,2,0,0" Command="{Binding Path=DataContext.Movie.Clicked, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" CommandParameter="{Binding Path=id_movie}"></Button>