WPF:ComboBox ItemTemplate不会应用,直到我选择框 [英] WPF: ComboBox ItemTemplate doesn't get applied until I select the box
问题描述
我有一个ViewModel,它的属性是一个类的实例。当我编辑的ViewModel我有一个ComboBox动态绑定到类的集合。问题是,如果项目在编辑之前具有实例,ItemTemplate将不会应用,直到我选择和展开ComboBox。
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.
所以当我弹出编辑窗口项目出现在comboBox是 myProject.myNameSpace.Type
但是一旦我点击ComboBox它变成 NameOfType SomeInfo
应该是。
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");
}
}
推荐答案
不能再现。是否因为您的AvailableTypes属性返回 _availableSections
而不是 _availableTypes
?如果没有,请张贴一个完整的,孤立的重现。
Can't reproduce. Could it be because your AvailableTypes property returns _availableSections
instead of _availableTypes
? If not, please post a complete, isolated repro.
这篇关于WPF:ComboBox ItemTemplate不会应用,直到我选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!