首页 > 解决方案 > Button 包含 DataContext 命令时如何显示新窗口

问题描述

我的 XAML 中有一个名为 VMContainer 的视图模型容器

<Window.DataContext>
    <local:VMContainer/>
</Window.DataContext>

如果我从我的容器中访问它,该按钮将不起作用并且没有任何反应。但是如果我直接将它访问到我的MOVIEViewModel 它就可以工作。我不明白这怎么会发生。

这是我的容器

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。

标签: c#wpfdata-bindingviewmodel

解决方案


命令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>

推荐阅读