I have several projects in which I have for exemple, textboxes that I would like to behave the same way always. For evertyhing like background to lenght, it's fine, but I also would like to add base event handler (in this case, the got focus event).
Is this possible and if so how ?
Thanks.
Edit : here is an example :
<Style x:Key="BaseComboBox" TargetType="ComboBox">
<Setter Property="FontSize" Value="12"></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Height" Value="22"/>
<Setter Property="Margin" Value="5,0,5,0"/>
<Setter Property="IsEditable" Value="True" />
<Add LostFocus Event that will validate the selection here...>
</Style>
All my styles are in Resources Dictionaries
<Style x:Key="MyStyle">
<EventSetter Event="Control.GotFocus" Handler="Control_GotFocus"></EventSetter>
</Style>
Why not to use common approach for validation in your project instead of creating something weird?
Try to read this WPF Data Binding and Validation Rules Best Practices
Related
I'm new to WPF and have run into a bit of a problem. I am trying to remove the blue gradient effect on a combobox (e.g. when the user mouses over it) in order to create a more flat UI aesthetic, but I haven't been able to figure out how. Most of the solutions I've seen while googling involve doing somthing like:
<Style x:Key="myComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<!-- and then the rest of the style re-implements the combobox from scratch... -->
That seems a bit much just to remove one minor effect. I've also tried doing things like:
<Style x:Key="myComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="Foreground" Value="LightGray"/>
<Setter Property="BorderBrush" Value="DarkGray"/>
<Setter Property="Focusable" Value="False" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGray"/>
</Trigger>
</Style.Triggers>
But that doesn't seem to have any effect either. Does anyone know what setting I might change to remove the hover effect?
I'm trying to figure out how to change the style of the AvalonEdit CodeCompletion window. However, I can't figure out the right combination of xaml style target/properties to change it. The main thing I'd like to do is get rid of the border, but maybe some additional changes as well.
Here is the xaml I've tried. None of it is affecting the UI.
xmlns:ae="clr-namespace:ICSharpCode.AvalonEdit.CodeCompletion;assembly=ICSharpCode.AvalonEdit"
<Style TargetType="{x:Type ae:CompletionWindow}">
<Setter Property="WindowStyle" Value="None" />
</Style>
<Style TargetType="{x:Type ae:CompletionWindowBase}">
<Setter Property="WindowStyle" Value="None" />
</Style>
<Style TargetType="{x:Type ae:CompletionListBox}">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type ae:CompletionList}">
<Setter Property="Background" Value="Orange" />
</Style>
Use this style to remove border on window:
<Style TargetType="{x:Type avalonEdit:CompletionWindow}">
<Setter Property="WindowStyle" Value="None"></Setter>
<Setter Property="ResizeMode" Value="NoResize"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
</Style>
To make the styles affect the UI, you can put them in a resource dictionary xaml and parse that with (ResourceDictionary)XamlReader.Parse(ResourcesAsXaml).
Then assign the ResourceDictionary to the Resources property of the CompletionWindow.
One of my Grid currently starts with the following code:
<Grid x:Name="Top_GRID" Margin="4.953,10" Width="817.28">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
<Style TargetType="Button">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
</Grid.Resources>
Just to clarify - I want to declare a Grid in which all TextBlocks are having the Background property set to "Red". All Button margins are set to "3" and so on.
I'd like to move the resource definition to a dictionary file.
Should I somehow wrap it as a Style?
If so, I'm going to have a recursive style declaration which (I think is illegal).
Sounds simple but I can't find the way to do it.
Try this
<Style x:Key="Grid_ControlStyle" TargetType="Grid">
<Style.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
<Style TargetType="Button">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
</Style.Resources>
</Style>
You Need to put all styles for button, TextBox etc. in resourceDictionary file. Then add this file in application resources:
<Application x:Class="Aplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\YourResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then in xaml you will be able to use it like this
<TextBlock Grid.Column="1" Grid.Row="1" Text="bla bla" Style="{DynamicResource YourStyle}"/>
Finally your style should look like
<Style x:Key="StyleName" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="3,3,3,3"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="12pt"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
Hope thats what you was looking for.
Please go through the concept of resource dictionary in WPF. Any style, colors, fonts etc whatever is related to look of the application which you wish to be repeated in more than one screen of your application shpuld be placed in the resource dictionary.
x:Key is the property which can be used to access the style anywhere across the application.
For your resource dictionary to be accessible througout the application, place in it app.xaml
In a WPF user control declaration I have the following style define:
<UserControl.Resources>
<Style x:Key="Datagrid" TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="HeadersVisibility " Value="Column"/>
<Setter Property="VerticalGridLinesBrush " Value="{StaticResource DatagridVerticalLinesBrush}"/>
<Setter Property="HorizontalGridLinesBrush " Value="Transparent"/>
<Setter Property="RowHeaderWidth " Value="0"/>
<Setter Property="CanUserAddRows " Value="False"/>
<Setter Property="CanUserDeleteRows " Value="False"/>
</Style>
</UserControl.Resources>
The problem is that the Visual Studio 2012 designer thinks the properties don't exist on the type DataGrid. It says that: the member "XXXXXXXXXXXX" is not recognized or is not accessible.
Despite the errors, the style is applied correctly at run time and the properties exist on the DataGrid (they are DependencyProperies) and are public.
Any idea what could be causing it to think they don't exist or why are they inaccessible to the designer?
By the way, the Background property is ok. It's only the other 7 that have errors.
I have loaded this up in VS2012, created a UserControl, added the above style. I observed that the Background and BorderBrush properties are seen as valid, but the others are not.
Then...I noticed the spaces in the quoted names. Once removed all is well...
<UserControl.Resources>
<Style x:Key="Datagrid" TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HeadersVisibility" Value="Column"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="VerticalGridLinesBrush" Value="{StaticResource DatagridVerticalLinesBrush}"/>
<Setter Property="HorizontalGridLinesBrush" Value="Transparent"/>
<Setter Property="RowHeaderWidth" Value="0"/>
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
</Style>
</UserControl.Resources>
It may be a cut/paste error but you have an extra space before the closing double-quotes on all the property names except for Background and BorderBrush. The parser probably trims the property name before reflection so it's able to find it at runtime.
It was my understanding that this:
<Style TargetType="{x:Type Image}">
<Setter Property="Margin" Value="0"/>
<Setter Property="Stretch" Value="None"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Cursor" Value="Help"/>
</Style>
would change the default image settings for each image rendered on the Window. Is this not the case? Is there an exception for images as it appears to work with other controls?
TIA
To set a style for a type you actually need to set the x:Key attribute like so:
<Style x:Key="{x:Type Image}" TargetType="{x:Type Image}">
<!-- setters here -->
</Style>