我创建了某种方形按钮作为CustomControl,带有绑定和东西,以及#34;重新获得一些编程技能" (学习目的)。
现在我遇到了我的自定义命令不会被解雇的问题。在我检查完所有内容后,我发现我直接测试了Click事件,但即便点击事件本身也不会被解雇。
自定义控制:
<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 =用于测试按钮的命令)
答案 0 :(得分:0)
这是我的错 - 我实现了一些代码使我的Window可以拖动 - 现在我知道这个方法正在吃MouseDown事件。