c# - 访问 ViewModel C# 中的组件

标签 c# xaml viewmodel

我有一个在 C# 中扩展 ViewModelBase 的类。复选框上已经有一个触发器:

public bool PrintPackingCode
{
    get
    {
        return this.reportConfiguration.PrintPackingCode;
    }

    set
    {
        this.reportConfiguration.PrintPackingCode = value;
        this.OnPropertyChanged("PrintPackingCode");
    }
}

我想挂接到该事件并呈现一个 GroupBox 以禁用,但我找不到访问 GroupBox 的方法。在 .xaml 中,我为我的 Box 命名为 PackingcodeGroupBox。我发现的所有方法和提示都不适用。我的尝试包括:

Direct Access: PackingcodeGroupBox.Enabled = false;
Using a x:Name
this.Resources["mykey"]

这里还有一些代码:

//At program start assign the view it's view model:
new SmlKonfigurationWindow(new SmlKonfigurationWindowVm(reportConfiguration, smlKonfigurationDialogVm));

public SmlKonfigurationWindow(ISmlKonfigurationWindowVm viewModel)
{
   this.DataContext = viewModel;
   this.viewModel = viewModel;

   this.InitializeComponent();
   this.ShowDialog();
}

xaml:

<CheckBox Content="Content" IsChecked="{Binding Path=PrintPackingCode, UpdateSourceTrigger=PropertyChanged}" Name="PrintPackingCode"/>
<GroupBox Header="Verpackungscode" Name="VerpackungscodeGroupbox">
   //Stuff to be disabled                      
</GroupBox>

最佳答案

IsEnabled 是环境属性,这意味着如果您禁用 GroupBox,则该组框内的所有控件也将被禁用。

尝试像这样在 GroupBox 上添加绑定(bind):

IsEnabled="{Binding PrintPackingCode}"

如果给复选框命名,也可以将 IsEnabled 绑定(bind)到复选框。

<CheckBox x:Name="myCheckBox" .../>
<GroupBox IsEnabled="{Binding ElementName=myCheckBox, Path=IsChecked}"/>

关于c# - 访问 ViewModel C# 中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53206716/

相关文章:

Android ViewModel 附加参数

c# - 如何扩展类型类

c# - MediaPlayerElement 循环播放视频 UWP 的问题

c# - 如何通过 ViewModel 控制 View VisualState

wpf - 在 WrapPanel 内自动调整大小?

c# - 绑定(bind)到集合计数

asp.net-mvc - 如何正确使用 View 模型

c# - 如何将 List<string> 转换为 List<myEnumType>?

c# - Umbraco,请求通过错误的 RenderMvcController 路由

c# - 将数据表从一个 aspx 页面传输到另一个 C#