mvvm - DataGrid.RowDetailsTemplate 使用访问命令方法的麻烦

标签 mvvm rowdetailstemplate

xml代码是:

<DockPanel>
    <DataGrid x:Name="Quote" AutoGenerateColumns="False">
        <DataGrid.Columns >
            <DataGridTextColumn      Header="Code" Binding="{Binding Code}"/>
            <DataGridTextColumn      Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn      Header="Quantities" Binding="{Binding Quantities}"/>
            <DataGridTextColumn      Header="Price" Binding="{Binding Price}"/>
            <DataGridTextColumn      Header="Cost" Binding="{Binding Cost}"/>
            <DataGridCheckBoxColumn  Header="Done"   Binding="{Binding Done}"/>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <Border BorderThickness="0" Background="Beige" Padding="10">
                    <StackPanel Orientation="Horizontal">
                        <Button Command="{Binding Path=DataContext.Renew1Command, RelativeSource= {RelativeSource FindAncestor,AncestorType=DataGrid}}">Call Command</Button>                        
                    </StackPanel>
                </Border>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>
</DockPanel>

模型是:

public class StockModel
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void propChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }   
    private string code;
    public string Code
    {
        get { return code; }
        set
        {
            code = value;
            this.propChanged("Code");
        }
    }                    
    ....          

    private bool done;
    public bool Done
    {
        get { return done; }
        set
        {
            done = value;
            this.propChanged("Done");
        }
    }
    public DelegateCommand Renew1Command;

    public void Renew1(object obj)
    {
        MessageBox.Show("Hello Command");
        System.Console.WriteLine("Hello Command");
    }
    public StockModel()
    {
        Renew1Command = new DelegateCommand();
        this.Renew1Command.ExecuteAction += this.Renew1;
    }             
}

最后我通过以下方式初始化数据源:

List<StockModel> stockList = new List<StockModel>();
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "200" });
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = false, Price = "16.1", Quantities = "100" });
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "100" });
stockList.Add(new StockModel() { Code = "601919", Cost = "8888", Name = "CIB", Done = true, Price = "16.1", Quantities = "200" });
this.Quote.ItemsSource = stockList;

DataGridTextColumn 可以显示绑定(bind)源的数据,
但我无法单击 DataTemplate 的按钮从模型中调用相应的操作。
如何修复绑定(bind)?
调用命令

最佳答案

移至 StockModel 类,替换“public DelegateCommand Renew1Command;”使用“public DelegateCommand Renew1Command { get; set; }”。
问题将得到解决。

关于mvvm - DataGrid.RowDetailsTemplate 使用访问命令方法的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34437363/

相关文章:

wpf - MVVM 弹出问题

WPF MVVM ComboBox SelectedItem 或 SelectedValue 不起作用

Silverlight:业务应用程序需要访问文件才能打印和移动

C#/WPF - DataGrid - 将 RowDetails 中 TextBox 的宽度绑定(bind)到包含 DataGrid 的宽度

wpf - 为什么宽度 ="*"不适用于位于 RowDetailsTemplate 中的 DataGrid 中的列

design-patterns - 是Motif/UIL模型- View - View 模型吗?

wpf - 状态栏不总是更新