.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Custom Panel + IScrollInfo + ListBox, How it works?
 

Custom Panel + IScrollInfo + ListBox, How it works?

Hello,

i've written a custom Panel:

Code Snippet

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;


namespace ScrollTest
{
public class testPanel:Panel,IScrollInfo
{

private Size _extent = new Size(0, 0);
private Size _viewport = new Size(0, 0);
private Point _offset;
private ScrollViewer _owner;
private TranslateTransform _trans = new TranslateTransform();

public testPanel()
{
this.RenderTransform = _trans;
}

protected override Size MeasureOverride(Size availableSize)
{
double yOffset = 0;

foreach (UIElement child in InternalChildren)
{
child.Measure(availableSize);
yOffset += child.DesiredSize.Height;
}

_extent = new Size(availableSize.Width, yOffset);
_viewport = availableSize;

if(_owner != null)
_owner.InvalidateScrollInfo();

return base.MeasureOverride(availableSize);
}

protected override Size ArrangeOverride(Size finalSize)
{
double yOffset = 0;

foreach (UIElement child in InternalChildren)
{
Rect rc = new Rect(0, yOffset, child.DesiredSize.Width, child.DesiredSize.Height);
child.Arrange(rc);
yOffset += child.DesiredSize.Height;
}



return base.ArrangeOverride(finalSize);
}

#region IScrollInfo Members


bool canHScroll = false;
public bool CanHorizontallyScroll
{
get
{
return canHScroll;
}
set
{
canHScroll = value;
}
}

bool canVScroll = true;
public bool CanVerticallyScroll
{
get
{
return canVScroll;
}
set
{
canVScroll = value;
}
}

public double ExtentHeight
{
get { return _extent.Height; }
}

public double ExtentWidth
{
get { return _extent.Width; }
}

public double HorizontalOffset
{

get { return _offset.X; }

}

public void LineDown()
{
SetVerticalOffset(this.VerticalOffset + 1);
}

public void LineLeft()
{
throw new NotImplementedException();
}

public void LineRight()
{
throw new NotImplementedException();
}

public void LineUp()
{
SetVerticalOffset(this.VerticalOffset - 1);
}

public Rect MakeVisible(Visual visual, Rect rectangle)
{
throw new NotImplementedException();
}

public void MouseWheelDown()
{
throw new NotImplementedException();
}

public void MouseWheelLeft()
{
throw new NotImplementedException();
}

public void MouseWheelRight()
{
throw new NotImplementedException();
}

public void MouseWheelUp()
{
throw new NotImplementedException();
}

public void PageDown()
{
throw new NotImplementedException();
}

public void PageLeft()
{
throw new NotImplementedException();
}

public void PageRight()
{
throw new NotImplementedException();
}

public void PageUp()
{
throw new NotImplementedException();
}

public ScrollViewer ScrollOwner
{
get { return _owner; }
set { _owner = value; }
}

public void SetHorizontalOffset(double offset)
{
throw new NotImplementedException();
}

public void SetVerticalOffset(double offset)
{
if (offset < 0 || _viewport.Height >= _extent.Height)
{
offset = 0;
}
else
{
if (offset + _viewport.Height >= _extent.Height)
{
offset = _extent.Height - _viewport.Height;
}
}

_offset.Y = offset;


if (_owner != null)
_owner.InvalidateScrollInfo();

_trans.Y = -offset;
}

public double VerticalOffset
{
get { return _offset.Y; }
}

public double ViewportHeight
{
get { return _viewport.Height; }
}

public double ViewportWidth
{
get { return _viewport.Width; }
}

#endregion
}
}


XAML:

Code Snippet

<Window x:Class="ScrollTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:ScrollTest"
Title="Window1" Height="155" Width="300">
<Window.Resources>
<Style TargetType="{x:Type ListBox}">
<Setter Property="Background" Value="Blue"/>
<Setter Property="ClipToBounds" Value="True"/>

<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="{TemplateBinding Background}">
<l:testPanel IsItemsHost="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>

<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ScrollViewer CanContentScroll="True" Grid.Row="0">
<l:testPanel>
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
<Button>Button 4</Button>
<Button>Button 5</Button>
<Button>Button 6</Button>
</l:testPanel>
</ScrollViewer>
<ListBox ScrollViewer.CanContentScroll="True" Grid.Row="1">
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
<Button>Button 4</Button>
<Button>Button 5</Button>
<Button>Button 6</Button>
</ListBox>
</Grid>
</Window>


I want my Panel in the Listbox as Items Host, but my Code works only with the ScrollViewer.

What do i wrong?

Best Regards
Thorsten

eclere

Hi Thorsten,

your ListBox-Template doesn't contain a ScrollViewer, so a ListBox using it can't scroll anymore. Set the Template for the ListBox like this:

Code Snippet

<Style TargetType="{x:Type ListBox}">

...
<
Setter Property="Template">

<Setter.Value>

<ControlTemplate>

<Border Background="{TemplateBinding Background}">

<ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False">

<l:testPanel IsItemsHost="True"/>

</ScrollViewer>

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

Thomas Claudius Huber

Hi Thorsten,

your ListBox-Template doesn't contain a ScrollViewer, so a ListBox using it can't scroll anymore. Set the Template for the ListBox like this:

Code Snippet

<Style TargetType="{x:Type ListBox}">

...
<
Setter Property="Template">

<Setter.Value>

<ControlTemplate>

<Border Background="{TemplateBinding Background}">

<ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False">

<l:testPanel IsItemsHost="True"/>

</ScrollViewer>

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

Thomas Claudius Huber
Hallo Thomas,

oh..so simple:-) Many thanks.

Thorsten
eclere

You can use google to search for other answers

Custom Search

More Threads

• Convert image bit depth from 32 to 8
• How to clone a tabitem in code?
• Path.Data.ClearValue()
• Displaying custom data in a combobox
• WPF Localization
• DATA BINDING: Why are ValidationRule and ValueConverter objects always invoked with null value?
• Setting Zindex of datatemplate in a Itemscontrol ? is it possible
• My cube doesn't rotate on center
• HierarchicalDataTemplate not finding correct public Type
• On Writing a Dockable Panel System in .NET 3.0