1

この主題に関するチュートリアルと投稿のロジックに頭を悩ませているのは、非常に苦労しています。私が書いているwpfアプリケーションに実装しようとしています。

基本的に、私はリストボックスを使用してリスト内のオブジェクトの ToString を表示し、ユーザーが追加および削除ボタンを介してそのリストとそれに対応するリストボックスに追加および削除できるようにしています。私が抱えている問題は、削除ボタンの実装にあります。リストボックスの項目が選択されていない場合はボタンを無効にしたいのですが、これはこのパターンが適していることの 1 つです。その条件を実装する方法がわかりません。

現時点では、リストボックスの項目をハイライトしてもボタンが有効になりません。CanExecuteChanged イベントが発生していないと思います..これを変更するにはどうすればよいですか?

私の CommandsHandler クラス:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace TechopsTools
{

    public class CommandHandler : ICommand
    {
        private Action<object> _execute;
        private bool _canExecute;

        public CommandHandler(Action<object> execute)
            : this(execute, true)
        {
        }

        public CommandHandler(Action<object> execute, bool canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }


        public bool CanExecute(object parameter)
        {
            return _canExecute;
        }

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

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

}

私のビューモデル:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using ProtoBuf;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace TechopsTools
{
    class LogCheckClientViewModel : INotifyPropertyChanged
    {
        private string uri;
        private string response;
        private bool _canRemove;
        private LogConstraints selectedConstraints;

        private ObservableCollection<LogConstraints> constraints;

        public event PropertyChangedEventHandler PropertyChanged;

        public LogConstraints SelectedConstraints
        {
            get
            {
                return selectedConstraints;
            }
            set
            {
                selectedConstraints = value;
                OnPropertyChanged("SelectedConstraints");
            }
        }

        private CommandHandler removeItemCommand;
        public ICommand RemoveItemCommand
        {
            get
            {
                if (removeItemCommand == null)
                    removeItemCommand = new CommandHandler(param => RemoveConstraint(), SelectedConstraints != null);
                return removeItemCommand;
            }
        }

        public string Response 
        {
            get
            {
                return response;
            }
            set
            {
                response = value;
                OnPropertyChanged("Response");
            }
        }

        public string Uri
        {
            get
            {
                return uri;
            }
            set
            {
                uri = value;
                OnPropertyChanged("Uri");
            }
        }

        public ObservableCollection<LogConstraints> Constraints
        {
            get
            {
                return constraints;
            }
            set
            {
                constraints = value;
                OnPropertyChanged("Constraints");
            }
        }

        public LogCheckClientViewModel()
        {
            constraints = new ObservableCollection<LogConstraints>();
        }

        public void AddConstraint()
        {
            NewConstraint newConstraint = new NewConstraint();
            newConstraint.ShowDialog();
            if (newConstraint._vm.constraint != null)
            {
                constraints.Add(newConstraint._vm.constraint);
            }
        }

        private void RemoveConstraint()
        {
            Constraints.Remove(SelectedConstraints);
            OnPropertyChanged("Constraints");
        }

xaml:

<Window x:Class="TechopsTools.LogCheckClient"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TechopsTools"
        Title="LogCheck" Height="453.057" Width="495.986">
    <Grid>
        <TextBox Text="{Binding Response}" HorizontalAlignment="Left" Height="128" Margin="38,212,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="413" VerticalScrollBarVisibility="Auto" IsEnabled="False"/>
        <Label Content="Response" HorizontalAlignment="Left" Margin="38,188,0,0" VerticalAlignment="Top" Width="78" Height="24"/>
        <TextBox Text="{Binding Uri}" HorizontalAlignment="Left" Height="23" Margin="38,26,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="413"/>
        <Label Content="Uri" HorizontalAlignment="Left" Margin="38,0,0,0" VerticalAlignment="Top" Width="78" Height="24"/>
        <Button Content="Add Constraint" HorizontalAlignment="Left" Margin="38,54,0,0" VerticalAlignment="Top" Width="127" Height="56" Click="Add_Click"/>
        <Button x:Name="Submit" Content="Submit Request" HorizontalAlignment="Left" Margin="38,345,0,0" VerticalAlignment="Top" Width="413" Height="70" Click="Submit_Click"/>
        <ListBox SelectedItem="{Binding Path=SelectedConstraints,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Constraints}" HorizontalAlignment="Left" Height="124" Margin="182,54,0,0" VerticalAlignment="Top" Width="269">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=description}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Command="{Binding RemoveItemCommand}" Content="Remove Constraint" HorizontalAlignment="Left" Margin="38,122,0,0" VerticalAlignment="Top" Width="127" Height="56" />
    </Grid>
</Window>
4

2 に答える 2

4

ハンドラーCanExecuteと同じようにデリゲートを使用する必要があります。Execute

RemoveItemCommand基本的には、最初にアクセスしたときに実行できるかどうかを確認しています。しかし、その後はずっとその値を保持します。

同じ条件でデリゲートを渡す場合 (おそらく null リストだけでなく、空のリストを追加する場合)、うまくいくと思います。

つまり、CommandHandler で、次のように変更します。

private bool _canExecute;

private Func<bool,object> _canExecute;

変更する

public bool CanExecute(object parameter)
{
    return _canExecute;
}

public bool CanExecute(object parameter)
{
    return _canExecute(parameter);
}

次に、ViewModelで変更します

removeItemCommand = new CommandHandler(param => RemoveConstraint(), 
                                       SelectedConstraints != null);

removeItemcommand = new CommandHandler(param => RemoveConstraint(), 
                                       param => SelectedConstraints != null);

(フリーハンドで書いているだけなので、これは正確なコードではないかもしれないことに注意してください。しかし、うまくいけば要点がわかります)

于 2013-07-17T19:12:50.783 に答える