我有一个像xaml这样的控件,只读启用。
<TextBox Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Background="Transparent" IsReadOnly="True" BorderThickness="0" TextWrapping="Wrap" >
现在,当我双击此文本框时,我应该能够输入文本。 Readonly属性应该变为false
如果我移动到窗口中除此文本框之外的其他项目,则文本框应该再次成为只读。
我正在尝试使用触发器。但没有得到正确的提示。谁能在这帮助我?
答案 0 :(得分:4)
你可以用2个事件,MouseDoubleClick和LostFocus
来实现这个目的<Grid>
<TextBox IsReadOnly="True"
MouseDoubleClick="TextBox_MouseDoubleClick"
LostFocus="TextBox_LostFocus"/>
</Grid>
在您的程序代码中:
private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TextBox textBox = sender as TextBox;
textBox.IsReadOnly = false;
//textBox.CaretIndex = textBox.Text.Count();
textBox.SelectAll();
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = sender as TextBox;
textBox.IsReadOnly = true;
}
答案 1 :(得分:3)
您可以使用System.Windows.Interactivity
程序集(msdn)来执行此操作。
首先:创建辅助类来设置属性:
public class SetterAction : TriggerAction<DependencyObject>
{
public SetterAction()
{
Setters = new List<Setter>();
}
public List<Setter> Setters { get; set; }
protected override void Invoke(object parameter)
{
foreach (var item in Setters)
{
AssociatedObject.SetValue(item.Property, item.Value);
}
}
}
XAML:
<TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Background="Transparent" IsReadOnly="True" BorderThickness="0" TextWrapping="Wrap"
Height="30" Width="200">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:EventTrigger.Actions>
<local:SetterAction>
<local:SetterAction.Setters>
<Setter Property="TextBox.IsReadOnly" Value="False" />
<Setter Property="TextBox.Background" Value="Green" />
</local:SetterAction.Setters>
</local:SetterAction>
</i:EventTrigger.Actions>
</i:EventTrigger>
<i:EventTrigger EventName="LostFocus">
<i:EventTrigger.Actions>
<local:SetterAction>
<local:SetterAction.Setters>
<Setter Property="TextBox.IsReadOnly" Value="True" />
<Setter Property="TextBox.Background" Value="Red" />
</local:SetterAction.Setters>
</local:SetterAction>
</i:EventTrigger.Actions>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
i
的位置:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
答案 2 :(得分:3)
您可以使用Style和EventTrigger来执行此操作
<Window xmlns:sys="clr-namespace:System;assembly=mscorlib" ...>
<Window.Resource>
<Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<EventTrigger RoutedEvent="LostFocus">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0"
Storyboard.TargetProperty="(TextBox.IsReadOnly)">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<sys:Boolean>True</sys:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseDoubleClick">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0"
Storyboard.TargetProperty="(TextBox.IsReadOnly)">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<sys:Boolean>False</sys:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resource>
...
<TextBox Style="{StaticResource MyTextBoxStyle}" .../>
</Window>