Colour multiple textboxes with one method wpf - c#

I have a method that is supposed to color the background of a textbox that is on focus(Enter)
private void onEnter_ColourChange(object sender, EventArgs e)
{
this.BackColor = Color.White;
}
This doesn't work, however. I checked this answer, but I just got a NullReferenceException.
Essentially, I would like to color multiple textboxes when the textbox is in focus, with one method. Is there a way to do this?

First of all: TextBox has only the property Background and not BackColor!
If you want to change the color of the focused TextBox, a simple way is, to use a Style with a Trigger.
<Window.Resources>
<Style TargetType="TextBox" x:Key="TextBoxFocusBackgroundStyle" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true" >
<Setter Property="Background" Value="Brown" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
As you can see, the Trigger watches on the property IsFocused. If the TextBox got the Focus (so IsFocused changes to true), the Background will be brown.
To use this Style:
<StackPanel>
<TextBox Text="Peter" Style="{StaticResource TextBoxFocusBackgroundStyle}"/>
<TextBox Text="Laszlo" Style="{StaticResource TextBoxFocusBackgroundStyle}"/>
<TextBox Text="Julia" Style="{StaticResource TextBoxFocusBackgroundStyle}"/>
<TextBox Text="Romeo" Style="{StaticResource TextBoxFocusBackgroundStyle}"/>
</StackPanel>
If you already has a Style for the TextBoxes you should use that Style in the BasedOn attribute.

Related

Force focus on control via xaml style

I have a treeview and a listbox.
I want to specify on the style trigger-setter option that, when the listbox visibility is Hidden the focus have to return on the treeview.
Is it possibile to condition an user control focus on the state of another user control?
something like
<Style TargetType="{x:Type TreeView}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Hidden">
<Setter Property="IsFocus" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
Yes it is, see the example here
FOR CHILDREN IN CONTAINERS
<StackPanel FocusManager.FocusedElement="{Binding ElementName=lol}">
<TextBox x:Name="lol"/>
<TextBox x:Name="lul"/>
</StackPanel>
FOR SELF
<TextBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>

Style TreeView Header. Set Header Unselectable

how can i style only the header of a tree view ?
I want to make the header unselectable.
Something like:
<TreeViewHeaderStyle>
<Setter Property="Focusable" Value="false">
</TreeViewHeaderStyle>
Thank you :)
Style the TreeViewItem in your Window's or Control's resources.
<Window.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsEnabled" Value="False"/>
</Style>
</Window.Resources>
EDIT:
If you still need to expand via the triangle, but want a disabled header, the solution is a bit dirtier. Many solutions show a complicated restyling of the HeaderedItemsControl you can search for that or simply add a Label to each TreeViewItem and handle a mouse down event. In the below example I wrapped a Label inside a grid so that I could style the label as needed. When the Grid is clicked the event is handled preventing expansion. If you are only setting the Label content, you can just add the MouseDown event to the Label and ditch the Grid.
<TreeViewItem>
<TreeViewItem.Header>
<Grid MouseDown="Grid_MouseDown">
<Label Content="TEST" IsEnabled="False" />
</Grid>
</TreeViewItem.Header>
<Button Content="Random SubItem"/>
</TreeViewItem>
CODE BEHIND:
private void Grid_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
e.Handled = true;
}

WPF Blur text in textbox

I want to set blur text in Textbox and PasswordBox.
When Textbox and PasswordBox is focused, that blur text will disappear and when lost focus, blur text will reappear.
Any solution?
You can use triggers to dynamically change value of a property under certain conditions. In this case you can set Effect property value to BlurEffect when the control is focused like this:
<Style x:Key="BlurEffect" TargetType="{x:Type Control}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="False">
<Setter Property="Effect">
<Setter.Value>
<BlurEffect Radius="5"></BlurEffect>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
Then you apply your style to you control:
<TextBox Style="{StaticResource BlurEffect}" />
<PasswordBox Style="{StaticResource BlurEffect}" />
You can apply this style to any element derived from Control class.

Change the style of content dialog button in windows phone universal application

I have this content dialog box defined inside my xaml:
<ContentDialog x:Name="AlertMessage" Background="#363636" IsSecondaryButtonEnabled="True" SecondaryButtonText="Cancel" IsPrimaryButtonEnabled="True" PrimaryButtonText="Ok" >
<ContentDialog.Content>
<StackPanel Name="rootStackPanel" Height="Auto" >
<StackPanel Margin="0">
<StackPanel Margin="0,0,0,10" Orientation="Horizontal">
<TextBlock x:Name="HeadingText" x:FieldModifier="public" Style="{StaticResource ApplicationMessageBoxHeadingStyle}" Text="Alert" />
<Image Margin="10,05,0,0" Source="/Assets/Images/alert.png" Width="35"></Image>
</StackPanel>
<TextBlock x:FieldModifier="public" x:Name="ContentText" Style="{StaticResource ApplicationMessageBoxErrorStyle}" Text="Are you sure you want to log off ?" />
</StackPanel>
</StackPanel>
</ContentDialog.Content>
</ContentDialog>
And I'm calling it like this:
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
MessageBox();
}
private async void MessageBox()
{
ContentDialogResult LogoutDialog = await AlertMessage.ShowAsync();
if (LogoutDialog == ContentDialogResult.Primary)
{
this.Frame.Navigate(typeof(MainPage));
}
else
{
// User pressed Cancel or the back arrow.
// Terms of use were not accepted.
}
}
Problem: is that my app have a style for all the buttons in my applications. And I want to apply same styles to the primary and secondary buttons of that dialog box too. And I am unable to figure out how to Achieve this. Is it possible to apply styles to these buttons?
I used this code to change Button color on my UWP app.
var cd = new ContentDialog();
cd.Content = "Remove file ?";
cd.Title = "Remove file";
cd.PrimaryButtonText = "OK";
cd.SecondaryButtonText = "Cancel";
var bst = new Windows.UI.Xaml.Style(typeof(Button));
bst.Setters.Add(new Setter(Button.BackgroundProperty, Colors.Red));
bst.Setters.Add(new Setter(Button.ForegroundProperty, Colors.White));
cd.PrimaryButtonStyle = bst;
var ress = await cd.ShowAsync();
You can indirectly customize the buttons.
The ContentDialog's buttons use the "default" style for the target type "Button". You can overwrite the ContentDialog Style of the SDK and add a different Button Style in the Setter of the Template. This button style will just be valid in the scope of the ContentDialog control.
In this example the new button style needs to be put in the first border element (x:Name="Container")
<Border.Resources>
<Style TargetType="Button" BasedOn="{StaticResource YourNormalButtonStyle}">
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
</Border.Resources>
If you want to change the ContendDialog PrimaryButtonSettings you need inside ContenDialog.Resources just to set a Style that you are targeting Buttons and the Setter of With Property="" and Value="" and that should fix below is one example.
Converter={StaticResource StringEmptyToBoolConverter}}"
<ContentDialog.Resources>
<converters:StringEmptyToBoolConverter x:Key="StringEmptyToBoolConverter"/>
<Style TargetType="Button">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="FontSize" Value="13.6"/>
<Setter Property="Width" Value="Auto"/>
</Style>
</ContentDialog.Resources>
<WebView Height="400" Width="500" DefaultBackgroundColor="Transparent" Source="{Binding State.GuideUri}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="RulesWebView"/>
The ContentDialog buttons aren't customizable, but you can leave the ContentDialog's primary and secondary buttons unset and add your own buttons within the template.

Wpf, style is not being applied

I've written a user control with popup, who's content is being set outside the control. The ControlTemplate of that control looks like the following:
<ControlTemplate TargetType="local:InfoIcon">
<Grid>
<ToggleButton x:Name="HelpButton" Style="{StaticResource HelpButton}" />
<Popup PlacementTarget="{Binding ElementName=HelpButton}" Placement="Bottom"
IsOpen="{Binding ElementName=HelpButton, Path=IsChecked, Mode=TwoWay}" StaysOpen="False">
<Border BorderBrush="#767676" BorderThickness="1"
Background="#f1f2f7">
<Border.Resources>
<!-- Important -->
<Style TargetType="Label">
<Setter Property="Foreground" Value="#575757" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="#575757" />
</Style>
<!-- /Important -->
</Border.Resources>
<ContentPresenter Content="{TemplateBinding HelpContent}" />
</Border>
</Popup>
</Grid>
</ControlTemplate>
The Important part - I want to assign custom styles to items, which are being put inside the popup (it serves as a clickable hint)
I'm using my control in the following way:
<local:MyControl>
<local:MyControl.HelpContent>
<TextBlock>Ala ma kota</TextBlock>
</local:MyControl.HelpContent>
</local:MyControl>
But despite styles in the Border, TextBlock's text's color always inherit the value from its parent (checked using Snoop) - resulting in white text on white background.
You can downlad the small PoC application, which demonstrates the problem.
My observations:
The styling does work for Label. It only doesn't work for TextBlock.
When I add TextBlock.Foreground="Red" to the Border, TextBlock becomes red, still ignoring style (but now using color from Border).
Snoop informs, that this TextBlock actually has the Style resolved correctly. But despite it shouldn't, it uses the inherited value instead of one specified in the style.
How can I solve this problem and why does it occur?
I received answer on Microsoft forums; I'll leave it here in case someone encounters the same problem.
The difference is that a TextBlock is not a control, i.e. it doesn't have any ControlTemplate and because of this the implicit style doesn't get applied to it when it is located inside the StackPanel. Please see the following page for more information: http://blogs.msdn.com/b/wpfsdk/archive/2009/08/27/implicit-styles-templates-controls-and-frameworkelements.aspx
You could use Label elements or set the style for the TextBlock elements explicitly.
-- Magnus (MM8)
Edit2
I've set the Foreground of the UserControl to something else. This behavior is because the child TextBlock controls of the UserControl inherit the Foreground-Settings somehow. This has nothing to do with the popup or some other approaches we tried yet.
I've stumbled upon another question with a similar problems here: Cannot override controls foreground colour in wpf
I suggest to accept this strange behavior and just set a Foreground Color of the UserControl instead:
<Style TargetType="{x:Type local:InfoIcon}">
<Setter Property="Foreground" Value="Red"/>
</Style>
previous Edit
You had my curiousity with this weird behavior, but after looking at your PoC it was rather obvious :) The Popup has some attached Properties TextElement.* where you can style the text elements in the popup. This was new to me, too and I will reseach a bit more afterwards. Nevertheless: Workaround for your Problem is to not style the TextBlock but the Popup instead. your code could look something like following :
<ControlTemplate TargetType="{x:Type local:InfoIcon}">
<ControlTemplate.Resources>
<Style TargetType="Popup">
<Setter Property="TextElement.Foreground" Value="Red"/>
</Style>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Red" />
</Style>
</ControlTemplate.Resources>
<Grid>
<ToggleButton x:Name="TB" Width="16" Height="16"/>
<Popup Placement="Bottom" PlacementTarget="{Binding ElementName=TB}" IsOpen="{Binding ElementName=TB, Path=IsChecked}" StaysOpen="False">
<ContentPresenter Content="{TemplateBinding InfoContent}"/>
</Popup>
</Grid>
</ControlTemplate>
I changed the styles to be outside of the controls, of course you can just use the attached properties of the popup directly. But initially you wanted to know how it works with the styles attached at the border, it does not matter now where you add the styles. You can use a ResourceDictionary for example.
As a suggestion, shouldn't this:
TargetType="local:InfoIcon"
be like this?
TargetType="{x:Type local:InfoIcon}"
Maybe you have some TextBlock style defining that it shouldd take the parent's control foreground.
Did you try to add a BasedOn property like this ?
<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Foreground" Value="#575757" />
</Style>
I tried with your code example and this works :
<ContentPresenter Content="{TemplateBinding InfoContent}">
<ContentPresenter.Style>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="TextBlock.Foreground" Value="Red" />
</Style>
</ContentPresenter.Style>
<ContentPresenter.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="Red" />
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
That's kind of odd because when I put the Foreground setter for the Label control inside the ContentPresenter.Style then this time it's Label wich doesn't work...I think it's because Label is a considered as a ContentControl whereas TextBlock is just a FrameworkElement.
Had a similar issue caused by another problem:
There is a strange bug in WPF that prevents styles, defined in merged dictionaries, from being applied to the first element:
https://www.engineeringsolutions.de/wpf-fix-style-is-only-applied-to-first-element/

Categories

Resources