我有一个简单的自定义控件,我有一个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
和Content
绑定似乎正常工作。这只是命令路由似乎无法正常工作。
答案 0 :(得分:0)
您绑定到属性路径SaveButton.Button
,并以TemplatedParent
为源。因此,您的绑定源是您正在模板化的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..}扩展名来缩短语法。
另一件事,为什么你在Button
的模板中有SaveButton
控件。我认为你应该只重新尝试按钮的视觉外观,但行为已经存在,你不需要Button内的Button。您可以简单地使用您正在模板化的Button的Command
属性,不需要此命令路由。
修改强>
试试这个模板
<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}"/>