WPF:在选择该框之前,不会应用 ComboBox ItemTemplate
我有一个 ViewModel,其属性是类的实例。当我编辑所述 ViewModel 时,我有一个动态绑定到该类集合的 ComboBox。问题是,如果项目在编辑之前具有实例,则在我选择并展开组合框之前,不会应用 ItemTemplate。
因此,当我弹出“编辑”窗口时,组合框中显示的项目是 myProject.myNameSpace.Type
但一旦我单击组合框,它就会变成 NameOfType SomeInfo
,就像它应该的那样是。
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");
}
}
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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法重现。难道是因为您的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.