【发布时间】:2016-01-18 13:57:52
【问题描述】:
我创建了某种方形按钮作为带有绑定和“重新获得一些编程技能”的东西的 CustomControl(学习目的)。
现在我遇到了我的自定义命令不会被触发的问题。在我检查了所有我能弄清楚的东西之后,我直接测试了点击事件,但即使点击事件本身也不会被触发。
自定义控件:
<UserControl x:Class="SGDB.Controls.QuadButton"
x:Name="QButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SGDB.Controls"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="100" Margin="2,2,2,2">
<UserControl.Resources>
<Style TargetType="Button">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#003264" Offset="0.25"/>
<GradientStop Color="#004896" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#FFCD9B" Offset="0.25"/>
<GradientStop Color="#FFB769" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontSize" Value="14"/>
<Setter Property="FontWeight" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsMouseOver}" Value="True">
<Setter Property="Foreground">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#411902" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Border BorderThickness="1" BorderBrush="White">
<Button Width="{Binding Size, ElementName=QButton}" Height="{Binding Size, ElementName=QButton}" Command="{Binding ExecuteCommand, ElementName=QButton}" Click="Button_Click">
<StackPanel Margin="10">
<Image Source="{Binding ImageSource, ElementName=QButton}" Width="64" Height="64" HorizontalAlignment="Center" VerticalAlignment="Top"/>
<TextBlock Text="{Binding DisplayText, ElementName=QButton}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,15,0,0"/>
</StackPanel>
</Button>
</Border>
代码隐藏:
public partial class QuadButton : UserControl {
public ICommand ExecuteCommand {
get { return (ICommand)GetValue(ExecuteCommandProperty); }
set { SetValue(ExecuteCommandProperty, value); }
}
// Using a DependencyProperty as the backing store for ExecuteCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ExecuteCommandProperty =
DependencyProperty.Register("ExecuteCommand", typeof(ICommand), typeof(QuadButton), new PropertyMetadata(null));
public int Size {
get { return (int)GetValue(SizeProperty); }
set { SetValue(SizeProperty, value); }
}
// Using a DependencyProperty as the backing store for Size. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SizeProperty =
DependencyProperty.Register("Size", typeof(int), typeof(QuadButton), new PropertyMetadata(null));
public string ImageSource {
get { return (string)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
// Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(string), typeof(QuadButton), new PropertyMetadata(null));
public string DisplayText {
get { return (string)GetValue(DisplayTextProperty); }
set { SetValue(DisplayTextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisplayTextProperty =
DependencyProperty.Register("DisplayText", typeof(string), typeof(QuadButton), new PropertyMetadata(null));
public QuadButton() {
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
MessageBox.Show("Test");
}
}
视图中使用自定义按钮的命令:
<custom:QuadButton Size="200" DisplayText="Administration" ImageSource="../Pictures/Users.png" ExecuteCommand="{Binding VM.TestButton}"/>
(VM = ViewModel,TestButton = 按钮测试命令)
【问题讨论】:
-
当您尝试在同一个按钮上使用
Command和Click事件时,会发生一些奇怪的事情。 -
我尝试使用您的代码重现您的问题,但我无法做到。您的 QuadButton 对我有用(它会引发 click 事件,然后执行我的命令)。为什么你认为没有引发点击事件?
-
当我点击我的任何按钮时,什么都没有发生 - 甚至没有点击动画或我的消息框弹出......
标签: c# wpf xaml command custom-controls