我使用MVVM架构编写WPF应用程序,因此我有Model,View和ViewModel。我的视图中有DataGrid
,其中包含Product
列表(包含多个字段:名称,余额等)。
我想添加额外的窗口/视图,点击DataGrid
中的元素后会显示。可能吗?如果是这样,怎么样?
我对xaml代码感兴趣,而不是button1_Click
。
这就是我现在所拥有的:
<Window ...
Height="600" Width="800"
WindowState="Maximized">
<Grid>
<DataGrid AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserResizeRows="False"
IsReadOnly="True"
SelectionMode="Single"
SelectionUnit="FullRow"
ItemsSource="{Binding Path=Products}"
SelectedItem="{Binding Path=CurrentProduct, Mode=TwoWay}"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ProductName}"
Header="Product Name"
Width="*" />
<DataGridTextColumn Binding="{Binding Path=Balance}"
Header="Balance"
Width="Auto" />
...
</DataGrid.Columns>
</DataGrid>
</Grid>
答案 0 :(得分:1)
当您右键单击datagrid的某个元素时,这将触发命令:
<DataGrid.InputBindings>
<MouseBinding
MouseAction="RightClick"
Command="{Binding yourCommand,Mode=OneWay}"
/>
</DataGrid.InputBindings>
运行新视图:
var win = new Window();
win.Content = new yourNewViewModel();
win.SizeToContent = SizeToContent.WidthAndHeight; //Adjust window size to content
win.Show();
您必须在App.xaml中链接yourNewViewModel和yourNewView:
<DataTemplate DataType="{x:Type yourProjectViewModel:yourNewViewModel}">
<local:yourNewView/>
</DataTemplate>
别忘了将yourProjectViewModel的xmlns添加到App.xaml