1

I have a CustomControl (say CC) that has been inherited from ContentControl and contains a ScrollViewer which includes a ContentPresenter. When I put a ListBox into the CC it works without any problem. But when I set the ItemsPanelTemplate of the ListBox it doesn't notify CC to scroll into the ListBox selected item.

What's the reason for it? -Thanks


UPDATE:

I'll encounter the problem described above only if I set HorizontalScrollBarVisibility or VerticalScrollBarVisibility to Hidden and customize the ItemsPanelTemplate of the ListBox simultaneously. (I need to hide scollbars.)

I wonder if hiding Scrollbars prevents ScrollViewer contents from notifying it to bring selected item into view, why this issue doesn't happen when I don't change items panel???


Generic.xaml:

<ResourceDictionary ...>
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border ...>
                        <ScrollViewer ...
                            CanContentScroll="True"
                            HorizontalScrollBarVisibility="Hidden"   « PROBLEM
                            VerticalScrollBarVisibility="Hidden">    « 

                            <ContentPresenter Content="{TemplateBinding Content}"/>

                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

MainWindow.xaml:

<Window x:Class="MyNamespace1.MainWindow"
        ...
        xmlns:proj="clr-namespace:MyNamespace0;assembly=...">
    <Grid>
        <proj:CustomControl1 x:Name="CC">
            <ListBox>
                <ListBox.ItemsPanel>                              «
                    <ItemsPanelTemplate>                          «
                        <StackPanel Orientation="Horizontal"/>    « PROBLEM
                    </ItemsPanelTemplate>                         «
                </ListBox.ItemsPanel>                             «

                <!--content goes here-->

            </ListBox>
        </proj:CustomControl1>
    </Grid>
</Window>
Mehdi
  • 1,985
  • 2
  • 22
  • 37

2 Answers2

0

Have you set the IsItemsHost property for the Panel in the ItemsPanelTemplate to True?

E.g. if the itemspaneltemplate should use a Canvas:

<ItemsPanelTemplate>
   <Canvas IsItemsHost="True" /> 
</ItemsPanelTemplate>

Related

Community
  • 1
  • 1
edvaldig
  • 2,191
  • 14
  • 17
  • Ok worth the try as that is usually a thing people forget. I guess you then need to provide us with the xaml code to better understand the problem :) – edvaldig Sep 22 '11 at 10:51
0

StackPanel treats its content has having infinite space.. You will have to limit its size explicitly, or change it to other panel, Grid for example.

Try this:

<ItemsPanelTemplate>
      <Grid/>  
  </ItemsPanelTemplate>

Or:

  <ItemsPanelTemplate>
       <StackPanel Orientation="Horizontal" Width="100" Height="100"/>
  </ItemsPanelTemplate>
MichaelS
  • 6,682
  • 9
  • 45
  • 71
  • Nope! This is already done via placing `CustomControl1` into a grid. (look at MainWindow code.) – Mehdi Sep 22 '11 at 21:25