When putting a textBox to IsReadOnly="true", the textBox changes the backgroundcolor a little.
-
Wednesday, October 10, 2012 3:48 PM
Hi,
When putting a textBox to IsReadOnly="true", the textBox changes the backgroundcolor a little.
Is this possible to prevent?
<TextBox Text="Test" IsReadOnly="True" Foreground="Black" BorderBrush="Transparent" Background="Red" />
All Replies
-
Wednesday, October 10, 2012 3:57 PM
Ye, you would have to create a custom template for the TextBox.
-
Wednesday, October 10, 2012 4:29 PM
That sounds interesting. Though I am not sure how this is done.
Is there any example of how to set this up?
-
Wednesday, October 10, 2012 4:35 PM
Do you have Expression Blend installed?
-
Thursday, October 11, 2012 3:04 AM
Is this possible to prevent?
yes you can prevent the behaviour by changing the textbox style with the help of expression blend, i did some time back, just appy this style to that textbox.
<Style x:Key="ReadOnlyStyle" TargetType="TextBox"> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Background" Value="#FFFFFFFF"/> <Setter Property="Foreground" Value="#FF000000"/> <Setter Property="Padding" Value="2"/> <Setter Property="BorderBrush"> <Setter.Value> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFA3AEB9" Offset="0"/> <GradientStop Color="#FF8399A9" Offset="0.375"/> <GradientStop Color="#FF718597" Offset="0.375"/> <GradientStop Color="#FF617584" Offset="1"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TextBox"> <Grid x:Name="RootElement"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="MouseOver"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="MouseOverBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#FF99C1E2"/> </ColorAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Disabled"> <Storyboard> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value="1"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="ReadOnly"> <Storyboard> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:Name="FocusStates"> <VisualState x:Name="Focused"> <Storyboard> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value="1"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Unfocused"> <Storyboard> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value="0"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:Name="ValidationStates"> <VisualState x:Name="Valid"/> <VisualState x:Name="InvalidUnfocused"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="InvalidFocused"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen"> <DiscreteObjectKeyFrame KeyTime="0"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Border x:Name="Border" Opacity="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1"> <Grid> <Border x:Name="MouseOverBorder" BorderBrush="Transparent" BorderThickness="1"> <ScrollViewer x:Name="ContentElement" BorderThickness="0" IsTabStop="False" Padding="{TemplateBinding Padding}"/> </Border> </Grid> </Border> <Border x:Name="DisabledVisualElement" IsHitTestVisible="False" Opacity="0" Background="#A5F7F7F7" BorderBrush="#A5F7F7F7" BorderThickness="{TemplateBinding BorderThickness}"/> <Border x:Name="FocusVisualElement" Margin="1" IsHitTestVisible="False" Opacity="0" BorderBrush="#FF6DBDD1" BorderThickness="{TemplateBinding BorderThickness}"/> <Border x:Name="ValidationErrorElement" Visibility="Collapsed" BorderBrush="#FFDB000C" BorderThickness="1" CornerRadius="1"> <ToolTipService.ToolTip> <ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ValidationToolTipTemplate}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"> <ToolTip.Triggers> <EventTrigger RoutedEvent="Canvas.Loaded"> <BeginStoryboard> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsHitTestVisible"> <DiscreteObjectKeyFrame KeyTime="0"> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </ToolTip.Triggers> </ToolTip> </ToolTipService.ToolTip> <Grid Height="12" HorizontalAlignment="Right" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12" Background="Transparent"> <Path Fill="#FFDC000C" Margin="1,3,0,0" Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z"/> <Path Fill="#ffffff" Margin="1,3,0,0" Data="M 0,0 L2,0 L 8,6 L8,8"/> </Grid> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
-
Thursday, October 11, 2012 8:07 AM
Thank you for this code.
I have put this code in the XAML and applied it to the textBox like below. But the application becomes completely "white" when starting up, as something is wrong.
This happens for the naked textBox. Initially I will want the style for the textBox embedded in the DataGridTemplateColumn. When putting the style to that textBox, the application starts fine But when putting an itemsource to the dataGrid, the application becomes "white" again.
I dont know if something is wrong with the style?
<TextBox x:Name="eTextBox3" Style="{StaticResource ReadOnlyStyle}" Width="244" Canvas.Top="184" Canvas.Left="35" IsReadOnly="True" /> <data:DataGridTemplateColumn Header="header1"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding A1}" Style="{StaticResource ReadOnlyStyle}" IsReadOnly="True" Foreground="Black" BorderBrush="Transparent" Background="White" /> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> -
Sunday, October 28, 2012 11:43 AM
I still have problem when putting an itemsource to the DataGrid. When I do that the whole application becomes "WHITE" and I have to force to close it.
It must be something wrong with the "StaticResource ReadOnlyStyle" (found in 2 posts ago) but cant figure out what it depends on?
<data:DataGridTemplateColumn Header="header1"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding A1}" Style="{StaticResource ReadOnlyStyle}" IsReadOnly="True" Foreground="Black" BorderBrush="Transparent" Background="White" /> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn>

