I'm trying to sync scrolling of my ListBox using BringIntoView on an attached property.
My attempt is not working. When I run my code the selected items are synced across the list boxes but if I select an item that is not displayed on the other ListBox it does not automatically scroll.
I have a simple ViewModel with IsShown and ItemText properties.
<Window.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsShown, Mode=TwoWay}"/>
<Setter Property="local:CustomProperties.BringIntoView" Value="{Binding IsShown}"/>
</Style>
<DataTemplate x:Key="DataTemplate1">
<TextBlock Name="_txt" Text="{Binding ItemText}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="255*"/>
<ColumnDefinition Width="262*"/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding ItemList}" ItemTemplate="{StaticResource DataTemplate1}" />
<ListBox Grid.Column="1" ItemsSource="{Binding ItemList}" ItemTemplate="{StaticResource DataTemplate1}" >
</ListBox>
</Grid>
Here is my dependency property
public static class CustomProperties
{
public static readonly DependencyProperty BringIntoViewProperty =
DependencyProperty.RegisterAttached( "BringIntoView",
typeof( bool ),
typeof( CustomProperties ),
new PropertyMetadata( OnBringIntoViewChanged ) );
public static void SetBringIntoView ( DependencyObject o, bool value )
{
o.SetValue( BringIntoViewProperty, value );
}
public static bool GetBringIntoView ( DependencyObject o )
{
return ( bool )o.GetValue( BringIntoViewProperty );
}
private static void OnBringIntoViewChanged ( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
if ( ( bool )e.NewValue )
{
if ( d is FrameworkElement )
( ( FrameworkElement )d ).BringIntoView();
}
}
}