C# 已禁用控件,但具有选择和复制功能
我正在编写一个应用程序,其中用户必须在几个特定视图中单击“编辑”才能对其进行编辑,我通过将控制器(文本框、组合框等)绑定到VM中的“NotReadOnly”属性来解决这个问题 现在,我的用户希望能够从我的控制器(特别是文本框)复制数据,而不必先单击我的编辑按钮。这是不可能的,因为IsEnabled=false会禁用大多数功能 更改为“IsReadOnly=True”不是一个替代方案,我想要禁用控制器的外观(背景、字体更改等),以便我的用户可以清楚地看到它不在编辑模式下,我不想通过绑定到VM中的“ReadOnly”属性来完成所有这些,在某些情况下,多个后台属性决定是否启用某个控制器 因此,我希望找到一些方法,让副本(最好也是选择/滚动)在禁用的控制器中工作C# 已禁用控件,但具有选择和复制功能,c#,wpf,isenabled,readonly-attribute,C#,Wpf,Isenabled,Readonly Attribute,我正在编写一个应用程序,其中用户必须在几个特定视图中单击“编辑”才能对其进行编辑,我通过将控制器(文本框、组合框等)绑定到VM中的“NotReadOnly”属性来解决这个问题 现在,我的用户希望能够从我的控制器(特别是文本框)复制数据,而不必先单击我的编辑按钮。这是不可能的,因为IsEnabled=false会禁用大多数功能 更改为“IsReadOnly=True”不是一个替代方案,我想要禁用控制器的外观(背景、字体更改等),以便我的用户可以清楚地看到它不在编辑模式下,我不想通过绑定到VM中的“
如果这是不可能的,是否有任何方法可以在不向每个控制器添加大量XAML的情况下获得禁用控制器的外观?无法从禁用的文本框中选择文本。您可以做的是将其设置为只读,并将类似设置为禁用
<TextBox IsEnabled="False">Disabled</TextBox>
<TextBox IsReadOnly="True" Text="Readonly" Background="LightGray" Foreground="Gray"></TextBox>
已禁用
请参阅本文:无法从禁用的文本框中选择文本。您可以做的是将其设置为只读,并将类似设置为禁用
<TextBox IsEnabled="False">Disabled</TextBox>
<TextBox IsReadOnly="True" Text="Readonly" Background="LightGray" Foreground="Gray"></TextBox>
已禁用
请参阅本文:您不必将XAML添加到包含控件的每个窗口中。只需将此代码添加到
WPF
项目的App.Xaml
文件中,应用程序中的所有文本框控件将具有与IsEnabled=false相同的行为:
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="White" />
<Style TargetType="TextBox">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ScrollViewer Name="PART_ContentHost" Background="{TemplateBinding Background}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Value="{StaticResource DisabledBackgroundBrush}" Property="Background" />
<Setter Value="{StaticResource DisabledForegroundBrush}" Property="Foreground" />
<Setter TargetName="PART_ContentHost" Property="Background" Value="Blue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果希望在整个应用程序中跨不同窗口使用样式,可以为整个应用程序定义样式:
<Application x:Class="WpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication"
StartupUri="MainWindow.xaml">
<Application.Resources>
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="White" />
<Style TargetType="TextBox">
<!--The code omitted for the brevity-->
</Setter>
</Style>
</Application.Resources>
</Application>
您不必将XAML添加到包含控件的每个窗口中。只需将此代码添加到WPF
项目的App.Xaml
文件中,应用程序中的所有文本框控件将具有与IsEnabled=false相同的行为:
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="White" />
<Style TargetType="TextBox">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ScrollViewer Name="PART_ContentHost" Background="{TemplateBinding Background}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Value="{StaticResource DisabledBackgroundBrush}" Property="Background" />
<Setter Value="{StaticResource DisabledForegroundBrush}" Property="Foreground" />
<Setter TargetName="PART_ContentHost" Property="Background" Value="Blue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果希望在整个应用程序中跨不同窗口使用样式,可以为整个应用程序定义样式:
<Application x:Class="WpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication"
StartupUri="MainWindow.xaml">
<Application.Resources>
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="Red" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="White" />
<Style TargetType="TextBox">
<!--The code omitted for the brevity-->
</Setter>
</Style>
</Application.Resources>
</Application>
这似乎很有希望,但我猜您的意思是Trigger Property=“IsReadOnly”,因为如果我想同时具备选择/复制和禁用外观/感觉的功能,我想在这里广泛应用样式?谢谢@当然,你是对的。我已经更新了我的代码。如果我的回答是您问题的答案,您可以将其指定为答案,以帮助其他人搜索类似的问题。当然,我会再尝试一下,看看它是否符合我的预期。如果您在这方面遇到问题,请告诉我,我可以试着进一步帮助您。这似乎很有希望,但我猜您的意思是Trigger Property=“IsReadOnly”如果我想同时拥有选择/复制和禁用外观/感觉的功能,那么我想在这里广泛应用样式?谢谢@当然,你是对的。我已经更新了我的代码。如果我的回答是您问题的答案,您可以将其指定为答案,以帮助其他人搜索类似的问题。当然,我会再尝试一下,看看它是否符合我的预期。如果您在这方面有问题,请告诉我,我可以尝试进一步帮助您。