반응형
Behavior 클래스를 먼저 작성한다.
public static class ScrollToSelectedBehavior
{
public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.RegisterAttached(
"SelectedValue",
typeof(object),
typeof(ScrollToSelectedBehavior),
new PropertyMetadata(null, OnSelectedValueChange));
public static void SetSelectedValue(DependencyObject source, object value)
{
source.SetValue(SelectedValueProperty, value);
}
public static object GetSelectedValue(DependencyObject source)
{
return (object)source.GetValue(SelectedValueProperty);
}
private static void OnSelectedValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var listView = d as ListView;
listView.ScrollIntoView(e.NewValue);
}
}
xaml에서 사용할때는 아래와 같이 사용한다.
<ListView SelectedIndex="{Binding SizeListViewSelectedIndex, Mode=TwoWay}"
behavior:ScrollToSelectedBehavior.SelectedValue="{Binding Path=SelectedValue, RelativeSource={RelativeSource Self}}"
ItemsSource="{Binding SizeSelectionLists}"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden">
반응형
'프로그래밍 > WPF' 카테고리의 다른 글
[WPF] Calendar DatePicker 크기 조절 방법 (0) | 2022.04.15 |
---|---|
[WPF] DataGridComboBoxColumn binding 버그 (0) | 2022.04.15 |
[WPF] Combobox AutoComplete 기능 구현 (0) | 2022.03.30 |
[WPF] Storyboard 이용하여 Visibility 제어 (0) | 2022.03.30 |
[WPF] DataGridTemplateColumn 내의 Grid Margin 없애기 (0) | 2021.12.06 |