C# 按钮集合的DependencyProperty

C# 按钮集合的DependencyProperty,c#,wpf,C#,Wpf,我想让用户有机会在CustomControl中设置一组按钮 我尝试使用ItemsControl解决此问题,如下所示: <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type cc:MyCustomControl}}, Path=Buttons}"> <ItemsControl.ItemTemplate> <DataTempl

我想让用户有机会在CustomControl中设置一组按钮

我尝试使用ItemsControl解决此问题,如下所示:

<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type cc:MyCustomControl}}, Path=Buttons}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding Command}">
                <Image Source="{Binding MyImageSource}"/>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
MyButton类:

public class MyButton: Button
{
    public ImageSource MyImageSource { get; set; }
}
以及我希望如何在CustomControl实现中看到它:

<cc:MyCustomControl>
    <cc:MyCustomControl.Buttons>
        <cc:MyButton Command="{Binding SignDocumentsCommand}"
                     MyImageSource="pack://application:,,,/CommonResources;component/Images/Icons/pen.png"/>

        <cc:MyButton Command="{Binding DeleteDocumentsCommand}"
                     MyImageSource="pack://application:,,,/CommonResources;component/Images/Icons/remove.png"/>
    </cc:MyCustomControl.Buttons>
</cc:MyCustomControl>


但它不起作用。在实时可视化树中,我只看到ItemsControl中的MyButtons。这是正确的方法吗?或者我需要用另一种方法解决它?

您用于items控件中按钮项的类型不应派生自Button。您可以使用如下所示的简单方法。也不必将Buttons属性声明为dependency属性。一个简单的可观察收集就足够了

public class MyButtonItem : DependencyObject
{
    public static DependencyProperty CommandProperty = DependencyProperty.Register(
        nameof(Command), typeof(ICommand), typeof(MyButtonItem));

    public static DependencyProperty ImageSourceProperty = DependencyProperty.Register(
        nameof(ImageSource), typeof(ImageSource), typeof(MyButtonItem));

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageSourceProperty); }
        set { SetValue(ImageSourceProperty, value); }
    }
}

public partial class MyCustomControl : UserControl
{
    public MyCustomControl()
    {
        InitializeComponent();
    }

    public ObservableCollection<MyButtonItem> Buttons { get; }
        = new ObservableCollection<MyButtonItem>();
}
公共类MyButtonItem:DependencyObject
{
公共静态DependencyProperty CommandProperty=DependencyProperty.Register(
nameof(命令)、typeof(ICommand)、typeof(MyButtonItem));
公共静态DependencyProperty ImageSourceProperty=DependencyProperty.Register(
名称(图像源)、类型(图像源)、类型(MyButtonItem);
公共ICommand命令
{
获取{return(ICommand)GetValue(CommandProperty);}
set{SetValue(CommandProperty,value);}
}
公共图像源图像源
{
获取{return(ImageSource)GetValue(ImageSourceProperty);}
set{SetValue(ImageSourceProperty,value);}
}
}
公共部分类MyCustomControl:UserControl
{
公共MyCustomControl()
{
初始化组件();
}
公共可观察收集按钮{get;}
=新的ObservableCollection();
}
控件XAML将是您已经拥有的:

<ItemsControl ItemsSource="{Binding Buttons,
                  RelativeSource={RelativeSource AncestorType=UserControl}}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding Command}">
                <Image Source="{Binding ImageSource}"/>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

然后将MyButtonItem对象添加到Buttons集合:

<cc:MyCustomControl>
    <cc:MyCustomControl.Buttons>

        <cc:MyButtonItem
            Command="{Binding SignDocumentsCommand}"
            ImageSource="/CommonResources;component/Images/Icons/pen.png"/>

        <cc:MyButtonItem
            Command="{Binding DeleteDocumentsCommand}"
            ImageSource="/CommonResources;component/Images/Icons/remove.png"/>

    </cc:MyCustomControl.Buttons>
</cc:MyCustomControl>


这显然不起作用,因为每个项目都有两个按钮,一个在ItemTemplate中,另一个作为列表元素传入。是只支持MyButton,还是希望能够在Buttons集合中传递不同的按钮类型?@Clemens,only MyButton还不清楚为什么会有带有ItemsControl的自定义控件。如果你想显示一组按钮,为什么不简单地使用一个常规的面板元素,例如StackPanel?@Clemens我想在代码的一部分用两个按钮实现这个控件,另一部分用四个按钮实现这个控件,等等,但是控件中除了按钮之外还有什么其他东西吗?除了命令,这对我很有帮助。我遇到了一个例外:“不能在“MyButton”类型的“Command”属性上设置“Binding”。只能在DependencyObject的DependencyProperty上设置“Binding”。将命令设置为DependencyProperty没有帮助。抱歉,这当然不行。应该有依赖属性。我已经编辑了答案。
<cc:MyCustomControl>
    <cc:MyCustomControl.Buttons>

        <cc:MyButtonItem
            Command="{Binding SignDocumentsCommand}"
            ImageSource="/CommonResources;component/Images/Icons/pen.png"/>

        <cc:MyButtonItem
            Command="{Binding DeleteDocumentsCommand}"
            ImageSource="/CommonResources;component/Images/Icons/remove.png"/>

    </cc:MyCustomControl.Buttons>
</cc:MyCustomControl>