1

私が持っている単純なカスタム コントロールがあり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}"/>

興味深い点は、IsDirtyContentバインディングが適切に機能しているように見えることです。機能していないように見えるのは、コマンドルーティングだけです。

4

1 に答える 1