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>
問題は解決しません。私はまだ最後のオブジェクトだけを削除することができ、それは一貫して機能しません。 –
私はそれが間違った変数キャプチャと関係があるかもしれないと思ったが、コードは実際にそれを行うとは思わない。あなたはいつも異なってそれをやろうとすることができます(編集を参照)。 –
@MichaelBrandonMorris:また、一貫性がないとはどういう意味ですか?クリックしても何も起こりませんか? –