c# - Button 包含 DataContext 命令时如何显示新窗口
问题描述
我的 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();
}
我的视图模型之一
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
是新窗口的目标,但即使我将执行更改为简单的消息框,它仍然什么都不做
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>
推荐阅读
- angular - 使用 map 翻译 forEach 语句
- javascript - Vue.js“导入”在外部 js 文件中不起作用
- javascript - 事件是如何传递给这个函数的?
- c - 从 Go 传递 C-struct 时出现 _Ctype_struct_0 错误
- java - FCM 通知未发送声音
- spring - hibernate 是否有一些内部“提交 id”?
- python - Python Dash/Plotly 不能正确显示图表?
- python - 连接两个数据框的问题。两者都具有相同的行数,但它们不能完美地合并
- kubernetes-helm - 嵌套在 helm 图表表达式中
- r - 在数字字段的每个级别上回归的线性模型