簡體   English   中英

將CommandParameter設置為ListBox(WP7)上當前點擊的項目

[英]Set CommandParameter to currently tapped item on a ListBox (WP7)

我想將命令參數設置為ListBox上當前選定的項目。

XAML:

<!--<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}">-->
    <ListBox ItemsSource="{Binding Places}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Tap">
                <i:InvokeCommandAction Command="{Binding ListBoxClick}" CommandParameter="{Binding}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListBox.ItemTemplate>
            <DataTemplate>
                (...)
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

C# (公開ListBoxClick命令的ViewModel代碼的一部分)

public RelayCommand ListBoxClick { get; set; }

ListBoxClick = new RelayCommand((o) => {
    //model is always null
    var model = o as BasicModel;

    SelectedPlace = model;
});

我添加了適當的引用和名稱空間:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

問題在於,在由RelayCommand對象調用的RelayCommando參數始終為null。

UPDATE

SelectedPlace屬性的C#代碼

public BasicModel SelectedPlace {
            get {
                return _selectedPlace;
            }
            set {
                _selectedPlace = value;
                RaisePropertyChanged("SelectedPlace");
            }
        }

當我使用這個:

<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}">

如果我第一次單擊ListBoxItem,一切正常,但是當我單擊選定的ListBoxItem時,什么也沒有發生,因為選擇不會更改。 在這兩種情況下,我都必須能夠檢測項目單擊。

嘗試這個:

<ListBox ItemsSource="{Binding Places}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <i:InvokeCommandAction Command="{Binding ListBoxClick}" 
CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={
x:Type ListBox}}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListBox.ItemTemplate>
        <DataTemplate>
            (...)
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果這不起作用,請嘗試將Binding更改為此:

CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Self}}"

更新>>>

抱歉,我沒有看到您使用的是Windows Phone7。或者,嘗試將屬性添加到綁定到ListBox.SelectedItem屬性的后台代碼/視圖模型中:

<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedItem}" ... />

然后,您應該可以執行以下操作:

ListBoxClick = new RelayCommand(() => {
    SelectedPlace = SelectedItem;
});

更新2 >>>

我不知道Windows Phone 7是否支持Binding.ElementName屬性,但是如果支持,請嘗試以下操作:

<ListBox Name="ListBox" ItemsSource="{Binding Places}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <i:InvokeCommandAction Command="{Binding ListBoxClick}" 
CommandParameter="{Binding SelectedItem, ElementNameListBox}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListBox.ItemTemplate>
        <DataTemplate>
            (...)
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我想出了一種實現目標的丑陋方法。

XAML

<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}">

C# (ViewModel)

private bool _placeSelected;

public BasicModel SelectedPlace {
    get {
        return _selectedPlace;
    }
    set {
        _placeSelected = true;
        _selectedPlace = value;
        RaisePropertyChanged("SelectedPlace");
    }
}

ListBoxClick = new RelayCommand((o) => {
    if (!_placeSelected) {
        SelectedPlace = _selectedPlace;
    }
    else {
        _placeSelected = false;
    }
});

這樣RaisePropertyChanged("SelectedPlace"); 在兩種情況下都會被調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM