WPF: ComboBox ItemTemplate doesn't get applied until I select the box

心不动则不痛 提交于 2019-12-08 09:45:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!