ComboBox里面有listview绑定

时间:2013-10-24 12:17:13

标签: wpf listview binding combobox

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

我的Listview有一个查询的datacontext,它是一个集合视图源 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>
                ....

我知道为什么它不起作用,但无法弄清楚如何让它发挥作用。

2 个答案:

答案 0 :(得分:0)

请勿更改DataContext的{​​{1}}。只需将ComboBox设置为ItemsSource尝试执行

StaticResource

或者将SelectedValue绑定更新为

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

答案 1 :(得分:0)

找到解决方案,找到了解决方法!感谢Nit推动正确的方向。 我已将组合框放在网格中并将网格Tag属性绑定到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>