2017-10-31 13 views
0

processTimeEditor00.xmlビュー内のアイテムリストをグリッドにバインドしようとしています。ビューモデルはtimeHandlerと呼ばれ、INotifyPropertyChangedインタフェースから継承し、ボタンの動作buttonAddNewProcessTimeCommandはICommandインタフェースから継承します。私は、 "プロセス時間の追加"ボタンがクリックされ、リストが新しい要素を取得するたびに更新されるようにビューをプロセスに配線しようとしています。ここ はここINotifyPropertyChangedとICommandを使用しているWpf。ビューが更新されない

public class timeItem 
{ 
    public Int32 hours { get; set; } 
    public Int32 mins { get; set; } 
    public String AMPM { get; set; } 
} 

はここに私のビュー

<Window x:Class="timeSetter.processTimeEditor00" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:timeSetter" 
    xmlns:viewModels="clr-namespace:timeSetter.ViewModels" 
    mc:Ignorable="d" 
    Title="Edit process times" 
    Name="processTimeEditor00Window" 
    Height="337.176" Width="300"> 
<Window.DataContext> 
    <viewModels:timeHandler/> 
</Window.DataContext> 
<Grid> 
    <ItemsControl Name="processTimesListPresenter" ItemsSource="{Binding processTimes, Mode=TwoWay}" Margin="20"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*"/> 
         <ColumnDefinition Width="100"/> 
         <ColumnDefinition Width="100"/> 
        </Grid.ColumnDefinitions> 
        <TextBox Text="{Binding hours, Mode=TwoWay}" Grid.Column="0"/> 
        <TextBox Text="{Binding mins, Mode=TwoWay}" Grid.Column="1"/> 
        <TextBox Text="{Binding AMPM, Mode=TwoWay}" Grid.Column="2"/> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
    <Button x:Name="addProcessTime" Content="Add process time" HorizontalAlignment="Left" VerticalAlignment="Top" Width="121" Margin="10,271,0,0" Command="{Binding btnAddNewProcessTimeCommand, Mode=OneWay}"/> 

</Grid> 

私のViewModelに(timeHandler)

public class timeHandler : INotifyPropertyChanged 
{ 
    private buttonAddNewProcessTimeCommand _btnAddNewProcessTimeCommand; 

    public List<timeItem> processTimes { get; set; } 
    public ICommand btnAddNewProcessTimeCommand 
    { 
     get 
     { 
      return _btnAddNewProcessTimeCommand; 
     } 
    } 

    public timeHandler() 
    { 
     loadProcessTimes(); 
     _btnAddNewProcessTimeCommand = new buttonAddNewProcessTimeCommand(this); 
    } 

    public void loadProcessTimes() 
    { 
     timeItem[] aux = new timeItem[] 
     { 
      new timeItem { AMPM="AM", hours=12, mins=35 }, 
      new timeItem { AMPM="PM", hours=2, mins=15 }, 
      new timeItem { AMPM="PM", hours=5, mins=15 } 
     }; 
     processTimes = aux.ToList<timeItem>(); 
    } 

    public void addProcessTime() 
    { 
     processTimes.Add(new timeItem { AMPM="AM", hours=12, mins=0 }); 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs("processTimes")); 

     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

} 

コマンド(buttonAddNewProcessTimeCommand)

class buttonAddNewProcessTimeCommand : ICommand 
{ 
    private timeHandler obj; 
    public buttonAddNewProcessTimeCommand(timeHandler _obj) 
    { 
     obj = _obj; 
    } 
    public Boolean CanExecute(object parameter) 
    { 
     return true; 
    } 
    public void Execute(object parameter) 
    { 
     obj.addProcessTime(); 
    } 
    public event EventHandler CanExecuteChanged; 
} 
である私のモデルであります

しかし、プロセスは期待どおりのことを行っていますが、ビューは更新されません。誰かが私が間違っていることを見つけるのを助けることができますか?

答えて

2

ObservableCollectionにprocessTimesを変更します。それは動作するはずです。リストは自動UI更新を行うことはできません。 ObservableCollectionについての情報

public class timeHandler : INotifyPropertyChanged 
{ 
    private buttonAddNewProcessTimeCommand _btnAddNewProcessTimeCommand; 

    public ObservableCollection<timeItem> processTimes { get; set; } 
    public ICommand btnAddNewProcessTimeCommand 
    { 
     get 
     { 
      return _btnAddNewProcessTimeCommand; 
     } 
    } 

    class buttonAddNewProcessTimeCommand : ICommand 
    { 
     private timeHandler obj; 
     public buttonAddNewProcessTimeCommand(timeHandler _obj) 
     { 
      obj = _obj; 
     } 
     public Boolean CanExecute(object parameter) 
     { 
      return true; 
     } 
     public void Execute(object parameter) 
     { 
      obj.addProcessTime(); 
     } 
     public event EventHandler CanExecuteChanged; 
    } 

    public timeHandler() 
    { 
     loadProcessTimes(); 
     _btnAddNewProcessTimeCommand = new buttonAddNewProcessTimeCommand(this); 
    } 

    public void loadProcessTimes() 
    { 
     timeItem[] aux = new timeItem[] 
     { 
     new timeItem { AMPM="AM", hours=12, mins=35 }, 
     new timeItem { AMPM="PM", hours=2, mins=15 }, 
     new timeItem { AMPM="PM", hours=5, mins=15 } 
     }; 
     processTimes = new ObservableCollection<timeItem>(aux); 
    } 

    public void addProcessTime() 
    { 
     processTimes.Add(new timeItem { AMPM = "AM", hours = 12, mins = 0 }); 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs("processTimes")); 

     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

} 
+0

Yeap、that worked !! Yesssss !! –

+0

定義は非常に明確です:ObservableCollection: "アイテムが追加、削除、またはリスト全体がリフレッシュされたときの通知を提供する動的データコレクションを表します。 感謝します。 –

関連する問題