まず、私はあなたが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'を追加する必要があります。
これだけです。そして、あなたのケースでは、可視性の変化のために。あなたはブーリアンをビジビリティにバインドできるように同じパターンを再び使うことができます(コンバータは必要です)
if-else-if文で1つのif文を置き換えようとしましたか? –
@UmairFarooq残念ながら違いはありません:( – CBreeze