我正在开发一个 WPF Projet,其中我有一个带有两个用户控件的视图。这基本上是一个带有网格的用户控件和另一个带有编辑面板的用户控件,用于编辑 DataGrid 中的选定对象。编辑面板控件由用于编辑另一个控件中选定对象的属性的文本框和一个用于保存的按钮组成。我想做的是将所选对象传递给编辑面板,即每次在网格中选择一个对象时,编辑面板都会更新以选择同一个对象。最好的方法是什么,请帮忙?一个例子是超级:0)
问问题
1286 次
2 回答
2
处理这个问题的最好方法是使用 MVVM 模式,其中您的两个用户控件都绑定到同一个 ViewModel。
网格可以绑定到您想要显示的对象的集合 (List<>),它还可以将其 SelectedRow/SelectedItem 属性绑定到 ViewModel 上名为 SelectedItem(或类似的)的相应属性。这意味着每次在网格中选择一行时,底层数据对象都会填充到 ViewModel 上的属性中。
然后将您的详细信息用户控件绑定到 ViewModel 上的相同 SelectedItem 属性。检查这个非常简单的 DataGrid 和 TextBox 绑定到相同 SelectedItem 属性的示例:
视图模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WpfApplication11
{
public class MyViewModel : INotifyPropertyChanged
{
public List<Customer> MyList
{
get { return _myList; }
set
{
_myList = value;
OnPropertyChanged("MyList");
}
}
public Customer SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
private Customer _selectedItem;
private List<Customer> _myList;
}
public class Customer
{
public string Name { get; set; }
}
}
主窗口
<Window x:Class="WpfApplication11.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:swm="clr-namespace:System.Windows.Media;assembly=WindowsBase"
xmlns:swm1="clr-namespace:System.Windows.Media;assembly=PresentationCore"
Title="MainWindow" Height="289" Width="525">
<Grid>
<DataGrid AutoGenerateColumns="True" Margin="12,12,12,38" Name="dataGrid1" ItemsSource="{Binding MyList}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
<Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="12,222,0,0" Name="label1" VerticalAlignment="Top" Width="57" />
<TextBox Text="{Binding Path=SelectedItem.Name, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="60,222,0,0" Name="textBox1" VerticalAlignment="Top" Width="267" />
</Grid>
</Window>
后面的 MainWindow 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication11
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
MyViewModel vm = new MyViewModel();
vm.MyList = new List<Customer>(new Customer[] { new Customer() { Name = "Bugs Bunny" }, new Customer() { Name = "Elmer Fudd" } });
this.DataContext = vm;
}
}
}
如果您运行此程序,然后在网格中选择一行,则客户名称将填充到下面的文本框中。如果您随后修改文本框中的名称并从中删除焦点(TAB 出来),则数据网格中的行将使用新名称更新 - 全部通过绑定。
有关更多信息,以前在 Stack Overflow 上有几千个关于 WPF 的 MVVM 模式的问题,其中许多问题专门针对您想要的主从视图。
于 2011-03-14T07:34:09.213 回答
0
在您的 XAML 标记中,只需将编辑面板的对象绑定到网格对象中的选定对象。
于 2011-03-14T07:17:39.097 回答