Style a custom WPF control when it's in a different state - wpf

I have a custom autocomplete control in WPF.It's created by combining a textbox and a dropdownlist, and a custom style for it.Looks like this ->"
The background color he has is the same even if is editable or readonly.And on my view i have more boxes and lists, they look like this ->
These are simple textboxes, and they change they're background depending on they're state: readonly or editable.
So my question is : how can i style my custom control to have that same gray background when it is in readonly mode in order to have the same standard on my view ?

I think you can use DataTrigger to get it works, like this:
<Style TargetType="YourCustomControl">
<Style.Triggers>
<DataTrigger Binding="{Binding IsReadOnly}" Value="True">
<Setter Property="Background" Value="ColorHere"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsReadOnly}" Value="False">
<Setter Property="Background" Value="ColorHere"/>
</DataTrigger>
</Style.Triggers>
</Style>

Related

Specify a binding in an application-level style resource?

I'm fairly new to WPF and have created a style to alter the appearance of a button control. The style contains a data trigger to change the button background (amongst other things) based on a boolean property in the data context, e.g.:-
<Style x:Key="IndicatorButton" TargetType="Button">
<DataTrigger Binding="{Binding Path=ValveIsOpen}" Value="True">
<Setter Property="Background" Value="#00FF00"/>
..etc..
Currently the style is only used by a single button, so the data trigger binding is hard-coded with a property called "ValveIsOpen".
I now want to re-use this style throughout my app, with different buttons being bound to different properties. How would I change the data trigger binding on each button that the style is applied to?
Many thanks
You need to define a base style and derived styles, such as
<Style x:Key="IndicatorButton" TargetType="Button">
<Setter Property="Foreground" .../>
...
<Style x:Key="ValveIndicatorButton" TargetType="Button" BasedOn={StaticResource IndicatorButton}>
<DataTrigger Binding="{Binding Path=ValveIsOpen}" Value="True">
<Setter Property="Background" Value="#00FF00"/>
..etc..

changing user control dynamically in wpf application

I have two user controls which needs to be loaded dynamically based on a property in the database.
I am modifying an existing XAML page. The page contains the following line
<wcontrols:page1 x:Name="page1" Width="674" Height="372.215"Canvas.Left="57" Canvas. Top="215.785" Loaded="page1_Loaded_1" />
I want to switch between page1 with page2 dynamically. Should i need to move the code into the code-behind file or is there any way to do that within the XAML file?
Define a style for the control and put in it a DataTrigger that set the page 2 at the change of a property of the viewmodel
<ContentControl>
<ContentControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=property}" Value="page1">
<Setter Property="ContentControl.Content" Value="{StaticResource page1}"/>
</DataTrigger>
<DataTrigger Biniding="{Binding Path=property}" Value="page2">
<Setter Property="ContentControl.Content" Value="{StaticResource page2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>

Hide selected TabIItems in WPF TabControl

I'm using the TabControl in WPF as a sort of equivalent to the ASP.Net multiview control. I need to make two of the four tabitem headers hidden. What would be the best method of doing this in XAML?
I fixed this by adding DataTriggers to my Template. If my tab is DeTached (Hidden) I set the Visibility property to Collapsed. If it's visiable again I simply set the Visibility property to Visible again.
<DataTrigger Binding="{Binding IsDetached}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding IsDetached}" Value="False">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
Edit: Updated based on #Miklós Balogh feedback. Thanks, improved my code as well, haha. :)

Calling an ElementMethod in a DataTrigger

I have a TextBox thats using this Style. I need to add a Focus() method to in this style.
So that when the TextBox is Visible and the ValidParent Property is false then i call the Focus() method on that TextBox
<Style x:Key="ParentTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ValidParent }" Value="false">
...
</DataTrigger>
<DataTrigger Binding="{Binding Path=ValidParent }" Value="false">
<Setter Property="BorderBrush" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
Is this possible ?? And if it is then if i had multiple textboxes with the same behaviour which one will recieve Focus?? Does the Order of the Controls in my Xaml make a diffrence then ??
Thank you
You cannot call methods via style triggers. Using Interactivity from the Blend SDK you have more options, including method calls but they cannot be easily used in styles.

Set the Background of a DataGridRow based on the content of a cell

Is there a way, using XAML, to dynamically set the background of a row based on the content of one of it's cells?
Thanks,
Phil
You can define a style for a row and change the color using DataTrigger. Something like this:
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding BooleanPropertyOnObjectBoundToRow}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
Here BooleanPropertyOnObjectBoundToRow is a boolean property on your data object one the cells is bound to.

Resources