在我的项目中,我使用tabcontrol进行导航。 tabcontrol有三个tabitem,第二个tabitem是一个usercontrol,它包含一个datagrid,第三个tabitem包含一个canvas。当我双击datagrid中的一行时,我希望UI跳转到第三个tabitem并在画布中绘制选定的行。我怎样才能做到这一点?谢谢! 主窗口xaml如下:
<Window x:Class="PCClient.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:local="clr-namespace:PCClient"
xmlns:viewModel="clr-namespace:PCClient.ViewModels"
xmlns:controls="clr-namespace:PCClient.Controls"
mc:Ignorable="d"
Loaded="Window_Loaded"
WindowState="Maximized"
Title="MainWindow" Height="480" Width="600">
<Window.DataContext>
<viewModel:MainViewModel></viewModel:MainViewModel>
</Window.DataContext>
<Grid>
<TabControl>
<TabItem Header="数据采集" Padding="5">
<controls:ECGImportControl DataContext="{Binding Children[0]}"/>
</TabItem>
<TabItem Header="数据管理" Padding="5">
<controls:ECGImportRecordsControl DataContext="{Binding Children[1]}"/>
</TabItem>
<TabItem Header="ECG全屏" Padding="5">
<Grid>
<DockPanel LastChildFill="True">
<ScrollBar Orientation="Vertical" DockPanel.Dock="Right"/>
<Canvas Background="AliceBlue"/>
</DockPanel>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
和usercontrol如下:
<UserControl x:Class="PCClient.Controls.ECGImportRecordsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PCClient.Controls"
xmlns:convert="clr-namespace:PCClient.Converters"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
<convert:ConvertIntToString x:Key="convertIntToString"></convert:ConvertIntToString>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Margin="5"/>
<Button Grid.Column="1" Content="查询" Margin="5"/>
</Grid>
<DataGrid Grid.Row="1" Margin="5" ItemsSource="{Binding ImportRecords}" SelectedItem="{Binding SelectedImportRecord,Mode=TwoWay}"
CanUserSortColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" SelectionMode="Extended"
CanUserReorderColumns="False" RowHeaderWidth="0" CanUserAddRows="False" AutoGenerateColumns="False" EnableRowVirtualization="False" GridLinesVisibility="None">
<DataGrid.Columns>
<DataGridTemplateColumn Header="ID" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding PatientID}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="姓名" Width="3*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding PatientName}" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="性别" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Sex,Converter={StaticResource convertIntToString}}" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="硬件ID" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding DeviceID}" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="硬件类型" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding DeviceType}" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="采集开始时间" Width="6*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding StartTime,StringFormat=yyyy-MM-dd HH:mm:ss}" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="采集结束时间" Width="6*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding EndTime,StringFormat=yyyy-MM-dd HH:mm:ss}" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="导入时间" Width="6*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding UploadTime,StringFormat=yyyy-MM-dd HH:mm:ss}" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
答案 0 :(得分:0)
如果您有TabItems
的常数,可以使用IsSelected
这样:
<TabControl>
<TabItem IsSelected="{Binding IsFirstTabSelected,Mode=TwoWay}">
<TextBlock>First Tab</TextBlock>
</TabItem>
<TabItem IsSelected="{Binding IsSecondTabSelected,Mode=TwoWay}">
<TextBlock>Second Tab</TextBlock>
</TabItem>
</TabControl>
您也可以使用Triggers
和VisualStateManager
- 而不是仅将其绑定到ViewModel
。
请确保 - 您获得了正确的活动。或者覆盖&#34;阻止&#34;事件 - 将事件方法中的e.Handled
设置为false
。也许你跟IsHitTestVisible=false
FrameworkElements
一起阻止你的活动。你的问题有点广泛。