私はmvvmパターンで簡単なプロジェクトをやっています。そのおよそ1行ごとに1つのテキストボックスを持っており、ボタンを削除するには、リスト、我々は1つのテキストボックスを持っているし、このようにボタンを追加WPF MVVMパターン
湖底で:
名1 buttondelete
name2のbuttondelete
NAME3 buttondelete
。
。行は削除する必要がありますbuttondeleteをクリックして、クリックでで
をbuttonadd
テキストボックスは、テキストボックスのテキストが新しい
行としてリストに挿入する必要がありbottonadd。
私は3つのレイヤーSepand.WPFProject.Model、Sepand.WPFProject.ViewModel、Sepand.WPFProject.Viewを持っています。
モデルにはコンテキストとリポジトリとモデルがあります(ここで私のモデルはID番号が& IDプロパティのクラスです)。私は
this.DataContext = new CategoryViewModel();
を持っており、XAMLで私は
を持って後ろにコード内で表示中public class CategoryViewModel
{
ModelRepository<Category> repository = new ModelRepository<Category>();
ObservableCollection<Category> categories = new ObservableCollection<Category>();
Category category = new Category();
public ObservableCollection<Category> GetAll()
{
IQueryable<Category> categoryRepository = repository.GetAll();
foreach (Category Category in categoryRepository)
categories.Add(Category);
return categories;
}
public ObservableCollection<Category> GetAllCategories
{
get { return GetAll(); }
}
public string TxtName
{
get { return category.Name; }
set { category.Name = value; }
}
:リポジトリは、このようなものです:ViewModelにで
public class ModelRepository<T>
where T : class
{
ModelDbContext ctx = new ModelDbContext();
public IQueryable<T> GetAll()
{
IQueryable<T> query = ctx.Set<T>();
return query;
}
public void Add(T entity)
{
ctx.Set<T>().Add(entity);
ctx.SaveChanges();
}
public void Delete(T entity)
{
ctx.Set<T>().Remove(entity);
ctx.SaveChanges();
}
私はこのようなcategoryViewModelクラスを持っています
<Window.Resources>
<DataTemplate x:Key="CategoryTemplate">
<Border Width="400" Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4">
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock Width="300" Margin="5" Text="{Binding Path=Name}"></TextBlock>
<Button Name="btnDeleteCategory" Width="50" Margin="5" Click="btnDeleteCategory_Click" >-</Button>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
。
。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox Grid.Column="0" Grid.Row="0" Name="lstCategory" ItemTemplate="{StaticResource CategoryTemplate}" ItemsSource="{Binding Path=GetAllCategories}"/>
<StackPanel Margin="5" Grid.Column="0" Grid.Row="1" Orientation="Horizontal">
<Label Content="Name : "/>
<TextBox Name="TxtName" Text="{Binding Path=TxtName ,Mode=TwoWay}" Width="260"/>
<Label Width="50"/>
<Button Width="50" Content="+" Name="btnAddCategory" Click="AddCategory_Click" />
</StackPanel>
</Grid>
</Grid>
、今私はアプリを実行すると、データベースからデータが移入リストボックス。しかし、私は追加ボタンのコードを書くことができませんでした。
削除ボタン;
私は何をすればいいのですか?
なぜ、私はリストのテキストボックスのテキストをCategoryViewModelクラスのTxtNameプロパティにバインドできませんでしたか?
私はバインディングパス= TxtNameリストボックスにデータが表示されないだろうが、=名前
バインディングパスで、それはあなたの質問があるデータベース
あなたの質問は何ですか? – WasGoodDone
申し訳ありません主な質問をするのを忘れてしまいました。私の主な質問は –
です。ご質問はまだ解決できません。申し訳ありません。 http://stackoverflow.com/help/how-to-ask – KarmaEDV