【问题标题】:Populate a second DataGrid once I double click on a row WPF双击一行 WPF 后填充第二个 DataGrid
【发布时间】:2013-06-16 02:03:13
【问题描述】:

我对 wpf 和 MVVM 还是有点陌生​​。我正在尝试在不破坏该模式的情况下编写解决方案。我有两个(当然三个,但对于这个问题的范围只有两个)DataGrids。我想双击一个行,然后从该行加载数据到第二个DataGrid(理想情况下,我会启动第二个线程来加载数据)。到目前为止,当我双击一行时,我可以弹出一个窗口。我将事件的代码放入 xaml 背后的代码中。对我来说,这似乎很窗户形式。不知何故,我觉得这大大打破了这种模式。

private void DataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) {
    if (popDataGrid.SelectedItem == null) {
        return;
    }
    var selectedPopulation = popDataGrid.SelectedItem as PopulationModel;
    MessageBox.Show(string.Format("The Population you double clicked on has this ID - {0}, Name - {1}, and Description {2}", selectedPopulation.populationID, selectedPopulation.PopName, selectedPopulation.description));
}

这是后面代码中事件的代码,这里是 xaml 中的网格定义:

<DataGrid ItemsSource="{Binding PopulationCollection}" Name="popDataGrid"
          AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected"
          CanUserAddRows="False" Margin="296,120,0,587" HorizontalAlignment="Left" Width="503" Grid.Column="1" 
          MouseDoubleClick="DataGrid_MouseDoubleClick">
</DataGrid>

我认为这段代码应该放在 MainWindowViewModel 中。所以我正在尝试创建一个命令:

public ICommand DoubleClickPopRow { get { return new DelegateCommand(OnDoubleClickPopRow); }}

和相同的事件处理程序:

private void OnDoubleClickPopRow(object sender, MouseButtonEventArgs e) {
}

但是ICommand 在返回DelegateCommand(OnDoubleClickPopRow) 时会抛出异常。

嗯,可以清楚地看到参数的数量不匹配。我知道我做错了什么,但我不太确定它是什么。我会继续研究这个,但如果你们能提供任何帮助,我将不胜感激。

【问题讨论】:

    标签: wpf events datagrid wpfdatagrid delegatecommand


    【解决方案1】:
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    <DataGrid ItemsSource="{Binding PopulationCollection}" Name="popDataGrid"
    AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected"
    CanUserAddRows="False" Margin="296,120,0,587" HorizontalAlignment="Left" Width="503"  Grid.Column="1" SelectedItem="{Binding ItemInViewModel}"></DataGrid>
    <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
    <i:InvokeCommandAction Command="{Binding Save_Bid}" />
    </i:EventTrigger>
    </i:Interaction.Triggers>
    

    您可以将其添加到您的DataGrid 并将您的代码添加到您的视图模型中。 现在我们已经在我们的视图模型中将一个选定的项目绑定到了一个项目,我们可以使用该项目来了解我们何时可以触发我们想要的事件以及当事件被触发时使用什么项目何时可以触发事件

    bool Can_Fire_Event()
    {
    if(ItemInViewModel != null)
    { return true; } else { return false; }
    }
    private RelayCommand _saveBid;
    public ICommand SaveBid
    {
    get
    {
    if (_saveBid == null)
    {
    _saveBid = new RelayCommand(param => Save_Bid(), param => Can_Fire_Event());
    }
    return _saveBid;
    }
    }
    
    public void Save_Bid()
    {
    //Open your new Window here, using your "ItemInViewModel" because this event couldn't be fired from your datagrid unless the "ItemInViewModel" had a value assigned to it
    
    }
    

    【讨论】:

    • 我应该在哪里添加这段代码?我尝试将它添加到 My DataGridTextColumns 行之前。那命名空间呢?我收到一个异常,告诉我找不到它?有需要下载的dll吗?
    • 我仍然对如何实现这一点感到困惑。你能推荐一些阅读吗?
    • 我加载了错误的 dll ......但我仍然不明白如何捕获事件并假设显示一个带有消息的窗口以及该行中的所有项目数据元素?
    • 因此,您的触发命令将转到视图模型中的中继命令。从您的视图模型中,您还将有一个选定的项目绑定到您的网格。因此,当双击发生时,在您的视图模型中,您会知道双击了什么项目,您可以从那里打开一个新窗口,并为该新窗口设置数据上下文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多