具有直接内容的自定义控件将传递到Commandbar

发布于 2025-02-06 19:46:19 字数 746 浏览 3 评论 0原文

我有一个自定义控件的想法,但我不知道如何实施它。

假设我有一个控制“ mycontrol”,其中有一个命令栏。

<Control ... MyControl>
    <Label Content="Hello" />

    <CommandBar>
        <DefaultButton Command={Binding DCom1} >
        <DefaultButton Command={Binding DCom2} >

        <SomePlaceHolderForAdditionalButtons />
    </CommandBar>
</Control>

我想执行以下操作:

<MyControl>
    <MyAdditionalButton Command={Binding Com1} />
    <MyAdditionalButton Command={Binding Com2} />
</MyControl>

之后,MyControl首先加载了两个默认按钮,而两个按钮作为第三个和第四个按钮。

不是 任何帮助都将受到赞赏。

I have an idea for a custom control, but I don't know how to implement it.

Let's say I have a control "MyControl" with a CommandBar in it.

<Control ... MyControl>
    <Label Content="Hello" />

    <CommandBar>
        <DefaultButton Command={Binding DCom1} >
        <DefaultButton Command={Binding DCom2} >

        <SomePlaceHolderForAdditionalButtons />
    </CommandBar>
</Control>

I want to do the following:

<MyControl>
    <MyAdditionalButton Command={Binding Com1} />
    <MyAdditionalButton Command={Binding Com2} />
</MyControl>

After that, MyControl is loaded with the two default Buttons first and the two provided Buttons as the third and fourth buttons.
I am not an artist. Sorry :-|

Is this even possible? Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2025-02-13 19:46:19

MyControl首先加载两个默认按钮,两个按钮作为第三个和第四个按钮。

您可以使模板控制接近。自定义项目属性ustomcontrol,然后调用UpdateView方法将AppBarbutton Element将其推入Default Commandbar。

例如

[ContentProperty(Name = "Items")]
public class CustomControl : Control
{   
    private const string CommandBarPresenter = "CommandBarPresenter";
    public CustomControl()
    {
        this.DefaultStyleKey = typeof(CustomControl);
        var items = new ObservableCollection<object>();
        items.CollectionChanged += Items_CollectionChanged;
        Items = items;
    }

    private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        UpdateView();
    }

    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public static readonly DependencyProperty ItemTemplateProperty =
    DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(CustomControl), new PropertyMetadata(null, OnItemTemplateChanged));

    private static void OnItemTemplateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        CustomControl target = obj as CustomControl;
        DataTemplate oldValue = (DataTemplate)args.OldValue;
        DataTemplate newValue = (DataTemplate)args.NewValue;
        if (oldValue != newValue)
            target.OnItemTemplateChanged(oldValue, newValue);
    }

    protected virtual void OnItemTemplateChanged(DataTemplate oldValue, DataTemplate newValue)
    {
        UpdateView();
    }

    public ICollection<object> Items
    {
        get;
    }

    private CommandBar _commandBarPresenter;

    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        _commandBarPresenter = GetTemplateChild(CommandBarPresenter) as CommandBar;
        UpdateView();
    }

    private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        UpdateView();
    }

    protected virtual System.Boolean IsItemItsOwnContainerOverride(System.Object item)
    {
        return item is ContentPresenter;
    }


    protected virtual void PrepareICommandElementForItemOverride(DependencyObject element, System.Object item)
    {
        ContentControl contentControl;

        if ((contentControl = element as AppBarButton) != null)
        {
            contentControl.Content = item;
            contentControl.ContentTemplate = ItemTemplate;
        }

    }


    private void UpdateView()
    {

        if (_commandBarPresenter == null)
            return;

        foreach (var item in Items)
        {

            if (item is AppBarButton)
            {
                _commandBarPresenter.SecondaryCommands.Add(item as AppBarButton);
                break;
            }

            if (!(item is AppBarButton))
            {
                throw new Exception("please add AppBarButton as content");
            }

        }
    }
}

XAML代码

<Style TargetType="local:CustomControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CustomControl">
                <StackPanel Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <CommandBar x:Name="CommandBarPresenter">
                        <AppBarButton Icon="Add" Label="Add"/>
                        <AppBarButton Icon="Edit" Label="Edit"/>
                    </CommandBar>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

用法

<local:CustomControl>
    <AppBarButton Icon="AlignCenter" Label="Center"/>
    <AppBarButton Icon="Cut" Label="Cut"/>
</local:CustomControl>

MyControl is loaded with the two default Buttons first and the two provided Buttons as the third and fourth buttons.

You could make template control to approach. Custom Items property for CustomControl, then call UpdateView method to push AppBarButton element into default commandbar.

For example

[ContentProperty(Name = "Items")]
public class CustomControl : Control
{   
    private const string CommandBarPresenter = "CommandBarPresenter";
    public CustomControl()
    {
        this.DefaultStyleKey = typeof(CustomControl);
        var items = new ObservableCollection<object>();
        items.CollectionChanged += Items_CollectionChanged;
        Items = items;
    }

    private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        UpdateView();
    }

    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public static readonly DependencyProperty ItemTemplateProperty =
    DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(CustomControl), new PropertyMetadata(null, OnItemTemplateChanged));

    private static void OnItemTemplateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        CustomControl target = obj as CustomControl;
        DataTemplate oldValue = (DataTemplate)args.OldValue;
        DataTemplate newValue = (DataTemplate)args.NewValue;
        if (oldValue != newValue)
            target.OnItemTemplateChanged(oldValue, newValue);
    }

    protected virtual void OnItemTemplateChanged(DataTemplate oldValue, DataTemplate newValue)
    {
        UpdateView();
    }

    public ICollection<object> Items
    {
        get;
    }

    private CommandBar _commandBarPresenter;

    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        _commandBarPresenter = GetTemplateChild(CommandBarPresenter) as CommandBar;
        UpdateView();
    }

    private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        UpdateView();
    }

    protected virtual System.Boolean IsItemItsOwnContainerOverride(System.Object item)
    {
        return item is ContentPresenter;
    }


    protected virtual void PrepareICommandElementForItemOverride(DependencyObject element, System.Object item)
    {
        ContentControl contentControl;

        if ((contentControl = element as AppBarButton) != null)
        {
            contentControl.Content = item;
            contentControl.ContentTemplate = ItemTemplate;
        }

    }


    private void UpdateView()
    {

        if (_commandBarPresenter == null)
            return;

        foreach (var item in Items)
        {

            if (item is AppBarButton)
            {
                _commandBarPresenter.SecondaryCommands.Add(item as AppBarButton);
                break;
            }

            if (!(item is AppBarButton))
            {
                throw new Exception("please add AppBarButton as content");
            }

        }
    }
}

Xaml Code

<Style TargetType="local:CustomControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CustomControl">
                <StackPanel Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <CommandBar x:Name="CommandBarPresenter">
                        <AppBarButton Icon="Add" Label="Add"/>
                        <AppBarButton Icon="Edit" Label="Edit"/>
                    </CommandBar>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Usage

<local:CustomControl>
    <AppBarButton Icon="AlignCenter" Label="Center"/>
    <AppBarButton Icon="Cut" Label="Cut"/>
</local:CustomControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文