按钮和相对源列表框 [英] Listbox of buttons and relative source

查看:78
本文介绍了按钮和相对源列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必然要叫选择一个视图模型属性列表框。每个选项都有一个标签和索引。我需要在列表中的按钮绑定到一个命令在同一视图模型。到目前为止,香港专业教育学院想出了这么多的:

I have a listbox bound to a view model property called Choices. Each choice has a label and an index. I need to bind the buttons in the list to a command on the same view model. So far Ive figured out this much:

<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是正确的。

I cant figure out what RelativeSource to use for the command and I'm not sure the CommandParameter is correct.

这似乎是一个很简单的事,但它显然对我可怜的老脑子太简单了。任何人都可以帮忙吗?

This seems a really simple thing to do but it's obviously too simple for my poor old brain. Can anyone help please?

感谢

推荐答案

排序:

    <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>

这篇关于按钮和相对源列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆