我是WPF新手。我在controltemplate中自定义一个toogle按钮,它有一个命令。我想将命令点击事件映射到我的自定义控件。
<Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Viewbox Stretch="Fill">
<Grid Width="150" Height="100">
<Grid.Background>
<SolidColorBrush Color="{Binding RelativeSource ={RelativeSource TemplatedParent},Path=PrimaryColor}"/>
</Grid.Background>
<TextBlock Text="+" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="64" FontWeight="Bold" Foreground="LightGray"/>
<Grid Margin="0" Width="150" Height="100">
<Grid.Background>
<LinearGradientBrush StartPoint="0.462063,0.00227975" EndPoint="0.462063,1.00111">
<LinearGradientBrush.GradientStops>
<GradientStop Color="#66007099" Offset="0"/>
<GradientStop Color="#4D0079AC" Offset="0.131072"/>
<GradientStop Color="#350082C0" Offset="0.367442"/>
<GradientStop Color="#030094E8" Offset="0.990952"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Grid.Background>
<Grid.Effect>
<DropShadowEffect BlurRadius="38" ShadowDepth="11.3386" Opacity="0.6" Color="#FF000000" Direction="270"/>
</Grid.Effect>
</Grid>
</Grid>
</Viewbox>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Grid Margin="0">
<Button Style="{StaticResource CloseButton}" Command="{x:Static my:ContainerButton.CloseCommand}" Height="20" Width="20" HorizontalAlignment="Right" VerticalAlignment="Top" Visibility="{Binding RelativeSource ={RelativeSource TemplatedParent},Path=ShowToolbar}" />
</Grid>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
那是我的xaml模板,
public class ContainerButton : ToggleButton{
public ContainerButton() {
closeCommand = new RoutedCommand("CloseCommand", typeof(ContainerButton));
var binding = new CommandBinding() { Command = closeCommand };
binding.Executed += OnCloseEvent;
CommandBindings.Add(binding);
}
private void OnCloseEvent(object sender, ExecutedRoutedEventArgs e) {
Content = null;
ShowToolbar = System.Windows.Visibility.Hidden;
}
public static readonly DependencyProperty CloseCommandProperty =
DependencyProperty.Register("CloseCommand", typeof(RoutedCommand), typeof(ContainerButton));
private static RoutedCommand closeCommand;
public static RoutedCommand CloseCommand {
get {
return closeCommand;
}
}}
我使用了像
这样的控件<local:ContainerButton x:Name="xToggle"
Style="{DynamicResource ToggleButtonStyle}"
PrimaryColor="Transparent"
Margin="252,222,82,34" />
当我构建项目并运行应用程序时,它运行良好。当我在visual studio 2010中切换到xaml设计视图时出现异常, Visual studio抛出一个
System.Windows.Markup.StaticExtension.Member Exception。
我在设计模式下删除了Command="{x:Static my:ContainerButton.CloseCommand}"
自定义控件显示。我不知道它有什么问题?