开发者

How to modify silverlight combobox data display

I have a combobox defined as follows:

<ComboBox x:Name="cboDept" Grid.Row="0" Margin="8,8,8,8" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" 
                  ItemsSource="{Binding Source={StaticResource cvsCategories}}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Width="Auto" Height="Auto">
                        <sdk:Label Content="{Binding CategoryID}" Height="20" />
                        <sdk:Label Content="{Binding Categor开发者_如何学PythonyName}" Height="20" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

It works fine. However, once I select an item in the list, I want a different template to be applied to the combobox selected item being shown to the user (the item shown after the disappearance of popup). In the above case, I want only CategoryName to be displayed in the ComboBox once I select the respective item.

Can anyone let me know on how to achieve this?

thanks


What you need to do is create a ResourceDictionary containing a few defined templates yourself. In the below, ComboBoxTemplateOne and ComboBoxTeplateTwo are user controls that are set out to display the combobox in the manor you want.

   <UserControl.Resources>
        <ResourceDictionary>
          <DataTemplate x:Key="TemplateOne">
               <local:ComboBoxTemplateOne />
            </DataTemplate>
          <DataTemplate x:Key="TemplateTwo">
               <local:ComboBoxTemplateTwo />
            </DataTemplate>
        </ResourceDictionary>
   </UserControl.Resources>

You will then need to create your own class that inherits from ContentControl "DataTemplateSelector", overriding OnContentChanged

  Protected Overrides Sub OnContentChanged(ByVal oldContent As Object, ByVal newContent As Object)
    MyBase.OnContentChanged(oldContent, newContent)

    Me.ContentTemplate = SelectTemplate(newContent, Me)

  End Sub

You will then need to create another class that inherits from the above DataTemplateSelector which overrides SelectTemplate ("TemplateSelectorClass"), which will return the DataTemplate defined above ("TemplateOne" or "TemplateTwo"). Also in this derived class, you will need to define a property for each of the templates you have

Public Property ComboboxTemplateOne As DataTemplate

Then head back to your XAML and n the blow XAML

 <local:TemplateSelectorClass  ComboboxTemplateOne="{StaticResource TemplateOne}"  Content="{Binding Path=ActiveWorkspace}>

This should work, as it is effectively doing the same work as setting the "DataTemplate" property in WPF (which doesn't exist in SilverLight) I realise there are a fair few steps here and its quite fiddly, but hopefully this will get you there. Any questions just shout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜