我需要以编程方式创建一个DataGrid,并需要向其添加双击行事件.这是如何在C#中完成的?我找到了这个;
myRow.MouseDoubleClick += new RoutedEventHandler(Row_DoubleClick);
Run Code Online (Sandbox Code Playgroud)
虽然这对我不起作用,因为我绑定DataGrid.ItemsSource到集合而不是手动添加行.
我试图在双击事件后从数据网格中检索行信息.我有事件设置,但现在我只需要设置函数来从行中检索数据.
XAML:
<DataGrid
Width="Auto"
SelectionMode="Extended"
IsReadOnly="True"
Name="ListDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding ListFieldObject.MoviesList}"
DataContext="{StaticResource MovieAppViewModel}"
cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect()]">
<DataGrid.Columns>
<DataGridTextColumn Width="200" IsReadOnly="True" Header="Title" Binding="{Binding Title}"/>
<DataGridTextColumn Width="100" IsReadOnly="True" Header="Rating" Binding="{Binding Rating}"/>
<DataGridTextColumn Width="100" IsReadOnly="True" Header="Stars" Binding="{Binding Stars}"/>
<DataGridTextColumn Width="93" IsReadOnly="True" Header="Release Year" Binding="{Binding ReleaseYear}"/>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
C#(MVVM ViewModel):
public void RowSelect()
{
//now how to access the selected row after the double click event?
}
Run Code Online (Sandbox Code Playgroud)
非常感谢!
我有一个DataGrid样式模板,我希望添加双击行为.绑定应该是正确的,但我似乎无法得到xaml编译/工作.
添加到IDictionary的所有对象必须具有Key属性或与其关联的其他类型的键.
下面的代码有什么问题?
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseDoubleClick" Handler="{Binding Connect}"/>
Run Code Online (Sandbox Code Playgroud)
更新每个Viktor的评论(给出完全相同的错误):
<Style x:Key="dataGridRowStyle" TargetType="{x:Type DataGridRow}">
<EventSetter Event="PreviewMouseDoubleClick" Handler="{Binding Connect}"/>
Run Code Online (Sandbox Code Playgroud)