WPF 4.0 AutoCompleteBox TwoWay绑定

时间:2011-05-18 12:52:19

标签: visual-studio-2010 entity-framework-4 autocomplete wpf-controls binding

我有一个WPF窗口,它有几个Listbox和DataGrid控件。主列表框显示承包商列表。地址数据网格绑定到承包商列表框并显示当前所选承包商的地址。

我希望此窗口(WPF)的用户能够使用本机Datagrid添加功能将地址添加到选定的承包商。我还希望用户能够通过单击datagrid单元格并将单元格作为AutoCompleteBox来编辑关联的地址记录。

我已在数据网格控件的State列中部分成功实现了AutoCompleteBox。目前,当前Contractor的正确Text值显示在Address datagrid的State列中。如果我编辑状态单元格,我得到预期的自动完成功能,我可以选择一个新状态。

什么是无效的是TwoWay绑定。目前,如果我更改当前的地址状态,它不会保存回实体模型或数据库。这并不奇怪,因为我知道我没有在这个控件上正确实现Twoway绑定。

我已经使用组合框成功实现了Twoway绑定,但组合框显然无法提供我需要的自动完成功能。

更新

我们昨天晚些时候进行了这项工作并得出了部分解决方案。现在我们有了一个新问题。

使用ComboBox,您可以:    DisplayMemberPath =“StateAbbrv”    SelectedValue =“{Binding Path = tbl_Address.StateID,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}”和
   SelectedValuePath = “STATEID”。

使用AutoCompleteBox,你似乎真的只有:    ValueMemberPath =“StateID”和    Text =“{Binding Path = tbl_Address.StateID,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged,Converter = {StaticResource StateConverter}}”Grid.Column =“1”Grid.Row =“1”Margin =“0,0,0, 0“>

使用AutoCompleteBox,您无法单独显示和选择值。由于我的Window控件绑定和数据库结构,我需要显示一个字符串值,如“CA California”,而实际上在后台有一个选定的表键整数值。

要清楚,我们需要在AutoCompleteBox控件的Text部分中包含单独的Display和Selected值。

这是显示承包商的主ListBox:

    <Grid DataContext="{StaticResource tbl_PartnerViewSource}" HorizontalAlignment="Left" Name="grid3" VerticalAlignment="Top" Height="242" Width="295" Grid.Column="1" Margin="0,1,0,0" Grid.Row="3">
        <Grid.RowDefinitions>
            <RowDefinition Height="31*" />
            <RowDefinition Height="211*" />
        </Grid.RowDefinitions>
        <Label Content="Contractor List" FontWeight="Bold" Height="25" Margin="0,0,169,0" Name="label10" Padding="3" VerticalAlignment="Top" />
        <ListBox DisplayMemberPath="tbl_Organization.OrgName" Height="212" HorizontalAlignment="Right" ItemsSource="{Binding}" Margin="0,30,0,0" Name="orgNameListBox" SelectedValuePath="PtnrID"  VerticalAlignment="Center" Width="294" IsSynchronizedWithCurrentItem="True" Grid.RowSpan="2" SelectionChanged="orgNameListBox_SelectionChanged" TabIndex="11" MouseDoubleClick="orgNameListBox_MouseDoubleClick" DataContextChanged="orgNameListBox_DataContextChanged" SourceUpdated="orgNameListBox_SourceUpdated" IsManipulationEnabled="True" />
    </Grid>

这是包含支持讨论中控件的Datacontext的Stack Panel:

<StackPanel Grid.Row="3" Height="243" HorizontalAlignment="Left" Name="stackPanel5" VerticalAlignment="Top" Width="295" Grid.Column="1"></StackPanel>
    <Label Content="Address(s)" FontWeight="Bold" Height="25" HorizontalAlignment="Left" Margin="306,0,0,0" Name="label1" Padding="3" VerticalAlignment="Top" Width="648" Grid.Column="1" Grid.Row="1" />
    <DataGrid Name="dataGridAddress" DataContext="{StaticResource tbl_Partnertbl_PartnerAddressViewSource}"  ItemsSource="{Binding}" SelectionMode="Single" SelectionUnit="CellOrRowHeader" AutoGenerateColumns="False" AlternatingRowBackground="#FFFF003B" AlternationCount="2" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Margin="308,22,0,0" Width="649" Height="113" VerticalAlignment="Top" CanUserDeleteRows="True" CanUserAddRows="True">
        <DataGrid.Columns>...

以下是部分正常工作的AutoCompleteBox:

<DataGridTemplateColumn x:Name="addState" Header="State" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <my1:AutoCompleteBox Name="autoCompleteBox1" 
                                                 IsTextCompletionEnabled="True" 
                                                 ItemsSource="{Binding Source={StaticResource tbl_StateViewSource}}"
                                                 ValueMemberPath="StateID"
                                                 Text="{Binding Path=tbl_Address.StateID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="1" Margin="0,0,0,0" >
                                <my1:AutoCompleteBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="{Binding Path=StateAbbrv}" Width="30" FontWeight="Bold" Margin="0,0,0,0" />
                                            <TextBlock Text="{Binding Path=StateName}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </my1:AutoCompleteBox.ItemTemplate>
                            </my1:AutoCompleteBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

这是使用TwoWay绑定的Combobox:

<GridViewColumn x:Name="addrState" Header="State" Width="75">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox Name="comboAddressState" SelectedValue="{Binding Path=tbl_Address.StateID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Margin="{StaticResource Margin2}" DisplayMemberPath="StateAbbrv" SelectedValuePath="StateID" ItemsSource="{Binding Source={StaticResource tbl_StateViewSource}}" IsSynchronizedWithCurrentItem="False" IsEnabled="True" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

这是Windows.Resource部分:

 <Window.Resources>
    <CollectionViewSource x:Key="tbl_PartnerViewSource" d:DesignSource="{d:DesignInstance my:tbl_Partner, CreateList=True}"></CollectionViewSource>
    <CollectionViewSource x:Key="tbl_Partnertbl_PartnerAddressViewSource" Source="{Binding Path=tbl_PartnerAddress, Source={StaticResource tbl_PartnerViewSource}}" />
    <CollectionViewSource x:Key="tbl_Partnertbl_PartnerContactViewSource" Source="{Binding Path=tbl_PartnerContact, Source={StaticResource tbl_PartnerViewSource}}" />
    <CollectionViewSource x:Key="tbl_Partnertbl_PartnerContacttbl_PartnerContactCommChanAddrViewSource" Source="{Binding Path=tbl_PartnerContactCommChanAddr, Source={StaticResource tbl_Partnertbl_PartnerContactViewSource}}" />
    <CollectionViewSource x:Key="tbl_Partnertbl_PartnerSystemViewSource" Source="{Binding Path=tbl_PartnerSystem, Source={StaticResource tbl_PartnerViewSource}}" />
    <CollectionViewSource x:Key="tbl_Partnertbl_PartnerAliasViewSource" Source="{Binding Path=tbl_PartnerAlias, Source={StaticResource tbl_PartnerViewSource}}" />
    <CollectionViewSource x:Key="tbl_Partnertbl_PartnerCommentViewSource" Source="{Binding Path=tbl_PartnerComment, Source={StaticResource tbl_PartnerViewSource}}" />
    <CollectionViewSource x:Key="tbl_ServicedCustomerViewSource"  d:DesignSource="{d:DesignInstance my:tbl_ServicedCustomer, CreateList=True}"  />
    <CollectionViewSource x:Key="tbl_SystemViewSource" d:DesignSource="{d:DesignInstance my:tbl_System, CreateList=True}" />
    <CollectionViewSource x:Key="tbl_PlatformViewSource" d:DesignSource="{d:DesignInstance my:tbl_Platform, CreateList=True}" />
    <CollectionViewSource x:Key="tbl_ManufacturerViewSource" d:DesignSource="{d:DesignInstance my:tbl_Manufacturer, CreateList=True}" />
    <CollectionViewSource x:Key="tbl_StateViewSource" d:DesignSource="{d:DesignInstance my:tbl_State, CreateList=True}" />
    <CollectionViewSource x:Key="tbl_ZIPViewSource" d:DesignSource="{d:DesignInstance my:tbl_ZIP, CreateList=True}" />
    <CollectionViewSource x:Key="tbl_CityViewSource" d:DesignSource="{d:DesignInstance my:tbl_City, CreateList=True}" />
    <Thickness x:Key="Margin1">8,-3,-7,-3</Thickness>
    <Thickness x:Key="Margin2">-7,-3,-7,-3</Thickness>
</Window.Resources>

这是Window(WPF)的图像:

enter image description here

0 个答案:

没有答案