gpt4 book ai didi

wpf - 当已选择行时,WPF MVVM DataGrid DoubleClick不起作用

转载 作者:行者123 更新时间:2023-12-03 10:19:07 25 4
gpt4 key购买 nike

所以我有一个与this question相同的问题。
但是,此问题的解决方案似乎是Code-Behind(已有4年历史了),但我使用的是MVVM。时间可能带来了更好的解决方案。

有谁知道在选中DataGridRow后可以在DataGridRow上进行双击的方法吗?

这是我的XAML:

<DataGrid x:Name="ContactsResult" 
AlternatingRowBackground="Transparent"
AutoGenerateColumns="False"
BorderBrush="{StaticResource DarkBlueBrush}"
BorderThickness="1"
CanUserAddRows="False"
FontFamily="{StaticResource DefaultFont}"
GridLinesVisibility="None"
HeadersVisibility="Column"
IsReadOnly="True"
ItemsSource="{Binding Results, Mode=TwoWay}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
>

<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding DetailsCommand}"
CommandParameter="{Binding ElementName=ContactsResult, Path=SelectedItem.ContactId}"
/>
</DataGrid.InputBindings>

<DataGrid.Columns>
<DataGridTextColumn x:Name="contactLastNameColumn"
Binding="{Binding Name_LastSufFirstMidSal}"
ElementStyle="{StaticResource dgCellPadding5}" Header="Contact Name"
Width="Auto"
/>
<DataGridTextColumn x:Name="companyNameColumn"
ElementStyle="{StaticResource dgCellPadding5}" Header="Company Name"
Width="Auto"
/>
<DataGridTextColumn x:Name="CityColumn"
Binding="{Binding MailingAddress.City}"
ElementStyle="{StaticResource dgCellPadding5}" Header="Mailing City"
Width="Auto"
/>
<DataGridTextColumn x:Name="StateColumn"
Binding="{Binding MailingAddress.StateOrProvince}" ElementStyle="{StaticResource dgCellPadding5}" Header="Mailing State"
Width="Auto"
/>
</DataGrid.Columns>
</DataGrid>

编辑

ElementSytle
<Style x:Key="dgCellPadding5" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="5" />
</Style>

最佳答案

以下针对.NET 4.7.1的示例触发了双击命令(无论选择状态如何)。我已经从上面包含了您引用的样式。您的代码与此不同吗?

<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wpfApp1="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<wpfApp1:ViewModel />
</Window.DataContext>
<Window.Resources>
<Style x:Key="dgCellPadding5" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="5" />
</Style>
</Window.Resources>
<DataGrid x:Name="ContactsResult"
AlternatingRowBackground="Transparent"
AutoGenerateColumns="False"
BorderThickness="1"
CanUserAddRows="False"
GridLinesVisibility="None"
HeadersVisibility="Column"
IsReadOnly="True"
ItemsSource="{Binding Contacts}"
SelectedItem="{Binding SelectedContact, Mode=TwoWay}">

<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding Command}"
CommandParameter="{Binding ElementName=ContactsResult, Path=SelectedItem}" />
</DataGrid.InputBindings>

<DataGrid.Columns>
<DataGridTextColumn ElementStyle="{StaticResource dgCellPadding5}"
Binding="{Binding ContactName}"
Header="Contact Name"
Width="Auto" />
<DataGridTextColumn ElementStyle="{StaticResource dgCellPadding5}" Header="Company Name"
Binding="{Binding CompanyName}"
Width="Auto" />
<DataGridTextColumn Binding="{Binding City}" Header="Mailing City"
Width="Auto" ElementStyle="{StaticResource dgCellPadding5}" />
<DataGridTextColumn Binding="{Binding State}" Header="Mailing State"
Width="Auto" ElementStyle="{StaticResource dgCellPadding5}" />
</DataGrid.Columns>
</DataGrid>
using System;
using System.Diagnostics;
using System.Windows.Input;
using Prism.Commands;
using Prism.Mvvm;

namespace WpfApp1
{
public class ViewModel : BindableBase
{
public ContactInfo[] Contacts
{
get;
} =
{
new ContactInfo
{
ContactName = "First",
CompanyName = "Acme",
City = "Somewhere",
State = "CA"
},
new ContactInfo
{
ContactName = "Second",
CompanyName = "Acme",
City = "Somewhere",
State = "CA"
},
new ContactInfo
{
ContactName = "Third",
CompanyName = "Acme",
City = "Somewhere",
State = "CA"
},
new ContactInfo
{
ContactName = "Fourth",
CompanyName = "Acme",
City = "Somewhere",
State = "CA"
}
};

private ContactInfo _selectedContact;

public ContactInfo SelectedContact
{
get => _selectedContact;
set => SetProperty(ref _selectedContact, value);
}

public ICommand Command
{
get;
} = new DelegateCommand<ContactInfo>(ci => Debug.WriteLine($"{ci.ContactName} {DateTime.Now}"));

public ICommand OtherCommand
{
get;
} = new DelegateCommand<string>(s => Debug.WriteLine($"{s} {DateTime.Now}"));
}

public class ContactInfo
{
public string ContactName
{
get;
set;
}

public string CompanyName
{
get; set;
}

public string City
{
get;
set;
}

public string State
{
get;
set;
}
}
}

关于wpf - 当已选择行时,WPF MVVM DataGrid DoubleClick不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51427456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com