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}"/>

有趣的部分是IsDirtyandContent绑定似乎工作正常。只是命令路由似乎不起作用。

4

1 回答 1

0

您正在绑定到属性路径SaveButton.Button,并使用TemplatedParentas 源。因此,您的绑定源是您正在模板化的 Button,但Button不包含SaveButton属性。

尝试在没有SaveButton属性路径的情况下配置绑定,如下所示:

  <Button DockPanel.Dock="Left" HorizontalAlignment="Left" Width="40" Height="21" FontSize="10"
          Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, FallbackValue={x:Static Localization:Strings.Save}}" 
          Command="{Binding Command, RelativeSource={RelativeSource TemplatedParent}}"
          CommandParameter="{Binding CommandParameter, RelativeSource={RelativeSource TemplatedParent}}" />

此外,您可以使用{TemplateBinding..}扩展来缩短语法。

另一件事,为什么您甚至可以ButtonSaveButton. 我认为您应该只重新设计按钮的视觉外观,但行为已经存在,您不需要按钮内的按钮。您可以简单地使用Command您正在模板化的 Button 的属性,不需要此命令路由。

编辑:

试试这个模板

<Style x:Key="SaveButtonStyle" TargetType="{x:Type Controls:SaveButton}">
<Style.Resources>
    <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</Style.Resources>
<Setter Property="Content" Value="Save"/>
<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>
                <ContentPresenter DockPanel.Dock="Left"/>                  
            </DockPanel>
        </ControlTemplate>
    </Setter.Value>
</Setter>

按钮的内容(“保存”)在样式设置器中设置。如果你想要一些其他的内容,你可以像这样使用你的按钮:

<Controls:SaveButton Content="Something" Command="{Binding Save}" IsDirty="{Binding IsDirty}" Style="{DynamicResource SaveButtonStyle}"/>
于 2013-05-28T17:59:08.307 回答