当按钮包含 DataContext 命令时如何显示新的 Window
How to Show new Window when Button containing DataContext command
我的 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
是新 window 的目标,但即使我将 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。
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>
我的 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
是新 window 的目标,但即使我将 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。
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>