0

我在将组合框的选定值绑定回其列表视图列值时遇到问题。

我的 Listview 有一个查询的数据上下文,它是一个集合视图源 ComboBox 作为 Accounts 的数据上下文,它也是一个集合视图源

两者都通过将 itemssource 设置为 {Binding} 来正确显示列表

我需要做的是将 Combobox selectedvalue 绑定到它所在的 Inquiries 行。

这是帐户列 XAML:

<GridViewColumn x:Name="AccountIdColumn" Header="Account" Width="80">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Margin="-6,-1" DataContext="{StaticResource AccountViewSource}" 
                                      ItemsSource="{Binding}"
                                      x:Name="AccountComboBox"
                                      SelectedValuePath="ID" 
                                      DisplayMemberPath="Description" 
                                      Height="Auto" 
                                      SelectedValue="{Binding AccountID, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
       </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

Listview 设置如下:

<ListView DataContext="{StaticResource tbl_InquiriesViewSource}" x:Name="Tbl_InquiriesListView" ItemsSource="{Binding Mode=OneWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" Margin="10,47,10,10" SelectionMode="Single" SourceUpdated="Tbl_InquiriesListView_SourceUpdated" TargetUpdated="Tbl_InquiriesListView_TargetUpdated" >
        <ListView.ItemContainerStyle>
            <Style>
                <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                ....

我知道它为什么不工作,但不知道如何让它工作。

4

2 回答 2

0

不要更改DataContext. ComboBox只需设置ItemsSourceStaticResource尝试做

           <ComboBox Margin="-6,-1"              
                                  ItemsSource="{Binding Source={StaticResource AccountViewSource}"
                                  x:Name="AccountComboBox"
                                  SelectedValuePath="ID" 
                                  DisplayMemberPath="Description" 
                                  Height="Auto" 

或将 SelectedValue 绑定更新为

 SelectedValue="{Binding DataContext.AccountID, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}} Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
于 2013-10-24T12:37:57.690 回答
0

找到了解决方案,很好地找到了解决方法!感谢 Nit 朝着正确的方向前进。我已将组合框放在网格中并将网格标记属性绑定到 AccountId 然后使用相对绑定来获取/设置标记

<Grid Tag="{Binding AccountId, Mode=TwoWay}">
    <ComboBox DataContext="{StaticResource AccountViewSource}" 
              ItemsSource="{Binding}" 
              DisplayMemberPath="Description"
              SelectedValuePath="ID"
              SelectedValue="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=Tag, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
于 2013-10-24T12:53:55.420 回答