2010-12-02 9 views
0

リストボックスは、Choicesというビューモデルプロパティにバインドされています。各選択肢にはラベルと索引があります。リスト内のボタンを同じビューモデル上のコマンドにバインドする必要があります。これまでのところ、アイブ氏は、このくらいの考え出し:ボタンと相対ソースのリストボックス

<ListBox Grid.Row="1" ItemsSource="{Binding Choices}" SelectedIndex="{Binding SelectedChoice, Mode=TwoWay}" > 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <Grid HorizontalAlignment="Stretch" Margin="1"> 
     <Button Content="{Binding Caption}" Height="24" HorizontalAlignment="Stretch" 
       Command="{Binding RelativeSource={RelativeSource ???}, 
            Path=SelectChoice}" CommandParameter="{Binding}"/> 
     </Grid> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

私はRelativeSourceは、コマンドで使用するかを把握カントと私はCommandParameterが正しいかはわかりません。

これは本当に簡単なことですが、私の貧しい古い脳にとっては明らかに単純すぎます。誰でも助けてくれますか?

おかげ

答えて

2

ソート:

<ItemsControl Grid.Row="1" ItemsSource="{Binding Choices}" > 
    <ItemsControl.ItemsPanel > 
    <ItemsPanelTemplate > 
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
     </StackPanel> 
    </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <Button Content="{Binding Caption}" Height="24" HorizontalAlignment="Stretch" Margin="0,0,4,0" 
       Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.SelectChoice}" 
       CommandParameter="{Binding}"/> 
    </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
関連する問題