DataTemplate의 이벤트 핸들러
-
05-07-2019 - |
문제
데이터 템플릿 내에 WPF ComboBox (ListBox의 많은 Comboboxes)가 있으며 Enter 버튼을 처리하고 싶습니다. 버튼이라면 쉬울 것입니다. 명령 + 상대 바인딩 경로 등을 사용합니다. 불행히도, 나는 명령으로 키 프레스를 처리하는 방법이나 템플릿에서 이벤트 핸들러를 설정하는 방법을 모릅니다. 제안이 있습니까?
해결책 2
비주얼 트리를 통과하는 일반적인 이벤트 핸들러를 사용하여 문제를 해결하고 해당 버튼을 찾고 명령을 호출합니다. 다른 사람이 같은 문제가있는 경우 의견을 게시하시면 더 자세한 내용을 제공하겠습니다.
upd
내 해결책은 다음과 같습니다.
버튼과 관련된 명령을 실행하는 것보다 시각적 트리를 검색합니다.
view.xaml :
<ComboBox KeyDown="ComboBox_KeyDown"/>
<Button Command="{Binding AddResourceCommand}"/>
view.xaml.cs :
private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var parent = VisualTreeHelper.GetParent((DependencyObject)sender);
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i) as Button;
if (null != child)
{
child.Command.Execute(null);
}
}
}
}
다른 팁
템플릿을 설정하는 스타일로 이벤트 세터를 사용할 수 있습니다.
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseWheel" Handler="GroupListBox_MouseWheel" />
<Setter Property="Template" ... />
</Style>
이 기사는 어떤 길을 라우팅 할 수있는 방법이 있습니다 Event
에게 Command
http://nerobrain.blogspot.nl/2012/01/wpf-events-to-command.html
제휴하지 않습니다 StackOverflow