2

我想最好使用样式禁用文本框中的文本选择。原因是我有一种风格,它使文本框看起来像一个文本块,直到满足某个标准(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}">

... ETC

4

3 回答 3

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>    
于 2012-04-18T16:41:31.497 回答
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>
于 2012-04-17T20:58:12.837 回答
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>
    

背后的代码 (.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");
        }
    }
      }
于 2012-04-17T20:41:16.683 回答