첨부 된 속성에서 BringIntoView를 사용하여 ListBox의 스크롤을 동기화하려고합니다.속성이 연결된 ListBoxItem에서 BringIntoView를 가져 오겠습니까?
내 시도가 작동하지 않습니다. 코드를 실행하면 선택한 항목이 목록 상자에 동기화됩니다. 그러나 다른 ListBox에 표시되지 않는 항목을 선택하면 자동으로 스크롤되지 않습니다.
저는 IsShown 및 ItemText 속성을 가진 간단한 ViewModel을 가지고 있습니다. 여기
<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>
내 종속성 속성
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();
}
}
}
이것은 샘플 응용 프로그램에 불과합니다. 내 실제 프로젝트는 계층 적 데이터 및 목록 상자에 바인딩 된 데이터 병합 된 버전이있는 TreeView를 사용합니다. 모든 것을 처리하기 위해 하나의 콜렉션 뷰에 의존 할 수 있는지 확실하지 않습니다. – kbeal2k