问题描述:b和c的content绑定了a的content,运行时,当点击内容区域时,当前选项卡会变到c,请问怎么才能不变呢?
最简代码如下:
<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:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TabControl Margin="20" ClipToBounds="False">
<TabItem x:Name="a" Header="a" Height="20" Padding="5">
<Grid Background="Gray">
</Grid>
</TabItem>
<TabItem Header="b" Content="{Binding ElementName=a,Path=Content,Mode=OneTime}">
</TabItem>
<TabItem Header="c" Content="{Binding ElementName=a,Path=Content,Mode=OneTime}">
</TabItem>
</TabControl>
</Grid>
</Window>
我自己解决了,记录一下。其实很简单,只要tabitem中的Grid添加PreviewMouseDown事件,在事件中中断事件路由就OK了。下面是示例:
private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.Source is TouchableScrollViewer) return;//这里是我不需要中断事件路由的控件
e.Handled = true;//中断事件路由
}