0

こんにちは、ボタンを他のlistView.Itemにバインドしたいです。私が欲しいのは、私たちがスタックオーバーフローに持っているようなものを持つことです。しかし、値の増減に問題があります。イベントクリックがありますが、対応するアイテムをリストに取得して値を増減する方法がわかりません。 リストビュー。 コンセプトモデル

<DataTemplate>
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <Label Width="706" Height="75" Content="{Binding feedback}"/>
            <StackPanel Orientation="Vertical">
                <Button Name="buttonUp" Content="^" Command="{Binding upVoteCommand}" />
                <Label HorizontalContentAlignment="Center" Width="50" Content="{Binding grade}"/>
                <Button Name="buttonDown" Content="v" Command="{Binding upVoteCommand}"/>
            </StackPanel>
        </StackPanel>
        <Label>-</Label>
    </StackPanel >

編集

class A {
    public string feedback {
        get;
        set;
    }
    public int grade {
        get;
        set;
    }

    private ICommand _upVoteCommand;
    private ICommand _downVoteCommand;
    public ICommand upVoteCommand {
        get {
            return _upVoteCommand;
        }
        set {
            _upVoteCommand = value;
        }
    }
    public ICommand downVoteCommand {
        get {
            return _downVoteCommand;
        }
        set {
            _downVoteCommand = value;
        }
    }
}

編集私はこのbutton.Commandを使用しましたが、それでも機能しません。このコマンドで何をすべきかわかりませんでした。

4

2 に答える 2

1

RoutedEvents はDataTemplates、イベント コードを配置できるコード ビハインドがないため、 では簡単には機能しません。それを行う方法はありますが、コマンドを使用して同じことを行うことができます。各項目のビュー モデル (MVVM を使用していると仮定します) で、タイプICommandのUpVoteCommand および DownVoteCommand と呼ばれるプロパティを作成しますそれらをCommandプロパティにバインドし、DataTemplate の Click ハンドラーを削除します。

[編集]

リスト内の 1 つのエントリに対して可能な Viewmodel の小さな例で、賛成票または反対票を投じることができます。

class MyEntryViewModel : INotifyPropertyChanged
{
    public MyEntryViewModel()
    {
        UpVoteCommand = new DelegateCommand(OnUpVoteCommand);
    }
    public int Votes 
    {
        get {return mVotes;}
        set {mVotes = value; RaiseProperty("Votes");}
    }

    public ICommand UpVoteCommand 
    {
        get; private set;
    }

    void OnUpVoteCommand(object aParameter)
    {
        Votes++;
    }
}

簡単にするために、INotifyPropertyChanged と反対票コマンドの実装を残しました。

于 2013-07-03T12:07:52.360 に答える