21. How can we find resources from ResourceDictionary and apply on controls?
ResourceDictionary stored resources as key value pairs and you have a method called FindResource in every control to find the resource from the ResourceDictionary.
Every resource is stored as reference type so you have to cast the object to the original type.
btn1.Background = (Brush)btn1.FindResource("background");
FindResource method first search in the current element and then in every parent control Resources property. In the last search in Application resources which is in App.xaml file. If it does not found a resource then it throws an exception.
There is another method TryFindResource that will returns null if it have not found a resource.
btn1.Background = (Brush)btn1.TryFindResource("background");
###ADSPACE1###
22. What is StaticResource?
StaticResource class is used for assigning a resource to a property of Control.
<Button x:Name="btn2" Background="{StaticResource BackgroundBrush1}" />
in code-behind:
btn2.Background = (Brush)btn2.FindResource("BackgroundBrush1");
23. What is DynamicResource?
DynamicResource is just like StaticResource class except it monitors the changes you will do with the Resource.
If you change anything in the Resource, bounded property of the control will be updated automatically.
<Button x:Name="btn2" Background="{DynamicResource BackgroundBrush1}" />
in code-behind:
btn2.SetResourceReference(BackgroundProperty, "BackgroundBrush1");
24. What is Style in WPF?
A style is group of properties and its value.
Styles are reusable code that are used for creating a consistent appearance for the Application. Below is the example of Style definition.
<Style x:Key="ButtonStyle">
<Setter Property="Button.Height" Value="90" />
<Setter Property="Button.Width" Value="210" />
</Style>
25. How can you assign a style to a control of WPF?
You have two methods for assigning a style to a control.
- Set Key property of Style
- Set TargetType property of Style
26. What is the difference between Key and TargetType properties of Style?
Key property let you assign the style with only particular control of WPF on which you assign the style property.
<Button Style="{StaticResource ButtonStyle}">Button 1</Button>
and when you set the TargetType property of style, then the style will be automatically applied to exact same type of control which you have defined in the TargetType.
<Style TargetType="Button">
<Setter Property="Width" Value="100" />
</Style>
27. What are WPF Triggers?
Triggers in WPF are used to change control’s visual properties based on some particular condition.
Triggers have a very special behavior when the condition is false it automatically change the styles to their original values which have before the condition is true.
28. How many types of Triggers available in WPF?
There are five types of Triggers available.
- Property Trigger
- Event Trigger
- Data Trigger
- Multi-condition Data Trigger
- Multi-condition Property Triggers
29. What is Property Trigger?
Property Trigger checks the value of control property to determine whether to change visual properties values of a control or not.
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFFF0000" />
</Trigger>
30. What is Data Trigger?
Data Trigger is used in data binding scenarios. In the condition of trigger, we can check the bounded value and then apply new properties value to a control.
<DataTrigger Binding="{Binding ItemType}" Value="Mobile">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
31. What is Event Trigger?
Event Trigger is used for start animation based on some particular event of a control.
<EventTrigger RoutedEvent="Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
In the above code if you click on the button, then start the storyboard class which is used for animation and set the Opacity property to 1 in the duration of 5 seconds.
32. What is Multi-condition Data Trigger?
Multi-condition is just like a Data Trigger but you can define multiple conditions.
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Name}" Value="Kapil" />
<Condition Binding="{Binding ItemType}" Value="Mobile" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="LightBlue" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
33. What is Multi-condition Property Trigger?
 Multi-condition Property Trigger is just like property trigger but you can define multiple conditions.
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="Background" Value="LightBlue" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Red" />
</MultiTrigger>
34. Tell me about different layout controls in WPF?
There are six layout controls in WPF.
- Canvas
- DockPanel
- Grid
- StackPanel
- UniformGrid
- WrapPanel
35. What is Canvas Panel?
Canvas is a panel control that arrange its child controls by position and size. All its children controls has to specify Canvas attach properties (Left, Top, Bottom, Right) to specify their position in the Canvas.
36. What is DockPanel Control?
DockPanel control arrange its chil controls into the edge of its boundary.
Every child has to specify DockPanel attach properties (Left, Right, Top, Bottom) to specify their position.
37. What is LastChildFill property in DockPanel?
This property allows the last child control to fill the remaining space of DockPanel.
38. What is Grid Control in WPF?
Grid control arranges its child controls in rows and columns. You have to define RowDefinitions and ColumnDefinitions to specify the rows and columns of Grid.
39. Can you specify row height and column width in percentage in WPF Grid control?
Yes.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="2* />
</Grid.RowDefinitions>
</Grid>
In the above code, I have defined 1/3 part to the first row and 2/3 part to the second row. Same can be define with column definitions.
40. Can we define Auto in rows and column sizes in Grid control?
Yes, We can define Auto in RowDefinition and ColumnDefinition in Grid control.
Auto means takes only those space which is required by child control.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="* />
</Grid.RowDefinitions>
<Button Grid.Row="0" />
<ContentControl Grid.Row="1" />
</Grid>
In the above code, first row will only take height which is required by the Button control, and the rest space is taken by ContentControl.