禁用TextBox样式中的文本选择

时间:2012-04-17 20:18:10

标签: c# wpf textbox textselection

我想最好使用样式禁用文本框中的文本选择。原因是我有一种样式,使文本框看起来像文本块,直到满足某个标准(IsRenaming)。这些是树视图的节点,因此我不希望用户能够选择文本。这是风格:

<Style x:Key="TextBlockStyleForTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="IsReadOnly" Value="True" />
</Style>

<Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBlockStyleForTextBox}">
        <Setter Property="Cursor" Value="Arrow"/>
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsRenaming}" Value="true">
                <DataTrigger.Setters>
                    <Setter Property="TextBox.IsReadOnly" Value="False" />
                    <Setter Property="Cursor" Value="IBeam" />
                    <Setter Property="Background" >
                        <Setter.Value>
                            <SolidColorBrush Color="{DynamicResource WhiteColor}"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Padding" Value="2" />
                    <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
                    <Setter Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>
                </DataTrigger.Setters>
            </DataTrigger>
        </Style.Triggers>
    </Style>

我认为我不会在任何地方覆盖IsReadOnly。这是我的文本框定义:

<DataTemplate x:Key="MyTemplate" >
        <TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" 
                 Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TreeView}}"
                 Style="{StaticResource RenamingTextBox}">

...等

3 个答案:

答案 0 :(得分:1)

嗯,禁止突出显示文本的一种快速方法是添加 到您的IsEnabled样式切换。否则,您可能希望使用StackPanel覆盖TextBox的模板,该模板切换TextBox和TexBlock - 超级易于操作,并且大量样本遍布,即使在堆栈上也是如此。 这是使用IsEnabled修改后的样式..

<Style x:Key="TextBlockStyleForTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="IsEnabled" Value="False"/>
    </Style>

    <Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBlockStyleForTextBox}">
        <Setter Property="Cursor" Value="Arrow"/>
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsRenaming}" Value="true">
                <DataTrigger.Setters>
                    <Setter Property="TextBox.IsReadOnly" Value="False" />
                    <Setter Property="IsEnabled" Value="True"/>
                    <Setter Property="Cursor" Value="IBeam" />
                    <Setter Property="Background" >
                        <Setter.Value>
                            <SolidColorBrush Color="{DynamicResource WhiteColor}"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Padding" Value="2" />
                </DataTrigger.Setters>
            </DataTrigger>
        </Style.Triggers>
    </Style>

答案 1 :(得分:1)

这很快而且很脏,但是在另一个答案中我们的谈话是一个很好的开始/例子,你可以和风格进行额外的调整,但它完全有效(只是测试了它)

<Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <TextBlock x:Name="block" Visibility="Visible" Text="{TemplateBinding Text}" Margin="1.5"/>
                            <TextBox x:Name="box" Visibility="Collapsed" Text="{TemplateBinding Text}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsRenaming}" Value="true">
                                <DataTrigger.Setters>
                                    <Setter TargetName="block" Property="TextBox.Visibility" Value="Collapsed" />
                                    <Setter TargetName="box" Property="TextBox.Visibility" Value="Visible" />
                                </DataTrigger.Setters>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>    

答案 2 :(得分:0)

你的款式运作得很好,因为我刚试过它们! 3个可能的问题:

  1. 您没有正确绑定TextBox
  2. 你没有分配你的风格
  3. IsRemaining没有提高属性更改


    这是我的测试

    <Window x:Class="Sample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="500" Width="600" 
    x:Name="wnd">
       <Window.Resources>
           ...I put your styles in there..
       </Windows.Resources>
    <StackPanel>
        <TextBox DataContext="{Binding ViewModel, ElementName=wnd}" Style="{StaticResource RenamingTextBox}" >tata</TextBox>
        <Button Command="{Binding ViewModel.SomeCommand, ElementName=wnd}">change read only</Button>
    </StackPanel>
    
  4. 代码(.xaml.cs)

    public partial class MainWindow : Window
    {
        public ViewModel ViewModel { get; set; }
    
        public MainWindow()
        {
            ViewModel = new ViewModel();
            InitializeComponent();          
        }
        }
    
    public class ViewModel : INotifyPropertyChanged
    {
        private DelegateCommand _someCmd;
        private bool _isRenaming;
    
        public DelegateCommand SomeCommand
        {
            get
            {
                return _someCmd ?? (_someCmd = new DelegateCommand(() =>
                                                                    {
                                                                        IsRenaming = true;
                                                                    }));
            }
        }
    
        public bool IsRenaming
        {
            get { return _isRenaming; }
            set
            {
                _isRenaming = value;
                RaisePropertyChanged("IsRenaming");
            }
        }
          }