积极答复者
WPF 设置TabIndex无效

问题
-
我自定义一个Button,如下:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="IsTabStop" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Width="auto" Height="auto">
<Image x:Name="image" Margin="0,0,0,0" Source="{Binding ElementName=UserControl2,Path=ImgIsNormal}"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextElement.Foreground="Black" TextElement.FontSize="14" TextElement.FontFamily="SegoeUI" Content="{Binding ElementName=UserControl2,Path=ImgString}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Source" TargetName="image" Value="{Binding ElementName=UserControl2,Path=ImgIsMouseOver}"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Source" TargetName="image" Value="{Binding ElementName=UserControl2,Path=ImgIsPressed}"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Source" TargetName="image" Value="{Binding ElementName=UserControl2,Path=ImgIsMouseOver}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>TabIndex设置无效,还有用Keyboard.Focus(buttonName)无法让button获取焦点,这是什么原因,是不是要在自定义的button中做些处理,请指教
答案
-
这是一个简单的例子:
ImageButton.cs
public class ImageButton : Button { public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(String), typeof(Button)); public static readonly DependencyProperty FocusedImageSourceProperty = DependencyProperty.Register("FocusedImageSource", typeof(String), typeof(Button)); public static readonly DependencyProperty PressedImageSourceProperty = DependencyProperty.Register("PressedImageSource", typeof(String), typeof(Button)); public string ImageSource { set { SetValue(ImageSourceProperty, value); } get { return (string)GetValue(ImageSourceProperty); } } public string FocusedImageSource { set { SetValue(FocusedImageSourceProperty, value); } get { return (string)GetValue(FocusedImageSourceProperty); } } public string PressedImageSource { set { SetValue(PressedImageSourceProperty, value); } get { return (string)GetValue(PressedImageSourceProperty); } } }
在Resources中声明样式:
<Style TargetType="local:ImageButton"> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="BorderThickness" Value="0" /> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Padding" Value="1"/> <Setter Property="IsTabStop" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:ImageButton"> <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"> <Grid> <Image Name="image" Margin="0 0 0 0" Source="{Binding Path=ImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageButton}}" /> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextElement.FontFamily="Courier New" TextElement.FontSize="14" /> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Source" TargetName="image" Value="{Binding Path=FocusedImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageButton}}"/> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter Property="Source" TargetName="image" Value="{Binding Path=PressedImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageButton}}"/> </Trigger> <Trigger Property="IsFocused" Value="true"> <Setter Property="Source" TargetName="image" Value="{Binding Path=FocusedImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageButton}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
和正常Button一样使用:
<local:ImageButton Grid.Row="0" Content="Cancel" ImageSource="/Resources/button_n.png" FocusedImageSource="/Resources/button_o.png" PressedImageSource="/Resources/button_p.png" Click="ImageButton_Click" />
Wanpeng wanpeng.ones@gmail.com
- 已标记为答案 Sheldon _XiaoModerator 2012年9月6日 9:07
全部回复
-
关于Keyboard.Focus你可以看下这个简介:
http://msdn.microsoft.com/en-us/library/aa969768.aspx
里面有关于Keyboard Focus的说明,推荐在Loaded事件中使用Keyboard.Focus.
Wanpeng wanpeng.ones@gmail.com
-
这是一个简单的例子:
ImageButton.cs
public class ImageButton : Button { public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(String), typeof(Button)); public static readonly DependencyProperty FocusedImageSourceProperty = DependencyProperty.Register("FocusedImageSource", typeof(String), typeof(Button)); public static readonly DependencyProperty PressedImageSourceProperty = DependencyProperty.Register("PressedImageSource", typeof(String), typeof(Button)); public string ImageSource { set { SetValue(ImageSourceProperty, value); } get { return (string)GetValue(ImageSourceProperty); } } public string FocusedImageSource { set { SetValue(FocusedImageSourceProperty, value); } get { return (string)GetValue(FocusedImageSourceProperty); } } public string PressedImageSource { set { SetValue(PressedImageSourceProperty, value); } get { return (string)GetValue(PressedImageSourceProperty); } } }
在Resources中声明样式:
<Style TargetType="local:ImageButton"> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="BorderThickness" Value="0" /> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Padding" Value="1"/> <Setter Property="IsTabStop" Value="True" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:ImageButton"> <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"> <Grid> <Image Name="image" Margin="0 0 0 0" Source="{Binding Path=ImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageButton}}" /> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextElement.FontFamily="Courier New" TextElement.FontSize="14" /> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Source" TargetName="image" Value="{Binding Path=FocusedImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageButton}}"/> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter Property="Source" TargetName="image" Value="{Binding Path=PressedImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageButton}}"/> </Trigger> <Trigger Property="IsFocused" Value="true"> <Setter Property="Source" TargetName="image" Value="{Binding Path=FocusedImageSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ImageButton}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
和正常Button一样使用:
<local:ImageButton Grid.Row="0" Content="Cancel" ImageSource="/Resources/button_n.png" FocusedImageSource="/Resources/button_o.png" PressedImageSource="/Resources/button_p.png" Click="ImageButton_Click" />
Wanpeng wanpeng.ones@gmail.com
- 已标记为答案 Sheldon _XiaoModerator 2012年9月6日 9:07