AutoCompleteBox & DataTemplates

Apr 12, 2010 at 11:12 AM
Edited Apr 12, 2010 at 12:43 PM

Hi,

I don't understand a thing. If I use a ContentPresenter to display views (for example, data templates), then the AutoCompleteBox sometimes forgets to display the content (ValueMemberPath). Instead of a settlement name, I can see the type name.  For example, the following template displays a location data (settlement, stateprovince, country): 

<DataTemplate x:Key="LocationView">
...
<!-- Event Handler Commands -->        

<corecommands:CommandTrigger
        EventSource="{Binding ElementName=acSettlement}"
        Event="{x:Static toolkitinputcontrols:AutoCompleteBox.PopulatingEvent}"
        Command="{Binding 'PopulatingCommand'}"
        CommandParameter="{Binding ElementName=acSettlement}"/>

<!-- DataTemplate Controls --> 

 

 

<toolkitinputcontrols:AutoCompleteBox
        x:Name="acSettlement" FilterMode="StartsWith"                                   
        SelectedItem="{Binding 'SelectedItem.Settlement', Mode=TwoWay}"
        ItemsSource="{Binding 'Settlement.CollectionView'}"
        ValueMemberPath="Name" MinimumPrefixLength="2" MinimumPopulateDelay="1000">
        <toolkitinputcontrols:AutoCompleteBox.ItemTemplate>
                <DataTemplate>
                        <StackPanel Margin="2">
                                <TextBlock Text="{Binding 'Name', Mode=OneWay}" FontWeight="Bold"/>
                                <TextBlock Text="{Binding 'StateProvince.Name', Mode=OneWay}" Margin="8,0,0,0"/>
                                <TextBlock Text="{Binding 'StateProvince.Country.IsoName', Mode=OneWay, Converter={coreconverters:CultureStringDisplayNameConverter}}" Margin="16,0,0,0"/>
                        </StackPanel>
                </DataTemplate>
        </toolkitinputcontrols:AutoCompleteBox.ItemTemplate>
</toolkitinputcontrols:AutoCompleteBox>
....
</DataTemplate> 

 

 

<toolkitinputcontrols:AutoCompleteBox x:Name="acSettlement" Grid.Column="1" Grid.Row="1" Margin="2"
                                              VerticalAlignment="Center" FilterMode="StartsWith"                                              
                                              SelectedItem="{Binding 'SelectedItem.Settlement', Mode=TwoWay}"
                                              ItemsSource="{Binding 'Settlement.CollectionView'}"
                                              ValueMemberPath="Name" MinimumPrefixLength="2" MinimumPopulateDelay="500">
          <toolkitinputcontrols:AutoCompleteBox.ItemTemplate>
            <DataTemplate>
              <StackPanel Margin="2">
                <TextBlock Text="{Binding 'Name', Mode=OneWay}" FontWeight="Bold"/>
                <TextBlock Text="{Binding 'StateProvince.Name', Mode=OneWay}" Margin="8,0,0,0"/>
                <TextBlock Text="{Binding 'StateProvince.Country.IsoName', Mode=OneWay, Converter={coreconverters:CultureStringDisplayNameConverter}}" Margin="16,0,0,0"/>
              </StackPanel>
            </DataTemplate>
          </toolkitinputcontrols:AutoCompleteBox.ItemTemplate>
        </toolkitinputcontrols:AutoCompleteBox>

It works very well but when I change the current view template (for example, an user navigates to the address view, and go back), then the AutoCompleteBox displays the type name instead of the Name property. This is the view template selector:

<ContentPresenter
       Content="{Binding 'Current', Mode=OneWay}"
       ContentTemplateSelector="{Binding Mode=OneWay, Source={StaticResource vtsOrganization}}"/> 

Step by step:

1. An user selects the location menu item. She/He can see a settlement name, for example: "Los Angeles". Our hero adds, edits, deletes items (ListBox + Details /AutoCompleteBox/)...
2. She/he navigates to a new view (template) using the menu items (general view, address view, etc.).  Our hero adds, edits, deletes items...
3. She/he goes back to the location view.
4. The AutoCompleteBox displays the selected settlement's type name instead of the Name property. It forgets to display the ValueMemberPath.
    But here is the trick. When I select a new item (settlement) in the listbox and go back to the previous item, it works very well again. The AutoCompleteBox displays the "Name" property again.
    This is some update bug??? 

+ When I load settlements, I have to use the Input priority:

DispatcherUtility.DoDispatch(() =>
{
     CollectionView.MoveCurrentToFirst();
},
DispatcherPriority.Input);
 
If I use normal priority, then the same problem occures. This is the end of an open command in the viewmodel. There is a workaround:

DispatcherUtility.DoDispatch(() =>
{
     var prev = this.SelectedItem;
     this.SelectedItem = null;
     this.SelectedItem = prev;
},
DispatcherPriority.Input); 

If I do it, then the AutoCompleteBox also works fine. Otherwise, other controls work very well without it.

 

Apr 14, 2010 at 6:06 AM

There is a workaround:
I must overwrite the ToString() method of all EF entities. But it is not a good thing. Please, fix this bug.
The AutoCompleteBox doesn't display the specified property of the selected object after loading.
Instead of a property value (for example, "Name"), I can see the ToString() value.