I have a task to create a clickable element inside a ListView, which is created by Bindings. I looked at the guides, used RelayCommand, it did not work. Please tell me the solution to the problem, why it does not work.
RelayCommand
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute;
}
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute?.Invoke(parameter) ?? true;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public void Execute(object parameter) { _execute(parameter); }
}
ViewModel
public class HomeViewModel : BaseViewModel
{
private readonly CoinCapHandler _coinCapHandler;
public List<Coin> Coins { get; set; } = new List<Coin>();
public List<Coin> AllCoins { get; set; } = new List<Coin>();
public RelayCommand IsCheckedCommand { get; set; }
public ICommand OpenWindowCommand { get; }
private string? _searchText = "";
public string SearchText
{
get => _searchText;
set
{
if (value != null)
{
_searchText = value.ToString();
ActiveFilter();
OnPropertyChanged(nameof(SearchText));
}
}
}
private string _selectedFilter = "By Name";
public string SelectedFilter
{
get => _selectedFilter;
set
{
_selectedFilter = value.ToString();
ActiveFilter();
OnPropertyChanged(nameof(SelectedFilter));
}
}
public HomeViewModel()
{
_coinCapHandler = new CoinCapHandler();
IsCheckedCommand = new RelayCommand(m => IsCheckedCommandExecute());
LoadCoinsAsync();
//OpenWindowCommand = new RelayCommand(OpenWindow);
}
private void IsCheckedCommandExecute()
{
//OnPropertyChanged(nameof());
}
private async void LoadCoinsAsync()
{
AllCoins = await _coinCapHandler.GetAllCoinsAsync();
Coins = await _coinCapHandler.GetAllCoinsAsync();
OnPropertyChanged(nameof(Coins));
}
private async void ActiveFilter()
{
if (_selectedFilter == "System.Windows.Controls.ComboBoxItem: By Name")
{
Coins = new(AllCoins.Where(coin =>
coin.Name.Contains(_searchText)));
}
OnPropertyChanged(nameof(Coins));
}
}
XAML
<Grid Margin="30 10 30 20" Grid.Row="2">
<ListView ItemsSource="{Binding Coins}">
<ListView.ItemTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding}" >
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftClick" Command="{Binding IsCheckedCommandExecute}" />
</TextBlock.InputBindings>
</TextBlock>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.View>
<GridView >
<GridViewColumn Header="Rank">
<GridViewColumn.DisplayMemberBinding>
<Binding Path="Rank" StringFormat="{}{0:D}"/>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
<GridViewColumn Header="Name">
<GridViewColumn.DisplayMemberBinding>
<Binding Path="Name"/>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
<GridViewColumn Header="Symbol">
<GridViewColumn.DisplayMemberBinding>
<Binding Path="Symbol"/>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
<GridViewColumn Header="Price">
<GridViewColumn.DisplayMemberBinding>
<Binding Path="Price" StringFormat="{}{0:C}"/>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
<GridViewColumn Header="Change">
<GridViewColumn.DisplayMemberBinding>
<Binding Path="ChangePercent" StringFormat="{}{0:P}"/>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
<GridViewColumn Header="Market Cap">
<GridViewColumn.DisplayMemberBinding>
<Binding Path="MarketCapUsd" StringFormat="{}{0:N}"/>
</GridViewColumn.DisplayMemberBinding>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
I try to use RelayCommand class from guides. But when the element is clicked, the method is not executed.