gpt4 book ai didi

c# - 如何在我的情况下使用 mvvm 绑定(bind) DataGrid(ObservableCollection 内的 ObservableCollection)

转载 作者:行者123 更新时间:2023-12-03 10:27:40 25 4
gpt4 key购买 nike

我的情况是,我有“tablegenerateModel”类的 ObservableCollection,该类进一步包含“column_Data”类的 ObservableCollection,并且这个“column_Data”类包含 3 个 UI 元素,这些元素必须绑定(bind)到包含三列的 DataGrid。 如果需要,您可以复制粘贴我的代码,我已经编写了所有类和 xaml 供您验证我的代码
所以我的 Model.cs 如下:

  public class tablegenerateModel {

public ObservableCollection < column_Data > Column_data_List {
get;
set;
}
public int NumberOfColumns {
get;
set;
}

private ICommand _CreatColumnCommand; //THIS IS THE BUTTON EVENT GENERATED WHEN USER PRESS AN INTEGER VALUE IN "NumberOfColumns" AND PRESS "Create" button. So the "Column_data_List" will repeat that many times the value user entered
ViewModel vm;
public ICommand CreatColumnCommand {
get {
if (_CreatColumnCommand == null) {
_CreatColumnCommand = new RelayCommand(
param => vm = new ViewModel(this)
);
}
return _CreatColumnCommand;
}
}
}
public class column_Data {
public string Column_Name {
get;
set;
}
}

我的 View 模型是:
   public class ViewModel: INotifyPropertyChanged {
private ObservableCollection < tablegenerateModel > _lb_GlobalList;
private ObservableCollection < column_Data > _Column_data_List;

public ObservableCollection < tablegenerateModel > lb_GlobalList {
get {
return _lb_GlobalList;
}
set {
if (value != _lb_GlobalList) {
_lb_GlobalList = value;
RaisePropertyChanged("lb_GlobalList");
}
}
}

public ObservableCollection < column_Data > Column_data_List {
get {
return _Column_data_List;
}
set {
if (value != _Column_data_List) {
_Column_data_List = value;
RaisePropertyChanged("Column_data_List");
}
}
}

public ICommand ClickCommand

private bool _canExecute;
public ViewModel() {
_canExecute = true;
lb_GlobalList = new ObservableCollection < tablegenerateModel > ();
for (int i = 1; i < 6; i++) {
//Add To it data here
}
}

public ViewModel(tablegenerateModel tablegenerateModels) //IT WILL BE CALLED WHEN BUTTON WILL BE CLICKED (see tablegenerateModel class ICommand for it)
{
Column_data_List = new ObservableCollection < column_Data > ();
for (int i = 1; i < tablegenerateModels.NumberOfColumns; i++) { //Add data here
}
MessageBox.Show("The NumberOfColumns value is:" + tablegenerateModels.NumberOfColumns);
}

}
}

View 是:
 <Window x:Class="PyXgen.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vM="clr-namespace:PyXgen" Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vM:ViewModel></vM:ViewModel>
</Window.DataContext>
<Grid Name="ButtonsContainer">


<ListBox Grid.Row="1" ItemsSource="{Binding lb_GlobalList}" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderBrush="Gray" Margin="0,30,0,0" Grid.RowSpan="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="45" />
<RowDefinition Height="45" />
<RowDefinition Height="45" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Grid Grid.Row="4">
<StackPanel>
<Grid Margin="0,2">


Grid.Row="0" Width="80px" Height="20px" Margin="20,5,0,0" Name="fname4"/>
<Button Grid.Row="1" Grid.Column="1" Height="25" Width="70" Content="Create" Command="{Binding CreatColumnCommand}" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
</Grid>
</StackPanel>
</Grid>
<Grid Margin="0,2" Grid.Row="5">
<DataGrid x:Name="gvSelectedCourses" Grid.Column="1" HorizontalAlignment="Center" ItemsSource="{Binding Column_data_List , Mode=TwoWay}" AutoGenerateColumns="False" Width="450">
<DataGrid.Columns>
<DataGridTextColumn Header="Column Name" Binding="{Binding ,Mode=TwoWay}" Width="150" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>

单击“_CreatColumnCommand”(在 tableGenerateModel 类中)的按钮将调用 ViewModel 构造函数,传递“this”对象以获取输入的整数值以重复 DataGrid 行数次输入的整数值(此整数值在“NumberOfColumns”文本框中输入同级)

现在的问题是如何绑定(bind)“_Column_data_List”,使其添加和更新 DataGrid 的行(通过用户在文本框中输入的整数值)。

目前,当我在文本框中输入整数值并单击“创建”按钮时,会使用 MessageBox 弹出窗口调用 ViewModel 构造函数
 MessageBox.Show("The NumberOfColumns value is:" + tablegenerateModels.NumberOfColumns);

如何将此“_Column_data_List”绑定(bind)到以便添加或更新 DataGrid 的行(通过用户在文本框中输入的整数值,在上面的快照中)。

有人可以帮我吗?

最佳答案

如下更新您的代码。您在 tablegeneratedModel 中绑定(bind)了 observable 集合,但您正在更新 ViewModel 上的集合。

public ViewModel(tablegenerateModel tablegenerateModels) //IT WILL BE CALLED WHEN BUTTON WILL BE CLICKED (see tablegenerateModel class ICommand for it)
{
tablegenerateModels.Column_data_List = new ObservableCollection<column_Data>();
for (int i = 1; i < tablegenerateModels.NumberOfColumns; i++)
{
Column_data_List.Add(lb_col = new column_Data()
{
Column_Name = "ColumnName" + i,
Data_Size = i,
Data_types = "Double" + i
});
}
MessageBox.Show("The NumberOfColumns value is:" + tablegenerateModels.NumberOfColumns);
}

关于c# - 如何在我的情况下使用 mvvm 绑定(bind) DataGrid(ObservableCollection 内的 ObservableCollection),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32716226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com