[英]WPF C# Commands bound to userControl won't fire
So, I have created a UserControl which really is an "advanced button". 因此,我创建了一个UserControl,它实际上是一个“高级按钮”。 I have implemented Dependency Properties, one of which is ICommand
, that is supposed to be bindable further when control is used in an actual Window. 我已经实现了依赖项属性,其中之一是ICommand
,当在实际的Window中使用控件时,应该可以进一步绑定。
However, for some reason the Command doesn't work. 但是,由于某些原因,该命令不起作用。
When I tried an exact same approach on a regular button, everything worked fine (thus it's not the fault of my DelegateCommand implementation or my ViewModel). 当我在常规按钮上尝试完全相同的方法时,一切工作正常(因此,这不是我的DelegateCommand实现或ViewModel的错)。
I tried to followup on why the bound command doesn't fire, but I couldn't find a reliable reason for it not to. 我试图跟进为什么绑定命令不会触发,但是我找不到一个可靠的理由。
Here is my UserControl XAML: 这是我的UserControl XAML:
<Window x:Class="NoContact.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NoContact"
mc:Ignorable="d"
xmlns:userControls="clr-namespace:NoContact.UserControls"
Title="MainWindow" Height="800" Width="960" Background="#22282a">
<Border BorderThickness="1" BorderBrush="#ffcd22" Margin="10,10,10,10">
<Grid>
<Button HorizontalContentAlignment="Stretch" Foreground="{Binding ElementName=ImageButtonUC, Path=Foreground}"
Background="{Binding ElementName=ImageButtonUC, Path=Background}">
<DockPanel Width="{Binding ElementName=ImageButtonUC, Path=ActualWidth}">
<Image Source="{Binding ElementName=ImageButtonUC, Path=Image}" DockPanel.Dock="Left"
Height="{Binding ElementName=ImageButtonUC, Path=ActualHeight, Converter={StaticResource HeightConverter}}"
Width="{Binding ElementName=ImageButtonUC, Path=ActualWidth, Converter={StaticResource HeightConverter}}" VerticalAlignment="Center"/>
<TextBlock Text="{Binding ElementName=ImageButtonUC, Path=Text}" FontSize="17" VerticalAlignment="Center" />
</DockPanel>
</Button>
</Grid>
<UserControl.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
And here is my UserControl code-behind: 这是我的UserControl背后的代码:
public partial class ImageButton : UserControl
{
// OTHER IRRELEVANT CLASS PARAMETERS ARE HERE
public ICommand ClickCommand
{
get { return (ICommand)GetValue(ClickCommandProperty); }
set { SetValue(ClickCommandProperty, value); }
}
// OTHER IRRELEVANT DEPENDENCY PROPERTIES ARE HERE
public static DependencyProperty ClickCommandProperty =
DependencyProperty.Register("ClickCommand", typeof(ICommand), typeof(ImageButton));
public ImageButton()
{
InitializeComponent();
}
}
Finally, this is how I use my control: 最后,这就是我使用控件的方式:
<userControls:ImageButton x:Name="phoneButton" ClickCommand="{Binding Path=MyButtonClickCommand}" Style="{StaticResource phoneImageButtonUCStyle}" Text="Telefon" Width="200" Height="100" VerticalAlignment="Top" />
The DataContext of the control is set on a stackpanel, that is wrapped around my control. 控件的DataContext是在堆栈面板上设置的,该面板包装在我的控件周围。 I have also tried setting it directly on the control, no effect. 我也尝试过直接在控件上设置它,没有效果。
Again, doing the same on a regular button works just fine. 同样,在常规按钮上执行相同操作也很好。
I have finally solved the problem - and it was pretty trivial. 我终于解决了这个问题-这很简单。
I had ommited the most important binding - UserControl's button Command to UserControl's Dependency property. 我省略了最重要的绑定-UserControl的按钮Command到UserControl的Dependency属性。
Changing it like this made it work: 像这样更改它可以正常工作:
<Button HorizontalContentAlignment="Stretch" Foreground="{Binding ElementName=ImageButtonUC, Path=Foreground}"
Background="{Binding ElementName=ImageButtonUC, Path=Background}"
Command="{Binding ElementName=ImageButtonUC, Path=ClickCommand}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.