【问题标题】:Catch selected value from dynamic comboboxes inside itemscontrol in Silverlight从 Silverlight 中的 itemscontrol 内的动态组合框中捕获选定的值
【发布时间】:2012-10-19 06:37:32
【问题描述】:

我在尝试从我在项目控件中创建的一组动态组合框中获取所选值时遇到了这个问题,为了更好地解释这里是我的 xaml 代码。

<ItemsControl Name="ItemsControlP"
                      Width="120"
                      Margin="5"
                      ItemsSource="{Binding ElementName=personWindow, Path=DataContext.City}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ComboBox Height="20"
                              DisplayMemberPath="Name"
                              IsSynchronizedWithCurrentItem="True"
                              ItemsSource="{Binding ElementName=personWindow, Path=DataContext.Person}"
                              SelectedIndex="0"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

City 和 people 是 ObservableCollection 类型,因此对于每个城市,它显示一个组合框,每个组合框都有一个人员列表,然后我需要在每个组合框中捕获选定的值。有什么办法吗?

【问题讨论】:

    标签: silverlight binding combobox itemscontrol selectedvalue


    【解决方案1】:

    您好,您可以像这样获取组合框的选定项,

        if (combobox.SelectedItem != null)
            {
                var x = (Person)combobox.SelectedItem;
            }
    

    然后您可以像这样以 x.name 的形式访问对象的实体。希望这对您有所帮助。

    【讨论】:

    • 嗨,谢谢你的帮助,但我不能这样做,我通过将 selecteditem 组合框属性绑定到一个视图模型属性来结束捕获,我通过代码进行绑定,以便我可以索引每个组合框而且效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多