我有一个简单的自定义控件ControlTemplate
。我似乎无法从Button
控件上获取命令以路由到其TemplatedParent
.
<Style x:Key="SaveButtonStyle" TargetType="{x:Type Controls:SaveButton}">
<Style.Resources>
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:SaveButton}">
<DockPanel Width="65" Height="21" LastChildFill="True">
<Canvas DockPanel.Dock="Left" HorizontalAlignment="Right" Margin="0 0 -49 0" ZIndex="1" IsHitTestVisible="False" >
<Image Source="Images/green-check.png" Width="16" Height="16" Visibility="{Binding IsDirty, Converter={StaticResource BoolToVisibilityConverter}}" Canvas.Top="-3" RenderOptions.BitmapScalingMode="Fant" />
</Canvas>
<Button DockPanel.Dock="Left" HorizontalAlignment="Left" Width="40" Height="21" FontSize="10"
Content="{Binding SaveButton.Content, RelativeSource={RelativeSource TemplatedParent}, FallbackValue={x:Static Localization:Strings.Save}}"
Command="{Binding SaveButton.Command, RelativeSource={RelativeSource TemplatedParent}}"
CommandParameter="{Binding SaveButton.CommandParameter, RelativeSource={RelativeSource TemplatedParent}}" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
和这个:
public class SaveButton : Button {
public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register("IsDirty", typeof(bool), typeof(SaveButton));
static SaveButton() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(SaveButton), new FrameworkPropertyMetadata(typeof(SaveButton)));
}
[Bindable(true), Category("Action")]
[Localizability(LocalizationCategory.NeverLocalize)]
public bool IsDirty {
get { return (bool)GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
}
和(最后):
<Controls:SaveButton Command="{Binding Save}" IsDirty="{Binding IsDirty}" Style="{DynamicResource SaveButtonStyle}"/>
有趣的部分是IsDirty
andContent
绑定似乎工作正常。只是命令路由似乎不起作用。