2014-03-06 153 views
1

在我的ViewModel中,我擁有包含Observable Collection of Cards的基類和Deck類。下面是它是如何在XAML綁定通過MVVM命令訪問集合

 <GridView ItemsSource="{Binding DeckCollection}" IsItemClickEnabled="True" Grid.Row="0"> 
      <GridView.ItemTemplate> 
       <DataTemplate> 
        <Button Command="{Binding Path=??}" 
          CommandParameter=?? 
         <Button.Content> 
          <Grid> 
           <Image 
            Source="{Binding ImagePath}" 
            Stretch="None"/> 
          </Grid> 
         </Button.Content> 
        </Button> 
       </DataTemplate> 
      </GridView.ItemTemplate> 
     </GridView> 

這裏是我的班

class Deck 
{ 
    private ObservableCollection<Card> _deckCollection = new ObservableCollection<Card>(); 
    public ObservableCollection<Card> DeckCollection 
    { 
     get { return _deckCollection; } 
     set { _deckCollection = value; } 
    } 

    public Deck() 
    { 
     ActionCommand = new MyCommand(); 
     ActionCommand.CanExecuteFunc = obj => true; 
     ActionCommand.ExecuteFunc = AddToList; 
    } 


    public void AddToList(object parameter) 
    { 
     var clickedCard = this; 
     //add Card to list which in this case is not possible 
     //DeckCollection.Add(this) ? 
    } 
} 

class Card 
{ 
    public String Name { get; set; } 
    public int Cost { get; set; } 
    public String ImagePath { get; set; } 
    public MyCommand ActionCommand { get; set; } 
} 

而且還mycommand的類

public class MyCommand : ICommand 
{ 

    public Predicate<object> CanExecuteFunc { get; set; } 
    public Action<object> ExecuteFunc { get; set; } 

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

    public event EventHandler CanExecuteChanged; 
    public void Execute(object parameter) 
    { 
     ExecuteFunc(parameter); 
    } 
} 

我做了修改建議,但現在ActionCommand不中可見集合,因爲只有屬於Card類的屬性才能綁定。

編輯:我改變了我的XAML文件下卻得到了一些錯誤

<Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Deck}, Path=ActionCommand}}"> 

屬性「AncestorType」型「的RelativeSource」沒有被發現。

在'RelativeSource'類型中未找到'Path'屬性。

成員「AncestorType」未被識別或無法訪問。

成員「路徑」未被識別或無法訪問。

未知的成員元素「AncestorType「的RelativeSource」

未知的成員「路徑」元素「的RelativeSource」

請幫

+0

XAML的視圖模型在哪裏? CardCollection是在哪裏定義的?從概念上講,如果你有一個卡類,並且正在調用一個「添加到列表」的方法,我希望你將它傳遞給一個列表,否則該卡怎麼知道要添加到什麼地方?更合理的做法是在名單上添加一個名爲「添加卡」的方法,通過卡片。儘管這兩種方法都需要通過視圖模型進行管理,視圖模型正在協調這些操作。 – Mashton

+0

我在代碼中做了很多更改,現在我看到我離開了CardCollection,而不是將其更改爲DeckCollection。所以基本上我的Deck類是我的ViewModel,它直接綁定爲GridView中的ItemSource – user2847238

+2

好了。那麼讓命令在你的viewmodel(deck)上作爲一個命令並且作爲命令的一個參數指定這個卡(或者在VM上有一個SelectedCard屬性)。然後以這種方式添加到您的列表 - 從卡組中使用卡,而不是其他方式。 – Mashton

回答

0

如果你想有按鈕,增加了新的項目,你的收集,我認爲這樣的東西可能是解決方案。

在XAML:

<GridView ItemsSource="{Binding DeckCollection}" IsItemClickEnabled="True" Grid.Row="0"> 
     <GridView.ItemTemplate> 
      <DataTemplate> 
       <Button> 
        <Button.Content> 
         <Grid> 
          <Image Source="{Binding ImagePath}" 
           Stretch="None"/> 
         </Grid> 
        </Button.Content> 
       </Button> 
      </DataTemplate> 
     </GridView.ItemTemplate> 
    </GridView> 
<!-- public property located in Deck class --> 
<Button Command="{Binding AddItemCommand}" Content="Add Item"/> 

在C#:

class Deck, INotifyPropertyChanged /*custom implementation depends on .NET version, in my case its .NET3.5*/ 
    { 
    private ObservableCollection<Card> _deckCollection = new ObservableCollection<Card>(); 
    public ObservableCollection<Card> DeckCollection 
    { 
     get { return _deckCollection; } 
     set { _deckCollection = value; 
       OnPropertyChanged(() => DeckCollection); } 
    } 

    // your Add command 
    public ICommand AddItemCommand { get { return new MyCommand(AddToList); } } 
    private void AddToList(object parameter) 
    { 
     DeckCollection.Add(new Card()); 
    } 

    public Deck() { } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged<T>(Expression<Func<T>> expression) 
    { 
     if (PropertyChanged == null) return; 

     var body = (MemberExpression)expression.Body; 
     if (body != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(body.Member.Name)); 
    } 
} 

在這種情況下,主要的事情是,你不能有集裏面添加按鈕。