我正在尝试使用silverlight-c#(不使用galla或prism或任何内置的东西)来实现MVVM方法。
我需要做的事情: (我在第一个视图中有一个按钮,在该按钮下面有一个网格,用于短时间显示一个列表)。当我按下一个按钮时,它会弹出一张表格 将输入信息,该信息应显示在图像1中“程序文本”下方区域的列表中
我所做的是:
我已成功创建按钮单击事件并读取用户从表单中写入的数据。现在的问题是我无法在**程序的区域内呈现用户从该表单读取的数据。
您可以在此处看到 程序 区域,其中包括:名称,作者和日期创建等等。就在下面,我想在按钮点击(“添加程序”按钮)上获得的表格上显示我将从用户那里读取的数据。
(“添加程序”是要单击的按钮) 点击后我得到了:
ProgramGrid.xaml中用于绑定
的代码<UserControl
x:Class="DEV_CENTER.ProgramGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
xmlns:local="clr-namespace:DEV_CENTER"
xmlns:vm="clr-namespace:ViewModel;assembly=ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="5 5 5 20" Background="White">
<TextBox Text="">
<TextBox.Background>
<ImageBrush ImageSource="Images/search_icon.png" Stretch="None" AlignmentX="Right" />
</TextBox.Background>
</TextBox>
</Grid>
<Button Click="OnClick" Grid.Row="1" Height="25" >Add Programs</Button>
<TextBlock Text="Programs" Grid.Row="2" FontWeight="Bold"/>
<data:DataGrid Grid.Row="3" x:Name="gridPrograms" SelectedItem="{Binding SelectedProgram, Mode=TwoWay}"
AutoGenerateColumns="False" ItemsSource="{Binding Programs}" IsReadOnly="True">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="3*"></data:DataGridTextColumn>
<data:DataGridTextColumn Header="Author" Binding="{Binding Path=Author}" Width="2*" ></data:DataGridTextColumn>
<data:DataGridTextColumn Header="Date Creation" Binding="{Binding Path=DateCreation}" Width="3*"></data:DataGridTextColumn>
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
</UserControl>
ProgramGrid.xaml.cs的代码:
namespace DEV_CENTER
{
public partial class ProgramGrid : UserControl
{
public ProgramGrid()
{
InitializeComponent();
this.DataContext = AddButtonViewModel.getInstance();
}
private void OnClick(object sender, System.Windows.RoutedEventArgs e)
{
AddButton addProgramObj = new AddButton(); //Because AddButton is my xaml childwindow
addProgramObj.Show();
}
}
}
AddButton.xaml和AddButton.cs的按钮代码没有问题(我的意思是我在按钮点击时获得的表格中输入的所有数据在调试时在相应的字段(名称,作者等)上都可见。)< / p>
AddButton.cs:
namespace DEV_CENTER
{
public partial class AddButton : ChildWindow
{
public AddButton()
{
this.DataContext = AddButtonViewModel.getInstance();
InitializeComponent();
}
}
}
AddButtonViewModel.cs是我的View模型类。 我猜它有问题。
namespace ViewModel
{
public class AddButtonViewModel : GenericViewModel
{
private static AddButtonViewModel addbuttonviewModel;
public static AddButtonViewModel getInstance()
{
if (addbuttonviewModel == null) addbuttonviewModel = new AddButtonViewModel();
return addbuttonviewModel;
}
#region PROPERTIES
private ObservableCollection<AddButtonViewModel> programs; //Its obtained for binding from ProgramGrid.xaml
public ObservableCollection<AddButtonViewModel> Programs
{
get { return this.programs; }
set { this.programs = value; OnPropertyChanged("Programs"); }
}
private String name;
public String Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
private String version;
public String Version
{
get { return version; }
set { version = value; OnPropertyChanged("Version"); }
}
private String author;
public String Author
{
get { return author; }
set { author = value; OnPropertyChanged("Author"); }
}
private String company;
public String Company
{
get { return company; }
set { company = value; OnPropertyChanged("Company"); }
}
private String dateCreation;
public String DateCreation
{
get { return dateCreation; }
set { dateCreation = value; OnPropertyChanged("DateCreation"); }
}
private String description;
public String Description
{
get { return description; }
set { description = value; OnPropertyChanged("Description"); }
}
private ICommand saveCmd; ITS for Save Button click after writing the data on form popuped.
public ICommand SaveCmd
{
get { return saveCmd; }
set { saveCmd = value; }
}
private ICommand cancelCmd;
public ICommand CancelCmd
{
get { return cancelCmd; }
set { cancelCmd = value; }
}
#endregion
#region CONSTRUCTOR
public AddButtonViewModel()
{
this.saveCmd = new DelegateCommand(SaveMetadata);
this.cancelCmd = new DelegateCommand(CancelMetadata);
}
#endregion
#region method
/*
* save from Form
*/
public void SaveMetadata(object param) //i can see on debugging that i have all the fields thati have saved in form
{
programs = new ObservableCollection<AddButtonViewModel>();
AddButtonViewModel p1 = new AddButtonViewModel();
p1.Name = Name;
p1.Author = Author;
p1.Company = Company;
p1.Description = Description;
p1.DateCreation = DateCreation;
Programs.Add(p1);
}
/*
* cancel modification action
*/
public void CancelMetadata(object param)
{
initForm();
}
#endregion
}
}
如何处理我的AddButtonViewModel.cs类,以便我能够以完全相同的方式呈现程序下面的List(包含从Form中保存的数据)?
答案 0 :(得分:1)
在您的代码中,您将程序添加到基础数据结构(programs
)而不是属性。您应该将其添加到Programs
,以便设置者提升OnPropertyChanged
。此外,您需要更改Programs
属性以绑定到模型的值(ButtonProgram
)。
public void SaveMetadata(object param) //i can see on debugging that i have all the fields thati have saved in form
{
ObservableCollection<ButtonProgram> newCollection = new
ObservableCollection<ButtonProgram>(programs.ToList());
ButtonProgram p1 = new ButtonProgram();
p1.Name = Name;
p1.Author = Author;
p1.Company = Company;
p1.Description = Description;
p1.DateCreation = DateCreation;
newCollection.Add(p1);
Programs = newCollection;
}
此更改应告知您的UI更新列表。