按钮命令未开火



我有一个奇怪的情况。我为WinRT开发一个应用程序,并在命令绑定方面存在一些问题。这是XAML的一部分:

    <control:ItemsHub ItemsSource="{Binding Categories}">
        <control:ItemsHub.SectionHeaderTemplate>
            <DataTemplate>
                <Button Command="{Binding CategoryNavigationCommand}" Margin="5,0,0,10" Content="{Binding Header}"/>
            </DataTemplate>
        </control:ItemsHub.SectionHeaderTemplate>
    </control:ItemsHub>

这是我的ViewModel:

    public CategorySectionViewModel(IRecipeService recipeService, INavigationService navigationService, RecipeTreeItemDto treeItem)
    {
        ...
        CategoryNavigationCommand = new DelegateCommand(NavigateToCategory);
        ...
    }

    private string _header;
    public string Header
    {
        get { return _header; }
        set { SetProperty(ref _header, value); }
    }
    public DelegateCommand CategoryNavigationCommand { get; private set; }

    private void NavigateToCategory()
    {
        _navigationService.Navigate("CategoryHub", _recipeTreeItemDto.NodePath);
    }

我在输出窗口中没有任何绑定错误,并且"标头"也显示在按钮中。但是命令不会被解雇,我点击它!我做错了什么?

也许是因为我创建了一个自定义HubControl。使用此控件,我可以为枢纽标题和轮毂符合体附加一个项目库和ItemTemplate。也许是因为这种绑定丢失了?

这是我的自定义集线器控件:

public class ItemsHub : Hub
{
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }
    public IList ItemsSource
    {
        get { return (IList)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }
    public HubSection SpotlightSection
    {
        get { return (HubSection)GetValue(SpotlightSectionProperty); }
        set { SetValue(SpotlightSectionProperty, value); }
    }
    public DataTemplate SectionHeaderTemplate
    {
        get { return (DataTemplate)GetValue(SectionHeaderTemplateProperty); }
        set { SetValue(SectionHeaderTemplateProperty, value); }
    }
    public static readonly DependencyProperty ItemTemplateProperty =
        DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ItemsHub), new PropertyMetadata(null, ItemTemplateChanged));
    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IList), typeof(ItemsHub), new PropertyMetadata(null, ItemsSourceChanged));
    public static readonly DependencyProperty SpotlightSectionProperty =
        DependencyProperty.Register("SpotlightSection", typeof(HubSection), typeof(ItemsHub), new PropertyMetadata(default(HubSection), SpotlightSectionChanged));
    public static readonly DependencyProperty SectionHeaderTemplateProperty =
        DependencyProperty.Register("SectionHeaderTemplate", typeof(DataTemplate), typeof(ItemsHub), new PropertyMetadata(default(DataTemplate), HeaderTemplateChanged));
    private static void SpotlightSectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ItemsHub hub = d as ItemsHub;
        if (hub != null)
        {
            bool hubContainsSpotLight = hub.Sections.Contains(hub.SpotlightSection);
            if (hub.SpotlightSection != null && !hubContainsSpotLight)
            {
                hub.Sections.Add(hub.SpotlightSection);
            }
            if (hub.SpotlightSection == null && hubContainsSpotLight)
            {
                hub.Sections.Remove(hub.SpotlightSection);
            }
        }
    }
    private static void HeaderTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ItemsHub hub = d as ItemsHub;
        if (hub != null)
        {
            DataTemplate template = e.NewValue as DataTemplate;
            if (template != null)
            {
                // Apply template
                foreach (var section in hub.Sections.Except(new List<HubSection> { hub.SpotlightSection }))
                {
                    section.HeaderTemplate = template;
                }
            }
        }
    }
    private static void ItemTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ItemsHub hub = d as ItemsHub;
        if (hub != null)
        {
            DataTemplate template = e.NewValue as DataTemplate;
            if (template != null)
            {
                // Apply template
                foreach (var section in hub.Sections.Except(new List<HubSection> { hub.SpotlightSection }))
                {
                    section.ContentTemplate = template;
                }
            }
        }
    }
    private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ItemsHub hub = d as ItemsHub;
        if (hub != null)
        {
            IList items = e.NewValue as IList;
            if (items != null)
            {
                var spotLightSection = hub.SpotlightSection;
                hub.Sections.Clear();
                if (spotLightSection != null)
                {
                    hub.Sections.Add(spotLightSection);
                }
                foreach (var item in items)
                {
                    HubSection section = new HubSection();
                    section.DataContext = item;
                    section.Header = item;
                    DataTemplate headerTemplate = hub.SectionHeaderTemplate;
                    section.HeaderTemplate = headerTemplate;
                    DataTemplate contentTemplate = hub.ItemTemplate;
                    section.ContentTemplate = contentTemplate;
                    hub.Sections.Add(section);
                }
            }
        }
    }
}

感谢您的帮助!

我找到了解决方案!我不知道为什么,但是默认情况下,集线器控件的截面标题不是交互式!因此,当我在自定义itemHub-control中分配项目库时,我必须将交互性设置为true。

这是我在ItemShub控件中更改的内容:

    private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ItemsHub hub = d as ItemsHub;
        if (hub != null)
        {
            IList items = e.NewValue as IList;
            if (items != null)
            {
                var spotLightSection = hub.SpotlightSection;
                hub.Sections.Clear();
                if (spotLightSection != null)
                {
                    hub.Sections.Add(spotLightSection);
                }
                foreach (var item in items)
                {
                    HubSection section = new HubSection();
                    DataTemplate headerTemplate = hub.SectionHeaderTemplate;
                    section.HeaderTemplate = headerTemplate;
                    DataTemplate contentTemplate = hub.ItemTemplate;
                    section.ContentTemplate = contentTemplate;
                    section.DataContext = item;
                    section.Header = item;
                    //This line fixed my problem.
                    section.IsHeaderInteractive = true;
                    hub.Sections.Add(section);
                }
            }
        }

您可以使用以下绑定表达式

command =" {binding categoryNavigationCommand,reactricesource = {reactractiOrce ancestortype = xyx}}}"

和xyz是一种控制类型

最新更新