简体   繁体   English

如何使用 mvvm 将 gridview 选定的行数据发送到另一个 window 与 wpf 中的文本框使用 mvvm

[英]How to send gridview selected row data to another window with textboxes in wpf using mvvm

I have tried a bunch of solutions about this problem on google but none seem to be helpful.我在谷歌上尝试了很多关于这个问题的解决方案,但似乎都没有帮助。 I have a button on every row which when clicked open a new window with textboxes.我在每一行都有一个按钮,点击它会打开一个带有文本框的新 window。 This window should display the selected row cells data.这个 window 应该显示选定的行单元格数据。 I load the datagrid from mysql database.我从 mysql 数据库加载数据网格。

VIEW看法

textboxes (XML) for second window第二个 window 的文本框 (XML)

<Label Content="{Binding sFirstName, Mode=OneWay }" /> <Label Content="{Binding sLastName, Mode=OneWay }" /> <Label Content="{Binding sFirstName, Mode=OneWay }" /> <Label Content="{Binding sLastName, Mode=OneWay }" />

Datagrid数据网格

<DataGrid ItemsSource="{Binding Path=MM}" SelectionMode="Extended" SelectedItem="{Binding SelectedItem}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=sFirstName}" />
<DataGridTextColumn Binding="{Binding Path=sLastName}" />
</DataGrid.Columns>

MODEL MODEL

public class MM : INotifyPropertyChanged
   {
       public event PropertyChangedEventHandler PropertyChanged;
       public void OnPropertyChanged([CallerMemberName]string PropertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName)); }

       private string _sFirstName, _sLastName;

       public string sFirstName { get { return _sFirstName; } set { if (_sFirstName != value) { _sFirstName = value; OnPropertyChanged("sFirstName"); } } }

       public string sLastName { get { return _sLastName; } set { if (_sLastName != value) { _sLastName = value; OnPropertyChanged("sLastName"); } } }

       public DataRowView SelectedRow
       {
           get { return SelectedRow; }
           set { SelectedRow = value; OnPropertyChanged("SelectedItem"); }
       }
}

VIEW MODEL查看 MODEL

Public class MV : INotifyPropertyChanged
{
private ICommand cmdLoad;
public ICommand CmdLoad { get { if (cmdLoad == null) cmdLoad = new RelayCommand(p => OnLoad()); return cmdLoad; } }

private void OnLoad() { Load(); }

public ObservableCollection<FinTuitionM> finTuitionM { get; set; }
       
public ClrIdVMD()
{
Load();
}

public void Load()
{

}
}

Code behind (cs)代码背后(cs)

public partial class Home : Window
{
MV mv;
public Home()
{ InitializeComponent();
mv = new MV(); DataContext = mv;
}
}

You seem to be very confused, so I have prepared a small example of what I think you are trying to achieve.你似乎很困惑,所以我准备了一个我认为你正在努力实现的小例子。

I am guessing that you want to have a main view that is essentially read only, and you intend to use a popup to make changes.我猜您想要一个基本上只读的主视图,并且您打算使用弹出窗口进行更改。 On this basis the View Model for the main window does not need to implement INotifyPropertyChanged.在此基础上查看Model为主window不需要实现INotifyPropertyChanged。 So a simple View Model would look like this:所以一个简单的视图 Model 看起来像这样:

public class MV 
{

    public ObservableCollection<MM> MMs { get; set; }

    private ICommand cmdShowDetails;
    public ICommand CmdShowDetails
    {
        get
        {
            if (cmdShowDetails == null) cmdShowDetails = new RelayCommand(p => ShowDetails());
            return cmdShowDetails;
        }
    }

    public void ShowDetails()
    {
        var detVM = new DetailsVM(SelectedItem);
        var dets = new DetailsWindow(detVM);
        dets.ShowDialog();
    }

    public MV()
    {
        MMs = new ObservableCollection<MM>
        {
            new MM{sFirstName = "Mickey", sLastName = "Mouse"},
            new MM{sFirstName = "Donald", sLastName = "Duck"},
            new MM{sFirstName = "Roger", sLastName = "Rabbit"},
        };
    }
    public MM SelectedItem { get; set; }

}

Notice that for demonstration purposes, I have loaded the ObservableCollection with some dummy data.请注意,出于演示目的,我在ObservableCollection中加载了一些虚拟数据。 In your case, this is replaced with data from the database.在您的情况下,这将替换为数据库中的数据。

The MM class that this refers to then looks something like this:这里指的 MM class 看起来像这样:

public class MM : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChangedEvent(string propertyName)
    {
        if (PropertyChanged != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            PropertyChanged(this, e);
        }
    }

    private string firstName;
    public string sFirstName
    {
        get { return firstName; }
        set
        {
            if (firstName == value)
            {
                return;
            }
            firstName = value;
            RaisePropertyChangedEvent("sFirstName");
        }
    }
    private string lastName;
    public string sLastName
    {
        get { return lastName; }
        set
        {
            if (lastName == value)
            {
                return;
            }
            lastName = value;
            RaisePropertyChangedEvent("sLastName");
        }
    }
}

Notice that SelectedItem is in the View Model (MV) and is an object of class MM, so that when the second window is opened, the ShowDetails command can pass the selected details. Notice that SelectedItem is in the View Model (MV) and is an object of class MM, so that when the second window is opened, the ShowDetails command can pass the selected details.

This therefore calls for a new very simple view model for the second (details) window:因此,这需要一个新的非常简单的视图 model 用于第二个(详细信息)window:

public class DetailsVM
{
    public MM Detail { get; set; }

    public DetailsVM(MM detail)
    {
        Detail = detail;
    }
}

The main window grid xaml now looks like this:主 window 网格 xaml 现在看起来像这样:

<Grid>
    <DockPanel>
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
            <Button Content="Show Details" Command="{Binding CmdShowDetails}"></Button>
        </StackPanel>            
        <DataGrid ItemsSource="{Binding MMs}" SelectedItem="{Binding SelectedItem}"  AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="First Name" Binding="{Binding sFirstName}" />
                <DataGridTextColumn Header="Last Name" Binding="{Binding sLastName}" />
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</Grid>

Notice here that I only have one button at the bottom of the window to transfer the details.请注意,我在 window 的底部只有一个按钮来传输详细信息。 This is because the details come from the selected item, which is the highlighted row.这是因为详细信息来自所选项目,即突出显示的行。

The code behind is simply:后面的代码很简单:

public partial class MainWindow : Window
{
    private MV _mV;
    public MainWindow()
    {

        InitializeComponent();
        _mV = new MV();
        DataContext = _mV;
    }
}

Finally the xaml for the second (details) window最后是xaml为第二个(详情)window

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40*"/>
        <RowDefinition Height="40*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="70*"/>
        <ColumnDefinition Width="200*"/>
    </Grid.ColumnDefinitions>
    <Label Content="First Name" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Center"/>
    <TextBox Text="{Binding Detail.sFirstName}" Grid.Column="1" Grid.Row="0" Width="150" Height="25" HorizontalAlignment="Left" />
    <Label Content="Last Name" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center"/>
    <TextBox Text="{Binding Detail.sLastName}" Grid.Column="1" Grid.Row="1" Width="150" Height="25" HorizontalAlignment="Left" />
</Grid>

Notice here that the binding is to Detail.sFirstName and Detail.sLastName.请注意,这里绑定的是 Detail.sFirstName 和 Detail.sLastName。 The DataContext is a DetailsVM object, which has a property Detail of type MM, hence sFirstName and sLastName are sub-properties of Detail. DataContext 是一个 DetailsVM object,它有一个类型为 MM 的属性 Detail,因此 sFirstName 和 sLastName 是 Detail 的子属性。

This window also has a very simple code behind:这个 window 后面还有一段很简单的代码:

public partial class DetailsWindow : Window
{
    private DetailsVM _details;
    public DetailsWindow(DetailsVM details)
    {
        _details = details;
        DataContext = _details;
        InitializeComponent();
    }
}

If you now run this, you will find that changes made in the second window are automatically reflected back into the main window.如果现在运行它,您会发现在第二个 window 中所做的更改会自动反映回主 window。 In practice you will probably want Save and Cancel buttons in the second window.在实践中,您可能需要第二个 window 中的保存和取消按钮。

I hope the above is sufficient to point you in the right direction!我希望以上内容足以为您指明正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 WPF 和 MVVM 将 ComboBox 中的选定数据从一个窗口显示到另一个窗口? - How can I display the selected data from a ComboBox from one window to another with WPF and MVVM? 如何使用WPF和MVVM模式从窗口获取数据到另一个 - How to get data from window to another with WPF and MVVM pattern 如何在不使用 MVVM 的情况下从另一个窗口更新 WPF 中的 dataGrid? - How to update dataGrid in WPF from another window without using MVVM? 如何使用mvvm显示WPF列表框中所选项目的数据? - How to show data from selected item in listbox wpf using mvvm? 如何使用Lambda / LINQ和WPF MVVM从gridview中删除所选对象 - How to Remove selected object from gridview using Lambda/LINQ & WPF MVVM 如何使用MVVM Wpf从文本框中添加文本并将其添加到datagrid - how to add text from textboxes and add it to datagrid using MVVM Wpf 如何在C#中使gridview将所有检查的行数据放入另一种形式的文本框中? - How to get gridview all checked row data into textboxes of another form in C#? 如何在 WPF C# 中为 DataGrid 获取 TextBoxes 中选定的行值 - How to get the selected Row values in TextBoxes for DataGrid in WPF C# 如何使用wpf和使用mvvm将窗口带到前面 - How to bring window to front with wpf and using mvvm 在WPF MVVM中如何将数据从Window.xaml.cs发送到WindowViewModels - in WPF MVVM How to send data from Window.xaml.cs to WindowViewModels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM