我试过了:
<UniformGrid DockPanel.Dock="Top" Columns="2" Rows="1">
<UniformGrid.Resources>
<Style TargetType="RadioButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<TabItem IsSelected="{TemplateBinding IsChecked}">
<TabItem.Header>
<ContentPresenter Margin="5" />
</TabItem.Header>
</TabItem>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UniformGrid.Resources>
<RadioButton Content="By company name" />
<RadioButton Content="By contact name" Grid.Column="1"/>
</UniformGrid>
除了没有选择/取消选择合适的标签外,它确实有效 我当然更喜欢RadioButtons,但我的客户坚持认为它应该是Tabs。
答案 0 :(得分:1)
我认为问题是TabItem正在“吃掉”鼠标点击,因此RadioButton永远不会被选中/取消选中。您可以将TabItem包装在Border中并设置IsHitTestVisible="False"
。执行此操作您将丢失IsMouseOver触发器等,因此您必须从默认模板重新添加它们
尝试这样的事情
<LinearGradientBrush x:Key="TabItemHotBackground" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#EAF6FD" Offset="0.15"/>
<GradientStop Color="#D9F0FC" Offset=".5"/>
<GradientStop Color="#BEE6FD" Offset=".5"/>
<GradientStop Color="#A7D9F5" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="TabItemSelectedBackground" Color="#F9F9F9"/>
<SolidColorBrush x:Key="TabItemHotBorderBrush" Color="#3C7FB1"/>
<Style x:Key="RadioButtonStyle1" TargetType="{x:Type RadioButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Border BorderBrush="Transparent" BorderThickness="0" Background="Transparent" CornerRadius="0">
<TabItem x:Name="tabItem" IsSelected="{TemplateBinding IsChecked}" IsHitTestVisible="False">
<TabItem.Header>
<ContentPresenter Margin="5"/>
</TabItem.Header>
</TabItem>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="tabItem" Value="{StaticResource TabItemHotBackground}"/>
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Panel.ZIndex" Value="1"/>
<Setter Property="Background" TargetName="tabItem" Value="{StaticResource TabItemSelectedBackground}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="false"/>
<Condition Property="IsMouseOver" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" TargetName="tabItem" Value="{StaticResource TabItemHotBorderBrush}"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
更新
<ControlTemplate TargetType="RadioButton">
<Border BorderBrush="Transparent" BorderThickness="0" Background="Transparent" CornerRadius="0">
<TabItem x:Name="tabItem" IsSelected="{TemplateBinding IsChecked}" IsHitTestVisible="False">
<TabItem.Header>
<ContentPresenter Margin="5"/>
</TabItem.Header>
</TabItem>
</Border>
</ControlTemplate>
更新2
单击TabItem时,您可以使用附加行为在RadioButton上引发MouseDown事件。或者,你可以设置IsChecked =“True”而不是提高点击事件
<ControlTemplate TargetType="RadioButton">
<TabItem x:Name="tabItem" IsSelected="{TemplateBinding IsChecked}"
local:LinkWithRadioButtonBehavior.LinkWithRadioButton="True">
<TabItem.Header>
<ContentPresenter Margin="5"/>
</TabItem.Header>
</TabItem>
</ControlTemplate>
<强> LinkWithRadioButtonBehavior 强>
public static class LinkWithRadioButtonBehavior
{
public static readonly DependencyProperty LinkWithRadioButtonProperty =
DependencyProperty.RegisterAttached
(
"LinkWithRadioButton",
typeof(bool),
typeof(LinkWithRadioButtonBehavior),
new UIPropertyMetadata(false, OnLinkWithRadioButtonPropertyChanged)
);
public static bool GetLinkWithRadioButton(DependencyObject obj)
{
return (bool)obj.GetValue(LinkWithRadioButtonProperty);
}
public static void SetLinkWithRadioButton(DependencyObject obj, bool value)
{
obj.SetValue(LinkWithRadioButtonProperty, value);
}
private static void OnLinkWithRadioButtonPropertyChanged(DependencyObject dpo,
DependencyPropertyChangedEventArgs e)
{
Control control = dpo as Control;
if (control != null)
{
if ((bool)e.NewValue == true)
{
control.PreviewMouseDown += OnMouseDown;
}
else
{
control.PreviewMouseDown -= OnMouseDown;
}
}
}
static void OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Control control = sender as Control;
MouseButtonEventArgs routedEventArgs = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left);
routedEventArgs.RoutedEvent = RadioButton.MouseDownEvent;
routedEventArgs.Source = control;
RadioButton clickedRadioButton = GetVisualParent<RadioButton>(control);
clickedRadioButton.RaiseEvent(routedEventArgs);
}
public static T GetVisualParent<T>(object childObject) where T : Visual
{
DependencyObject child = childObject as DependencyObject;
while ((child != null) && !(child is T))
{
child = VisualTreeHelper.GetParent(child);
}
return child as T;
}
}
答案 1 :(得分:0)
您可以将radiobuttons设置为ToggleButtons样式,并创建分配给“Click”事件Blend Interaction Triggers的互斥状态。这是一种痛苦,但比你接受的答案要少得多。希望它可以帮助某人