MVVM itemsource按钮命令和调用另一个视图

本文关键字:调用 另一个 视图 命令 itemsource 按钮 MVVM | 更新日期: 2023-09-27 18:17:17

"请帮忙"。当编辑按钮按下时,我需要从视图模型调用另一个视图。我不能再用了。我从这个视图调用另一个子视图。请帮助

编辑->Delete& lt;/边界>& lt;/边界>& lt;/边界>& lt;/边界>& lt;/边界>& lt;/边界>& lt;/边界>——>

                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
        </Grid>
        </StackPanel>    
    </ScrollViewer>
    <Button Command ="{Binding SubmitCommand}" Name="Submit" Content="Search" HorizontalAlignment="Left" Margin="349,45,0,0" VerticalAlignment="Top" Width="75"/>
    <Button Command ="{Binding CloseCommand}" Name="Close" Content="Close" HorizontalAlignment="Left" Margin="535,379,0,0" VerticalAlignment="Top" Width="75"/>
    <Button Command ="{Binding ReLoadCommand}" Name="Reload" Content="ReLoad" HorizontalAlignment="Left" Margin="535,45,0,0" VerticalAlignment="Top" Width="75"/>
    <Button Command ="{Binding ClearCommand}" Name="Clear" Content="CLear" HorizontalAlignment="Left" Margin="443,45,0,0" VerticalAlignment="Top" Width="75"/>
    <Label x:Name="label" Content="Enter First Name or Last Name for Search" HorizontalAlignment="Left" Margin="84,18,0,0" VerticalAlignment="Top"/>
    <ComboBox ItemsSource="{Binding SelectedYear}" Name="comboBox" HorizontalAlignment="Left" 
              Margin="10,379,0,0" VerticalAlignment="Top" Width="120" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Item}">
        <!--SelectedItem="{Binding Item} -->
    </ComboBox>

</Grid>

ViewModel:

    private ICommand _editRowData;
    public ICommand EditRowData
    {
        get { return _editRowData ?? (_editRowData = new CommandHandler(() => RowDataAction(), _canExecute)); }
    }

   private SecondWindowViewModel childView;
    public void RowDataAction()
    {
        childView = new SecondWindowViewModel();
        //MessageBox.Show("Edit Button Pressed");
        SecondWindow view = new SecondWindow()
        {
            DataContext = childView
        };
        childView.Labelcontent = "kala";
        view.ShowDailog();
        //view.ShowDailog() is giving me error. Please explain and provide me solve. 

MVVM itemsource按钮命令和调用另一个视图

将按钮绑定更改为:

<Button Command="{Binding Path=DataContext.EditRowData, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" ... etc