c# - 如何使用 XAML 在整个容器(如 Panel 或 GroupBox)上设置 IsReadOnly/IsEnabled?

标签 c# wpf xaml mvvm isenabled

我有一个 MVVM 应用程序,它包含多个 View 以及一些基于用户权限、查看/编辑模式和对象状态的复杂 IsReadOnly 规则。

我想为同一容器中的整个控件组设置 IsReadOnly 和/或 IsEnabled 属性(GroupBox/ StackPanel/Grid/UserControl/等)。此属性的值将在 ViewModel 中定义。

每个 UserControl 我有 3-6 个不同的 SomeGroupIsReadOnly 属性(有大量输入控件,如 TextBoxRadioButtonsComboBoxes 和一些 DataGrids),我正在寻找一个通用的、MVVM 友好的解决方案,它将允许我在每个容器上重用绑定(bind)基础,而不是分别为每个单独的控件指定它们。

如何使用 XAML 在容器(如 Panel 或 GroupBox)内的所有控件上设置 IsReadOnly/IsEnabled?

WPF 似乎不支持开箱即用...

编辑

我忘了提到为容器设置 IsEnabled 会禁用文本框的一项重要功能 - 能够复制其内容。我需要它们处于 IsReadOnly=true 状态。如果有解决方法,那么我的问题就会得到解决。

最佳答案

禁用包含您的控件的面板也会禁用面板内的控件。将 Panel 的 IsEnabled 成员绑定(bind)到 ViewModel 上的 bool 属性,并根据您的规则设置它以禁用 Panel 及其所有子项。

Xaml:

<GroupBox IsEnabled="{Binding Path=IsGroupEnabled}">
    <StackPanel>
        <Button Content="Sample Button"/>
        <TextBox Text="Sample text box"/>
    </StackPanel>
</GroupBox>

关于c# - 如何使用 XAML 在整个容器(如 Panel 或 GroupBox)上设置 IsReadOnly/IsEnabled?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051226/

相关文章:

c# - .NET 中定义的 char struct -- 运算符在哪里?

c# - 反序列化 xml,其中字符串可能包含 xml/html

c# - 仅在依赖属性更改值后引发事件

c# - 为什么 MultiBinding a TranslateTransform.X 在 WPF 中似乎不起作用?

c# - EventTrigger 中的模板绑定(bind)

c# - 使用 gRPC 进行基于自定义 channel 的身份验证

c# - 使用C#识别 “GridViewCellEditEndedEvent”中的选定行

c# - 从 DataTemplate 上的按钮获取 ListBox 行对象

c# - 设置组合框的下拉高度(winrt)

c# - 在 WPF 中绑定(bind)包含 WindowsFormHost 项的 ItemsControl