2016-09-09 9 views
0

文字列(ToAddToLBFilterStrings)が設定されたListBox(secondListBox)があります。ユーザーがListBoxItemをクリックすると、それをListBoxから削除します。これが私がこれをやろうとしているところです。代わりに、ただ一つの項目を除去する選択時にListBoxItemを削除する

private void OnAddToFilterLBSelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var _selectedString = secondListBox.SelectedItem as string; 
    if (!string.IsNullOrEmpty(_selectedString)) 
    { 
     if (_selectedString == "Current") 
     { 
      currentItem.Visibility = Visibility.Visible; 
     } 
     if (_selectedString == "Subcontractors") 
     { 
      subbieItem.Visibility = Visibility.Visible; 
     } 
     if (_selectedString == "Suppliers") 
     { 
      suppliersItem.Visibility = Visibility.Visible; 
     } 
     if (_selectedString == "Plant Hire") 
     { 
      plantHireItem.Visibility = Visibility.Visible; 
     } 
     if (_selectedString == "Architects") 
     { 
      architectsItem.Visibility = Visibility.Visible; 
     } 
     if (_selectedString == "QS") 
     { 
      qsItem.Visibility = Visibility.Visible; 
     } 
     if (_selectedString == "Project Managers") 
     { 
      projectManagerItem.Visibility = Visibility.Visible; 
     } 
     if (_selectedString == "Structural Engineers") 
     { 
      structEngItem.Visibility = Visibility.Visible; 
     } 
     if (_selectedString == "Service Engineers") 
     { 
      servEngItem.Visibility = Visibility.Visible; 
     } 

     ToAddToLBFilterStrings.Remove(_selectedString); 
     secondListBox.Items.Refresh(); 
    } 
} 

が、これは時々すべてのアイテム、アイテムの時々ランダムにグループを削除し、私がそれまでの期待どのように機能していません。

+0

if-else-if文で1つのif文を置き換えようとしましたか? –

+0

@UmairFarooq残念ながら違いはありません:( – CBreeze

答えて

1

まず、私はあなたがWPFを使用しているので、あなたはMVVMモデルとデータ連結を使用する必要があるため、あなたは、完全に間違ったパス上にあることを始めたいです。 1つのことだけを行うサンプルアプリケーションを作成しました。「クリックしたアイテムをリストボックスから削除する」。

<Window x:Class="Test.MainWindow" 
     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:Test" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525" 
     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
     > 
    <Window.DataContext> 
     <local:MainViewModel /> 
    </Window.DataContext> 

    <Grid> 
     <ListBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName}"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="SelectionChanged"> 
        <i:InvokeCommandAction Command="{Binding ListItemClickCommand}" /> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </ListBox> 
    </Grid> 
</Window> 

のViewModel:

public class MainViewModel : BaseViewModel 
{ 
    public ObservableCollection<string> Names { get; set; } = new ObservableCollection<string>() {"A", "B", "C"}; 

    #region SelectedName 

    private string selectedName; 

    public string SelectedName 
    { 
     get 
     { 
      return selectedName; 
     } 
     set 
     { 
      if (value != selectedName) 
      { 
       selectedName = value; 
       NotifyPropertyChanged(); 
      } 
     } 
    } 

    #endregion 

    #region ListItemClickCommand 

    private ICommand listItemClickCommand; 

    public ICommand ListItemClickCommand 
    { 
     get 
     { 
      if (listItemClickCommand == null) 
      { 
       listItemClickCommand = new RelayCommand(OnListItemClick); 
      } 

      return listItemClickCommand; 
     } 
    } 

    void OnListItemClick(object param) 
    { 
     Names.Remove(SelectedName); 
    } 

    #endregion 
} 

BaseViewModel:

public abstract class BaseViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

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

RelayCommandクラス:

public class RelayCommand : ICommand 
{ 
    #region Fields 

    readonly Action<object> _execute; 
    readonly Predicate<object> _canExecute; 

    #endregion // Fields 

    #region Constructors 

    /// <summary> 
    /// Creates a new command that can always execute. 
    /// </summary> 
    /// <param name="execute">The execution logic.</param> 
    public RelayCommand(Action<object> execute) 
     : this(execute, null) 
    { 
    } 

    /// <summary> 
    /// Creates a new command. 
    /// </summary> 
    /// <param name="execute">The execution logic.</param> 
    /// <param name="canExecute">The execution status logic.</param> 
    public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
    { 
     if (execute == null) 
      throw new ArgumentNullException("execute"); 

     _execute = execute; 
     _canExecute = canExecute; 
    } 

    #endregion // Constructors 

    #region ICommand Members 

    [DebuggerStepThrough] 
    public bool CanExecute(object parameter) 
    { 
     return _canExecute == null ? true : _canExecute(parameter); 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    public void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

    #endregion // ICommand Members 
} 
そして、それは

見る...このようになります

ご覧のとおり、コードビハインドコードはなく、ビューとビューモデルは完全に分離されています。

サイドノート:Blendインタラクションを使用するには、参照に 'System.Windows.Interactivity'を追加する必要があります。

これだけです。そして、あなたのケースでは、可視性の変化のために。あなたはブーリアンをビジビリティにバインドできるように同じパターンを再び使うことができます(コンバータは必要です)

+1

答えは良いですが、WPFを使用しているので、MVVM-ModelとDataBindingsを使用する必要があるため、優れた機能はありますが、データバインディングを使用しなくても使用できます。メンテナンス性とスケーラビリティを向上させるために使用する必要がありますが、WPFアプリケーションはMVVMなしでも存続できます。 。 –

-1

ListではなくObservable Collectionクラスを使用してください。プロパティ/ ListItemが変更されたことを通知するための機能が組み込まれています。したがって、リストを更新する必要はありません。観察可能なコレクションは自動的にリフレッシュされ、UIは即座に更新されます。

複数の「if」ステートメントを使用するのではなく、「elseIf」を使用します。

if文を入力するときにリストから項目を削除します。

例:すべての

declaration: 
// declare the List like this.. 

    ObservableCollection<string> ToAddToLBFilterStrings = new ObservableCollection<string>(); 


    if (_selectedString == "Project Managers") 
      { 
       projectManagerItem.Visibility = Visibility.Visible; 

      } 
      elseif (_selectedString == "Structural Engineers") 
      { 
       structEngItem.Visibility = Visibility.Visible; 
      } 
      elseif (_selectedString == "Service Engineers") 
      { 
       servEngItem.Visibility = Visibility.Visible; 
      } 

     // Remove your Item here. And the List will be refreshed Automatically 

       ToAddToLBFilterStrings.Remove(_selectedString); 
関連する問題