2016-03-24 20 views
-7

私は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リストボックスにデータが表示されないだろうが、=名前

バインディングパスで、それはあなたの質問があるデータベース

+2

あなたの質問は何ですか? – WasGoodDone

+0

申し訳ありません主な質問をするのを忘れてしまいました。私の主な質問は –

+3

です。ご質問はまだ解決できません。申し訳ありません。 http://stackoverflow.com/help/how-to-ask – KarmaEDV

答えて

2

からのデータを示して書くとき、私はここに

   <TextBlock Width="300" Margin="5" Text="{Binding Path=Name}"></TextBlock> 

意味ちょっと散らかったしかし、私はあなたの問題だと思うものに取り組むつもりです。

this.DataContext = new CategoryViewModel(); 

しかし、何もない:

は、あなたが持っている後ろのコードで言います。

ボタンが動作していない理由を確認する際の最初のことは、実行中のアクションを確認することです。 XAMLにはクリックイベントが使用されていると記載されています:

btnDeleteCategory_Click 

これはどこですか?それはあなたのコードビハインドにもありませんか?あなたが何も持っていないことがあるかもしれません。それがあなたのボタンが何もしていない理由です - あなたは何もするように指示していません!

MVVMでは、ViewModelの[プロパティ]にデータをバインドする方法と同様に、ViewModelの[コマンド]を使用してボタンをバインドする必要があります。あなたのビューで

Command="{Binding Path=DeleteCommand}" 

、および:

はあなたのような何かを必要とTxtNameへの結合

public ICommand DeleteCommand 
{ 
    get { return new DelegateCommand<object>(FuncToCall, FuncToEvaluate); } 
} 

private void FuncToCall(object context) 
{ 
    //this is called when the button is clicked - Delete something 
} 

private bool FuncToEvaluate(object context) 
{ 
    //this is called to evaluate whether FuncToCall can be called 
    //for example you can return true or false based on some validation logic 
    return true; 
} 

はそれが実装されていないので、/にPropertyChangedを呼び出す働いていない可能性があります。