locked
Setting Combobox SelectedBoxItemTemplate RRS feed

  • Question

  • Hi,

       I am trying to wrap the text in combobox items. This I was able to achieve using the below code. But the real problem came when I noticed the text wrapping occured after selecting the item. I want this text wrap only in the dropdown list and not in the selected item. I googled around and found a property of combobox named, SelectedBoxItemTemplate. Unfortunately we can get the value and cannot be set

    Any idea on how to set the template. Or any work around to acheive my customization?

     <UserControl.Resources>
            <DataTemplate x:Key="WrapTemplate">
                <TextBlock TextWrapping="Wrap" Text="{Binding Name}" />
            </DataTemplate>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot">
            <ComboBox x:Name="cmbBox" ItemsSource="{Binding}"   
                               ItemTemplate="{StaticResource WrapTemplate}"
                                 Width="120" >
                <ComboBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel HorizontalAlignment="Left" Orientation="Vertical" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Width="120">
                        </StackPanel>
                    </ItemsPanelTemplate>
                </ComboBox.ItemsPanel>
            </ComboBox>
        </Grid>
    

    Regards,

    Raaj

    Monday, February 27, 2012 12:54 PM

Answers

  • You can use template for the Combobox item. Something similar as the following:

     

    <ComboBox ItemsSource="{Binding StringList}" x:Name="cmb" Grid.Row="1" Width="150">     
       <ComboBox.ItemTemplate>
        
    <DataTemplate>
           
    <TextBlock Text="{Binding}" TextWrapping="Wrap" MaxWidth="160"/>
        
    </DataTemplate>
      
    </ComboBox.ItemTemplate>
    </ComboBox>

    Monday, February 27, 2012 1:23 PM

All replies

  • You can use template for the Combobox item. Something similar as the following:

     

    <ComboBox ItemsSource="{Binding StringList}" x:Name="cmb" Grid.Row="1" Width="150">     
       <ComboBox.ItemTemplate>
        
    <DataTemplate>
           
    <TextBlock Text="{Binding}" TextWrapping="Wrap" MaxWidth="160"/>
        
    </DataTemplate>
      
    </ComboBox.ItemTemplate>
    </ComboBox>

    Monday, February 27, 2012 1:23 PM
  • check the followin code, may be this will be help you

     <Grid x:Name="LayoutRoot" Background="White">
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="100,96,0,0"  Name="comboBox1" VerticalAlignment="Top" Width="212">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock TextWrapping="Wrap" Text="{Binding Name}" Height="100" Width="200"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </Grid>
    Monday, February 27, 2012 1:49 PM
  • Thanks guys. I have implemented this.

    Monday, February 27, 2012 3:36 PM
  • Hi,

    Have you resolved your issue?

    Friday, March 2, 2012 4:15 AM
  • Yes

    Monday, March 12, 2012 4:39 PM
  • Normally, you would mark the thread as "Answered"

    Monday, March 12, 2012 5:07 PM