- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 WPF 的新手,我正在尝试在 ContentControl 中动态添加一个按钮,它应该在单击时触发一个命令。我正在使用 MVVMLight 来处理命令。
下面我有一个有两个按钮的例子。顶部按钮直接放置在 StackPanel 中。此按钮按预期触发命令。
第二个按钮放置在 ContentControl 中。它显示正确,但单击按钮时命令不会触发。我假设这是因为 Binding 不会通过 DataTemplate 向下传输,但如果我使用常规命令而不是 MVVMLight RelayCommand,它似乎可以工作。
我不想删除框架,所以我想知道是否有人知道如何修复它?谢谢
<Window x:Class="ContentControlExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ContentControlExample.ViewModel">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="MyButton" >
<Button Content="SUBMIT" Command="{Binding MyCommand}" Width="200" Height="50"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<!--When this button is clicked, the Command executes as expected-->
<Button Content="SUBMIT" Command="{Binding MyCommand}" Width="200" Height="50"/>
<!--Nothing happens when this button is clicked-->
<ContentControl ContentTemplate="{StaticResource MyButton}"/>
</StackPanel>
</Window>
这是带有命令的 ViewModel:
public class MainViewModel : ViewModelBase
{
public ICommand MyCommand { get; private set; }
public MainViewModel()
{
MyCommand = new RelayCommand(MyCommand_Executed, MyCommand_CanExecute);
}
private bool MyCommand_CanExecute()
{
return true;
}
private void MyCommand_Executed()
{
MessageBox.Show("The command executed");
}
}
最佳答案
这里的问题是 ContentTemplate
中的隐式 DataContext 是 Content
并且尚未设置任何内容。您需要将 Content
设置为一些 Binding 以桥接当前在可视化树中的 DataContext
,如下所示:
<ContentControl ContentTemplate="{StaticResource MyButton}" Content="{Binding}"/>
关于c# - 从 ContentControl 中的按钮触发命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33271813/
在我们的 VSTO Word 2010 插件中,我们试图在给定的其他 ContentControl 之后插入一个 RichTextControl。我们试过这个: public ContentC
ContentControl.Template 和 ContentControl.ContentTemplate 有什么区别?我什么时候使用哪个? 例如,我可以在 WPF 的 xaml 文件中编写:
我有一个 ViewModel,它表示多个选项并实现 IDataErrorInfo。仅当至少选择了这些选项之一时,此 ViewModel 才有效。它绑定(bind)到 ContentControl。 D
我已经用这个把头撞在墙上有一段时间了。 我有一个从 ContentControl 派生的自定义控件 - 它完美地工作,除了它不会以声明方式或以编程方式调整大小以适应其父级。 自定义控件以(最终)内容呈
以下代码位于 MEF MVVM 桌面应用程序中。 Shell 已经构建,但正在初始化一个模块。 模块想要将文本添加到 Shell 中的 ContentControl,但以下代码不会导致在 Shell
我正在尝试创建自定义工具提示控件。该控件继承自ToolTip类。我的自定义工具提示将有一个标题和一个内容区域。内容可以是普通文本或任何其他内容(图像、richtextbox 等)。以下是自定义工具提示
我有一个自定义 WPF 控件。它有一个嵌套的 ContentControl,它绑定(bind)到模板的 Content 属性,因此它可以将任何对象设置为其内容。 如果内容是原始字符串,我想将以下样式应
我正在尝试构建一个控件,它根据传入的类型有选择地显示不同的内容,但由于某种原因,我最终什么也没显示。 我在这里缺少一些基本的东西吗? (此代码已从我的实际生产应用程序中大量剥离,但表现出相同的行为)
我有一个自定义 ContentControl,它具有固定的 XAML 布局,如 UserControl(而不是通常应用的通用模板)。 以前这个布局没有额外的标记,所以它的字面意思是: 这很好用。
我整晚都在为这个问题苦思冥想。我想要做的就是拥有一个内容控件,它可以根据 ViewModel 中的 bool 值在显示两个不同按钮之间切换。 基本上我有一个任务在后台运行,带有一个取消按钮。一旦你点击
我正在为 Word 2007 开发应用程序级加载项插件。不幸的是,我在设置动态添加的内容时遇到了一个小问题: 我已经创建了内容控件,并使用以下代码更改了它的样式: PlainTextContentCo
我最近偶然发现了以下问题:在我的 WPF 应用程序中,我实现了一个小设计器,您可以在其中将元素放在 Canvas 上,移动、缩放和旋转它们。 在网上搜索时,我找到了 following solutio
我正在尝试学习 MVVM,但遇到了一个奇怪的问题。我有一个带有抽屉控件的主菜单,它出现并显示一个菜单: 在这个抽屉所在的主窗口中,我有一个 ContentControl我在其中使用绑定(bind)设置
我正在尝试拥有一个绑定(bind)到 View 的 MainWindow。我在代码中更改了该 View 并希望它在主窗口中更新,但是这并没有发生。 我的 XAML 中有这段代码 然后我通
我试图创建一些图表图像,但从未在屏幕上显示这些图表。我已经在这方面工作了很长一段时间并尝试了很多不同的事情,但似乎没有任何效果。如果我首先在窗口中显示图表,则代码可以完美运行,但如果我不在窗口中显示它
谁能告诉我为什么这会使我的应用程序崩溃?我不知道为什么似乎有一些无休止的递归。我得到了这个异常(exception) Logical tree depth exceeded while travers
我不知道这里发生了什么。当我直接绑定(bind)到 TextBox 时,可以编辑值,但我想绑定(bind)到 ContentControl 中。 为什么 ContentControl 不更新 View
我想禁用我的一个元素的 EntrenceThemeAnimation。我有一个网格,将此动画设置为其所有子项(我认为这是页面的默认设置)。是否可以为该网格的子级 ContentControl 禁用此动
为什么我无法在设计时解析 ContentControl ContentTemplateSelector? (在运行时有效) Designer(VS2010)显示异常: System.Reflectio
我想创建内部有一个“子”控件的自定义 WPF 控件。子类化 ContentControl 或 UserControl 可行,但有一个缺陷:这些控件在设计器模式下不起作用。 “不起作用”是指这种情况:假
我是一名优秀的程序员,十分优秀!