MVVMLIGHT EVENTTOCOMMAND 및 WPFTOOLKIT DATAGRID를 두 번 클릭합니다
-
18-09-2019 - |
문제
EventTocommand를 사용하여 행에 대한 DataGrid Double Click Handler를 설정하는 방법을 알아 내려고합니다. 명령은 각 행의 뷰 모델에 있습니다. 단지 저것 아직 상호 작용을 사용하지 않았기 때문에 내 경험에서 많은 것입니다.
감사.
나는 mvvmlight 태그를 사용했지만 아직 새로운 태그를 만들 수있는 충분한 반복은 없다.
해결책
명령이 "rowviewModel"이 아닌 "gridviemodel"에 사는 경우 솔루션이 될 것입니다.
<Window...
...xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras">
<dg:DataGrid x:Name="dg">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding SelectedItem, ElementName=dg}" Command="{Binding Path=SelectCommand, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</dg:DataGrid>
</Window>
행에는 고유 한 뷰 모델이 있기 때문에 RowView를 만들 수 있으며 RowView에서 Row의 하위 요소 (컨테이너)의 MouseDoubleClick 이벤트를 사용합니다.
또는 명령 바인딩에 대한 변환기를 만듭니다.
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding SelectedItem, ElementName=dg, Mode=OneWay, Converter=...}"/>
그런 다음 변환기는 선택된 유형이 명령을 반환하는 데 필요한 유형인지 확인합니다 (릴레이 커밋 속성을 사용하여 iselectCommandable과 같은 것).
다른 팁
누군가가 여기를보고 와서 내가 어떻게 내가 어떻게했는지 궁금해하는 경우, 이벤트 tocommand
public class DataGridAttachedBehaviors
{
#region DoubleClick
public static DependencyProperty OnDoubleClickProperty = DependencyProperty.RegisterAttached(
"OnDoubleClick",
typeof(ICommand),
typeof(DataGridAttachedBehaviors),
new UIPropertyMetadata(DataGridAttachedBehaviors.OnDoubleClick));
public static void SetOnDoubleClick(DependencyObject target, ICommand value)
{
target.SetValue(DataGridAttachedBehaviors.OnDoubleClickProperty, value);
}
private static void OnDoubleClick(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var element = target as Control;
if (element == null)
{
throw new InvalidOperationException("This behavior can be attached to a Control item only.");
}
if ((e.NewValue != null) && (e.OldValue == null))
{
element.MouseDoubleClick += MouseDoubleClick;
}
else if ((e.NewValue == null) && (e.OldValue != null))
{
element.MouseDoubleClick -= MouseDoubleClick;
}
}
private static void MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
UIElement element = (UIElement)sender;
ICommand command = (ICommand)element.GetValue(DataGridAttachedBehaviors.OnDoubleClickProperty);
command.Execute(null);
}
#endregion DoubleClick
#region SelectionChanged
//removed
#endregion
}
내 xaml에서 :
<dg:DataGrid.RowStyle>
<Style BasedOn="{StaticResource DataGridDemoRowStyle}"
TargetType="{x:Type dg:DataGridRow}">
<Setter Property="skins:DataGridAttachedBehaviors.OnDoubleClick"
Value="{Binding Recall}" />
</Style>
</dg:DataGrid.RowStyle>
제휴하지 않습니다 StackOverflow