问题
I have a ViewModel with a property that is a instance of a class. When I edit said ViewModel I have a ComboBox dynamically bound to a collection of the class. Problem is that if the Item has the instance before editing the ItemTemplate won't get applied until I select and expand the ComboBox.
So when I pop up the Edit window the item appearing in the comboBox is myProject.myNameSpace.Type
but as soon as I click the ComboBox it turns into NameOfType SomeInfo
like it should be.
XAML:
<ComboBox Grid.Column="1"
Width="Auto"
HorizontalAlignment="Left"
VerticalAlignment="Top"
SelectedItem="{Binding Path=Type, Mode=TwoWay}"
ItemsSource="{Binding Path=AvailableTypes}"
TextSearch.TextPath="TypeName"
IsTextSearchEnabled="True"
IsEditable="True" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="4"
Text="{Binding Path=TypeName}" />
<TextBlock Margin="4"
Text="{Binding Path=TypeInfo}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"
Foreground="Red">
</TextBlock>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ComboBox.GroupStyle>
C#:
private ListCollectionView _availableTypes;
public ListCollectionView AvailableTypes
{
get
{
if (_availableTypes == null)
{
_availableTypes = new ListCollectionView(Context.GetAllTypes());
_availableTypes.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
}
return _availableTypes;
}
}
public TypeClass Type
{
get { return Model.Type; }
set
{
Model.Type = value;
RaisePropertyChanged("Type");
}
}
回答1:
Can't reproduce. Could it be because your AvailableTypes property returns _availableSections
instead of _availableTypes
? If not, please post a complete, isolated repro.
来源:https://stackoverflow.com/questions/6342972/wpf-combobox-itemtemplate-doesnt-get-applied-until-i-select-the-box