DataGrid AutoGenerateColumns ErrorTemplate - wpf

I'm trying override default DataGridTextBoxColumn ErrorTemplate. I can do this if I manually set DataGridTextBoxColumn but I must using autogeneratecolumns, then I can't style this... Any ideas?
Datagrid default cell ErrorTemplate
Thanks..

What you get when you're editing a cell in a datagridtextboxcolumn is a TextBox.
I don't have something set up I can test this with, but it won't really fit in a comment.
I would think if you just apply a style to textbox then that will work.
Put a style in the Datagrid.resources:
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
And, of course, whatever template you want to use in there.

Related

Default DataGridComboBoxColumn ControlTemplate

Without bothering you with what I intend to do, could someone kindly point out where I can get the default DataGridComboBoxColumn ControlTemplate. I am sure there must be a Control Template or some kind of style that targets a DataGridComboBoxColumn, otherwise how did Microsoft build the DataGridComboBoxColumn.
Without bothering you with what I intend to do
I think it is not so unimportant, because of these advises on MSDN page DataGridComboBoxColumn Class:
Represents a DataGrid column that hosts ComboBox controls in its
cells.
and
If you want to use other controls in your DataGrid, you can create
your own column types by using DataGridTemplateColumn.
For styling (also ControlTemplate!) of ComboBox you can use ElementStyle and EditingElementStyle properties of DataGridComboBoxColumn.
Default template for ComboBox you can find here: ComboBox Styles and Templates
Small example:
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<TextBlock Text={Binding SomePropertyOfYourRowDataContext}/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.ElementStyle>
Could someone kindly point out where I can get the default DataGridComboBoxColumn ControlTemplate.
A DataGridComboBoxColumn is not a Control and hence it has no ControlTemplate.
It creates a ComboBox in its GenerateEditingElement method and it is this ComboBox that you see when the cell of a DataGridComboBoxColumn is in edit mode: https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/DataGridComboBoxColumn.cs,90924e66a85fbfa4.

How do you expand an element to fill an Expanders Header?

I'm trying to expand an element to fill the horizontal space of an Expander.Header. I asked a similar question earlier, though I have since realized that the premises of the question was incorrect. Any use of Grids, or DockingPanels seems to have no effect on the ability for an element to fill the horizontal space of a Expander.Header.
Some more digging revealed that the ContentPresenter for the header is automatically set to HorizontalAlignment.Left. How do I go about changing this?
The only way to do that is finding the source xaml for the expander and modify it the way you want, creating your own expander style.
What you're looking for is a ControlTemplate. See here. You have to override the default template for the Expander.
You can declare a template which will affect all Expander controls using the following:
<ControlTemplate TargetType="Expander">
...
</ControlTemplate>
Or, you can set the Template property in a Style.
<Style TargetType="Expander">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Expander">
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

How to change wpf listviewitem background color when its data is using binding?

I'm trying to change listview's row background color for a data binding listview.
From msdn, I know there is ListViewsItem.Backgroud for the color of its background.
But since I'm using data binding for ListView's data source,the type of ListView's item is actually my class type, no longer ListViewItem. So I can't find its Background property.
I guess I missed something, how should I do it?
Thanks
You can set ItemContainerStyle of your listview like below to set any property on item.
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Red"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
You even change the background depending of dataitem state using triggers in style.

silverlight 3.0 grid row selector color

is it possible to specify the color for the row selector in silverlight grid
Yes but you need to copy the control template for the DataGridRowHeader control and place it in a Style object in a resource:-
<UserControl.Resources>
<Style x:Key="CustomRowHeader" TargetType="DataGridRowHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="localprimitives:DataGridRowHeader">
<!-- Copy of the rest of the standard controls template -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<UserControl.Resources>
<DataGrid RowHeaderStyle="{StaticResouce CustomRowHeader}" ... >
Now you can fiddle around with the color value and pretty much anything else that is used to render the row selector.
You can probably do this with Blend fairly well if you have it and are familiar with using it. I prefer to copy the template from the documentation. See DataGrid Styles and Templates
No, but it's perfectly possible to subdivide a grid into rows/columns and fill them with rectangles+background or something like that.

Can you set the content of a button in wpf with a style?

When I try this, all the buttons turn blue, but the content isn't set.
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Content" Value="Edit"/>
</Style>
</Window.Resources>
Any ideas?
EDIT : This example was indeed somewhat too oversimplified. My problem was in changing styles in runtime, where the color was changing allright, but not the content. The solution for me was not to initialize content in xaml, but in code.
Remember if you set the content property in Style, but if you give some other content in inline xaml it will not work for example if you use
Sometimes XAML Editor like Blend will put Content="" if you just try to delete the content, it will not remove attribute, you will have to check.
So even if your XAML contains Content="" or your code initializes Content Property to even null or empty string, it will not work.
Works for me (tried it in kaxaml). But black text on a Blue background can be hard to read. You sure its not there?

Resources