2010-12-02 1 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>