2011-06-28 10 views
0

ItemsControl内のボタンにmyコマンドをバインドするときにエラーが発生しました。Silverlightによる相対バインディングコマンドWP7

<phone:PhoneApplicationPage.DataContext> 
    <ViewModel:MyViewModel /> 
</phone:PhoneApplicationPage.DataContext> 

と::

これは私のコードである

<ItemsControl ItemsSource="{Binding MyList}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Button Content="Test" 
       cmd:ButtonBaseExtensions.Command="{Binding MyViewModel.TestCommand}" 
       cmd:ButtonBaseExtensions.CommandParameter="{Binding}"/> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

そして私が手:もちろん

System.Windows.Data Error: BindingExpression path error: 'MyViewModel' property not found on '...' '...' (HashCode=77119633). BindingExpression: Path='MyViewModel.ChooseCommand' DataItem='...' (HashCode=77119633); target element is 'System.Windows.Controls.Button' (Name=''); target property is 'Command' (type 'System.Windows.Input.ICommand').. 

、私は絶対的な結合または相対いずれかを使用する必要があり、しかし私はそれをする方法を知らない。任意のヘルプ

答えて

1

あなたButtonを事前に

おかげでリストまたは何らかのIEnumerableタイプである私は推測していyouur MyListプロパティにバインドされてItemsControl以内です。各ButtonDataContextは、それが表すMyList内の項目になります。

トップビューモデルにボタンをバインドするのは間違いありません。Silverlight(3)がサポートしていない何らかの相対ソースバインディングが必要です。

私はここでSilverlightの相対ソース結合の置換を作成しました:

http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/

をただし、パフォーマンスが重要なWP7、のために、私はそれを使用していないだろう!

ビューモデルで必要な関係を作成するだけではどうですか?すなわちMyListの各項目(MyListItemと呼ぶ)に対して、親ビューモデルを指すプロパティを公開します。他の言葉では、を指すMyListItem.Parentの特性があります。

+0

良いアイデア、少し醜いです^^でも、うまくいきました、ありがとう – Tim

関連する問題