2016-07-27 6 views
0

私は、各テキストボックスに一致するボタンを押して、テキストボックスを追加および削除するテキストボックスの動的リストボックスを作成しようとしています。私はすべての作業を行っていますが、削除は、最後の項目でのみ行われることがあります(ただし必ずしもそうではありません)。コレクションから自己を削除するバインドコマンド

私はExcludedPathクラスがある​​ている:私のViewModelクラスで

public class ExcludedPath : INotifyPropertyChanged 
{ 
    private Visibility _addButtonVisibility; 
    private string _path; 

    private ICommand _removeButtonCommand; 
    private Visibility _removeButtonVisibility; 

    public ICommand RemoveButtonCommand 
    { 
     get 
     { 
      return _removeButtonCommand; 
     } 
     set 
     { 
      _removeButtonCommand = value; 
      NotifyPropertyChanged(); 
     } 
    } 

    public Visibility AddButtonVisibility 
    { 
     get 
     { 
      return _addButtonVisibility; 
     } 
     set 
     { 
      _addButtonVisibility = value; 
      NotifyPropertyChanged(); 
     } 
    } 

    public string Path 
    { 
     get 
     { 
      return _path; 
     } 
     set 
     { 
      _path = value; 
      NotifyPropertyChanged(); 
     } 
    } 

    public ICommand AddButtonCommand { get; set; } 

    public Visibility RemoveButtonVisibility 
    { 
     get 
     { 
      return _removeButtonVisibility; 
     } 
     set 
     { 
      _removeButtonVisibility = value; 
      NotifyPropertyChanged(); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(
     [CallerMemberName] string propertyName = "") 
    { 
     PropertyChanged?.Invoke(
      this, 
      new PropertyChangedEventArgs(propertyName)); 
    } 
} 

、ここでは関連するスニペットは、次のとおりです。

internal ViewModel() 
{ 
    SetUpProperties(); 
} 

public ObservableCollection<ExcludedPath> ExcludedPaths { get; 
    private set; } 

private void AddExcludedPathExecute() 
{ 
    ExcludedPaths.Add(new ExcludedPath()); 
} 

private bool CanAddExcludedPath() 
{ 
    return ExcludedPaths.All(x => !x.Path.IsNullOrWhiteSpace()); 
} 

private bool CanRemoveExcludedPath() 
{ 
    return ExcludedPaths.Count > 1; 
} 

private void OnExcludedPathsChanged(
    object sender, NotifyCollectionChangedEventArgs e) 
{ 
    var addButtonCommand = new RelayCommand(
     AddExcludedPathExecute, CanAddExcludedPath); 

    if (e.Action == NotifyCollectionChangedAction.Add) 
    { 
     for (var i = 0; i < ExcludedPaths.Count - 1; i++) 
     { 
      ExcludedPaths[i].AddButtonVisibility = Visibility.Hidden; 
     } 
     var excludedPath = ExcludedPaths.Last(); 
     excludedPath.AddButtonCommand = addButtonCommand; 
     excludedPath.AddButtonVisibility = Visibility.Visible; 
     excludedPath.RemoveButtonCommand = new RelayCommand(
      () => RemoveExcludedPath(excludedPath), 
      CanRemoveExcludedPath); 
    } 
    else if (e.Action == NotifyCollectionChangedAction.Remove) 
    { 
     ExcludedPaths.Last().AddButtonVisibility = Visibility.Visible; 
    } 
} 

private void RemoveExcludedPath(ExcludedPath excludedPath) 
{ 
    ExcludedPaths.Remove(excludedPath); 
} 

private void SetUpProperties() 
{ 
    ExcludedPaths = new ObservableCollection<ExcludedPath>(); 
    ExcludedPaths.CollectionChanged += OnExcludedPathsChanged; 
    ExcludedPaths.Add(new ExcludedPath()); 
} 

そして、私のView

<ListBox ItemsSource="{Binding ExcludedPaths}"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <ContentPresenter/> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <DockPanel LastChildFill="True"> 
       <Button DockPanel.Dock="Right" 
        Content ="+" 
        Command="{Binding AddButtonCommand}" 
        Visibility="{Binding AddButtonVisibility}"/> 
       <Button DockPanel.Dock="Right" 
        Content="-" 
        Command="{Binding RemoveButtonCommand}" 
        Visibility="{Binding RemoveButtonVisibility}"/> 
       <TextBox DockPanel.Dock="Left" 
         Text ="{Binding Path, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
         HorizontalAlignment="Stretch" 
         HorizontalContentAlignment="Stretch"/> 
      </DockPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

答えて

3

コレクションchを扱うときあなたは(var excludedPath = ExcludedPaths.Last();)のように最後のアイテムを取得するだけではなく、e.NewItemsを繰り返し処理する必要があります。あなたのアプローチは動作するはずですけれども

は(同様に除去にあなたがe.OldItemsを反復する必要があります。)


は個人的に私は、リストを含むオブジェクトで削除を処理します。これを行うには、あなたがこのようにそれを結合する私の方法:削除する

<Button Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource AncestorType=ListBox}}" 
     CommandParameter="{Binding}"/> 

項目は、コマンドパラメータとして渡されます。

+0

問題は解決しません。私はまだ最後のオブジェクトだけを削除することができ、それは一貫して機能しません。 –

+0

私はそれが間違った変数キャプチャと関係があるかもしれないと思ったが、コードは実際にそれを行うとは思わない。あなたはいつも異なってそれをやろうとすることができます(編集を参照)。 –

+0

@MichaelBrandonMorris:また、一貫性がないとはどういう意味ですか?クリックしても何も起こりませんか? –

関連する問題