由於最近有這方面的需求,而且剛接觸wpf不久,在網上找了很多方法,都不是使用MVVM模式的,因為DataGrid的列不能綁定
這就難受了,我想了個折中的方法,這個是使用了MVVMLight的消息機制,我就不說太多了,直接上代碼
UI界面
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Content="新增列" Command="{Binding AddColumnCmd}" Margin="5"/>
<Button Content="刪除列" Command="{Binding DeleteColumnCmd}" Margin="5"/>
<Button Content="新增數據" Command="{Binding AddDataCmd}" Margin="5"/>
</StackPanel>
<DataGrid x:Name="dataGrid" AutoGenerateColumns="True" Grid.Row="1" SelectionUnit="Cell" SelectionMode="Extended"/>
</Grid>
這里需要在

這里發一個消息,MessageToken如下:其實就是一個字符串類,防止重復
public class MessageToken { /// <summary> /// 設置DataGrid消息 /// </summary> public static readonly string SetDataGrid = nameof(SetDataGrid); }
ViewModel
public class MainViewModel : ViewModelBase { /// <summary> /// Initializes a new instance of the MainViewModel class. DataGrid /// </summary> public MainViewModel() { //注冊設置vm里的DataGrid與界面的相關聯 Messenger.Default.Register<DataGrid>(this, MessageToken.SetDataGrid, (x) => { DDataGrid = x; for (int i = 0; i < 5; i++) { dynamic item = new ExpandoObject(); item.A = "Property A value - " + i.ToString(); item.B = "Property B value - " + i.ToString(); _Items.Add(item); } DDataGrid.Columns.Add(new DataGridTextColumn() { Header = "A", Binding = new Binding("A") }); DDataGrid.Columns.Add(new DataGridTextColumn() { Header = "B", Binding = new Binding("B") }); DDataGrid.ItemsSource = _Items; }); } /// <summary> /// 綁定的數據 /// </summary> ObservableCollection<ExpandoObject> _Items = new ObservableCollection<ExpandoObject>(); public DataGrid DDataGrid; public ObservableCollection<ExpandoObject> Items { get { return _Items; } set { _Items = value; RaisePropertyChanged(()=> Items); } } public RelayCommand AddColumnCmd => new Lazy<RelayCommand>(() => new RelayCommand(AddColumn)).Value; public RelayCommand AddDataCmd => new Lazy<RelayCommand>(() => new RelayCommand(AddData)).Value; public RelayCommand DeleteColumnCmd => new Lazy<RelayCommand>(() => new RelayCommand(DeleteColumn)).Value; private void AddData() { dynamic item = new ExpandoObject(); item.A = "New Item - A"; item.B = "New Item - B"; item.NewColumn1 = "New Item - C"; Items.Add(item); } int newColumnIndex = 1; private void AddColumn() { foreach (IDictionary<String, Object> item in Items) { item.Add("NewColumn" + newColumnIndex, "New Column Value - " + newColumnIndex.ToString()); } DDataGrid.Columns.Add(new DataGridTextColumn() { Header = "NewColumn" + newColumnIndex, Binding = new Binding("NewColumn" + newColumnIndex) }); newColumnIndex++; } private void DeleteColumn() { for (int i = 0; i < DDataGrid.SelectedCells.Count; i++) { //DataRowView Row = (DataRowView)DDataGrid.SelectedCells[i].Item; //string result = Row[DDataGrid.SelectedCells[i].Column.DisplayIndex].ToString(); //string result = DDataGrid.SelectedCells[i].Column.DisplayIndex.ToString(); DDataGrid.Columns.Remove(DDataGrid.SelectedCells[i].Column); } } }
記得在界面上綁定MainViewModel
運行效果

刪除B列

我在嘗試不用消息的方式,直接綁定,成功再發。
