【问题标题】:Programmatically Adding Buttons in MVVM but Command doesn't work以编程方式在 MVVM 中添加按钮,但命令不起作用
【发布时间】:2015-06-11 07:16:20
【问题描述】:

我正在尝试动态添加按钮,它可以工作,但命令不起作用。

我遇到了错误

ItemTemplate 和 ItemTemplateSelector 对于已经属于 ItemsControl 容器类型的项目将被忽略; Type='RadRibbonButton'

<telerik:RadRibbonGroup>
 ...
<telerik:RadRibbonGroup.DataContext>
    <vm:Group1/>
</telerik:RadRibbonGroup.DataContext>

        <ItemsControl ItemsSource="{Binding ButtonsCollection}"  >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding}" Command="{Binding DataContext.ButtonCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadRibbonGroup}}}"   />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

</telerik:RadRibbonGroup>



ButtonCommand= new DelegateCommand(ExecuteCommand);

private void ExecuteCommand()
{
     ...
}

【问题讨论】:

    标签: wpf mvvm telerik


    【解决方案1】:
    1. 检查它是否在调试模式下到达命令。
    2. 您的 ButtonsCollection 是依赖属性吗?你会提高它的零钱吗?
    3. 您不应该将实际按钮添加到您的按钮集合中,这不是纯 mvvm。您需要在数据模板中创建按钮,并在集合中创建按钮内容等属性。
    4. 如果您仍然坚持创建实际按钮,我认为模板“”将不起作用。尝试 ”

    【讨论】:

      【解决方案2】:
      1. 达不到
      2. ObservableCollection 按钮(我可以在视图中看到按钮)

      3.我应该在集合运行时创建和添加按钮(这背后有特殊的逻辑)。在构造函数中我创建命令

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-06
        • 1970-01-01
        相关资源
        最近更新 更多