组合框的列表框并将它们绑定到WPF

时间:2010-05-13 11:16:25

标签: c# wpf wpf-controls

我有一个情况,我有一个组合框的列表框,主要是它绑定到一个桥实体,所以该对象包含外键。我需要做的是,我需要将组合的显示绑定到相应的实体,并将它们的值成员绑定到我将列表框绑定到的桥实体中的外键值。

我现在的代码是:

<ListBox Name="lstServices" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch">
                     <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="2" DataContext="{Binding ElementName=wndMain,Path=DataContext}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>

                                <ComboBox Name="cmbService" SelectedIndex="0" DisplayMemberPath="Name"  SelectedValuePath="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=FK_ServiceID}" ItemsSource="{Binding Path=AllServices}" Grid.Column="0"></ComboBox>
                                <ComboBox Name="cmbService_Role" Margin="2,0,0,0" SelectedValuePath="{Binding Path=FK_ServiceRoleID}" DisplayMemberPath="Name" ItemsSource="{Binding Path=AllService_Roles}" Grid.Column="1"></ComboBox>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

我可以设法显示我需要的值,但是由于List Item上下文发生了变化,我无法访问listbox itemSource。

感谢任何帮助 Bishoy

1 个答案:

答案 0 :(得分:2)

我明白了:D,这是应该怎么做

<ListBox Name="lstServices" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch" ItemsSource="{Binding MemberServices}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <ComboBox Name="cmbService" SelectedValue="{Binding FK_ServiceID,Mode=TwoWay}" ItemsSource="{Binding ElementName=wndMain, Path=DataContext.AllServices,Mode=OneTime}"  SelectedValuePath="ID" DisplayMemberPath="Name"  Grid.Column="0" ></ComboBox>
                                <ComboBox Name="cmbService_Role" SelectedValue="{Binding FK_ServiceRoleID,Mode=TwoWay}" ItemsSource="{Binding ElementName=wndMain, Path=DataContext.AllService_Roles,Mode=OneTime}"  SelectedValuePath="ID" DisplayMemberPath="Name"  Grid.Column="1" Margin="2,0,0,0"></ComboBox>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>