C# 绑定到继承的按钮自定义控件上的命令属性
我创建了一个名为C# 绑定到继承的按钮自定义控件上的命令属性,c#,wpf,binding,C#,Wpf,Binding,我创建了一个名为ActionButton的自定义控件,该控件继承自Button。我添加了两个依赖属性,它们工作得很好,但是,我无法使与Command属性的绑定工作。当应用程序运行时,Command属性始终返回null 有人能告诉我我做错了什么吗 下面是一些代码,我希望这些代码应该足够了 // Custom control public class ActionButton : Button { static ActionButton() { DefaultStyl
ActionButton
的自定义控件,该控件继承自Button
。我添加了两个依赖属性,它们工作得很好,但是,我无法使与Command属性的绑定工作。当应用程序运行时,Command属性始终返回null
有人能告诉我我做错了什么吗
下面是一些代码,我希望这些代码应该足够了
// Custom control
public class ActionButton : Button
{
static ActionButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ActionButton), new FrameworkPropertyMetadata(typeof(ActionButton)));
}
// Some dependency properties go here
}
// In Generic.xaml
<Style TargetType="{x:Type controls:ActionButton}">
<Setter Property="Width" Value="48"/>
<Setter Property="Height" Value="48"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:ActionButton}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Border.ToolTip>
<StackPanel Margin="{DynamicResource ToolTipMargin}" MaxWidth="{DynamicResource MaxToolTipWidth}">
<TextBlock Style="{DynamicResource ToolTipHeaderStyle}" Text="{TemplateBinding ToolTipHeader}"/>
<Separator Visibility="Hidden"/>
<TextBlock Style="{DynamicResource ToolTipContentStyle}" Text="{TemplateBinding ToolTipText}"/>
</StackPanel>
</Border.ToolTip>
<Grid>
<Ellipse x:Name="BackEllipse" Stroke="{DynamicResource MahApps.Brushes.Accent}" StrokeThickness="0" Fill="{DynamicResource MahApps.Brushes.Accent}"/>
<Ellipse x:Name="FillEllipse" Stroke="{DynamicResource MahApps.Brushes.Accent}" StrokeThickness="3"/>
<TextBlock x:Name="BlockIconTextBox" Text="{TemplateBinding Icon, Converter={StaticResource FontIconConverter}}" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="BackEllipse" Property="Opacity" Value="0.6"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="BackEllipse" Property="Opacity" Value="0.0"/>
<Setter TargetName="BlockIconTextBox" Property="Opacity" Value="0.6"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="FillEllipse" Property="Opacity" Value="0.3"/>
<Setter TargetName="BlockIconTextBox" Property="Opacity" Value="0.3"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
// View base class...
public class View : UserControl
{
public View()
{
ActionButtons = new ObservableCollection<ActionButton>();
}
public static readonly DependencyProperty ActionButtonsProperty = DependencyProperty.Register(nameof(ActionButtons), typeof(ObservableCollection<ActionButton>), typeof(View));
public ObservableCollection<ActionButton> ActionButtons
{
get => (ObservableCollection<ActionButton>)GetValue(ActionButtonsProperty);
set => SetValue(ActionButtonsProperty, value);
}
}
// Markup in a view...
<local:View
x:Class="Vesuvius.TeleCalc.Windows.Views.SettingsView"
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:Vesuvius.TeleCalc.Windows.Views"
xmlns:viewModels="clr-namespace:Vesuvius.TeleCalc.Windows.ViewModels"
xmlns:controls="clr-namespace:Vesuvius.TeleCalc.Windows.Controls"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="1024"
Title="Settings"
>
<local:View.DataContext>
<viewModels:SettingsViewModel />
</local:View.DataContext>
<local:View.ActionButtons >
<!-- This is where things start to go wrong (I think) -->
<controls:ActionButton Icon="Color" ToolTipHeader="Reset theme" ToolTipText="Reset theme to default values." Command="{Binding ResetThemeCommand}"/>
</local:View.ActionButtons>
<!-- I have removed the rest for brevity -->
// In SettingsViewModel...
public SettingsViewModel()
{
ResetThemeCommand = CommandFactory.Create(ResetTheme);
}
public ICommand ResetThemeCommand { get; }
private void ResetTheme(object parameter)
{
// Do stuff here
}
// The issue...
public partial class SettingsView
{
public SettingsView()
{
InitializeComponent();
// The Command of SettingsViewModel.ResetThemeCommand is always null, so I have to resort to this nasty hack...
ActionButtons[0].Command = ((SettingsViewModel)DataContext).ResetThemeCommand;
// It's also worth noting, that the dependency properties ToolTipHeader and ToolTipText of the ResetThemeCommand are working properly.
}
}
//自定义控件
公共类ActionButton:按钮
{
静态操作按钮()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ActionButton)),new FrameworkPropertyMetadata(typeof(ActionButton));
}
//这里有一些依赖属性
}
//在Generic.xaml中
//查看基类。。。
公共类视图:UserControl
{
公众观点()
{
ActionButtons=新的ObservableCollection();
}
public static readonly dependencProperty actionbuttonproperty=dependencProperty.Register(操作按钮的名称)、可观察集合的类型、视图的类型);
公共可观察收集操作按钮
{
get=>(ObservableCollection)GetValue(ActionButtonProperty);
set=>SetValue(actionButtonProperty,value);
}
}
//视图中的标记。。。
//在SettingsViewModel中。。。
公共设置视图模型()
{
ResetThemeCommand=CommandFactory.Create(ResetTheme);
}
公用ICommand重置ECommand{get;}
私有void重置主题(对象参数)
{
//在这里做事
}
//这个问题。。。
公共部分类设置视图
{
公共设置视图()
{
初始化组件();
//SettingsViewModel.ResetThemeCommand的命令总是空的,所以我不得不求助于这个讨厌的黑客。。。
ActionButtons[0]。命令=((SettingsViewModel)数据上下文);
//还值得注意的是,ResetThemeCommand的依赖属性ToolTiHeader和ToolTipText工作正常。
}
}
似乎只将ActionButton添加到视图的ActionButtons
集合属性中,而不添加到任何视觉或逻辑树中。因此,它将不会继承DataContext属性值,并且基于DataContext的绑定(如{Binding resethemecommand}
将不起作用。谢谢@Clemens,我已经根据您的评论使ActionButtons起作用了。