How to add new setter programatically in existing ItemContainerStyle in WPF? - wpf

I have item container style for list view like below :
<ListView.ItemContainerStyle>
<Style>
<Setter Property="Padding" Value="0"></Setter>
<Setter Property="Margin" Value="0,0,0,-1"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=EventType}" Value="2">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="White"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
Now I need to add new setter for context menu programatically based on item data. How can I do it ?
Please guide me.....
Thanks

I have to display Images dynamically and show/hide depends on state of checkboxes using Style and DataTrigger.
<Image Source="/WpfApplication;component/Imgs/img1_1.png">
<Image.Style>
<Style>
<Setter Property="Image.Visibility" Value="Collapsed"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=isDisplayingImgSet1, Path=IsChecked}" Value="True">
<Setter Property="Image.Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
After spec modification i have to create checkboxes dynamically, then to set Style.
At beginning, I have the same error message.
Image img = new Image();
//..
img.Style.Setters.Add(setter);
img.Style.Triggers.Add(trigger);
//..After a 'SetterBaseCollection' is in use (sealed), it cannot be modified.
The solution is to create a style object and Affect it to Image.Style
//Visibility
DataTrigger trigger = new DataTrigger();
trigger.Binding = new Binding
{
ElementName = "isDisplayingImgSet"+NumSet,
Path = new PropertyPath(CheckBox.IsCheckedProperty)
};
trigger.Value = "True";
trigger.Setters.Add(new Setter(ContentControl.VisibilityProperty, Visibility.Visible));
Setter setter = new Setter(ContentControl.VisibilityProperty, Visibility.Collapsed);
Style style = new Style(typeof(Image));
style.Setters.Add(setter);
style.Triggers.Add(trigger);
img.Style = style;
Hope that helping you
PS : It's my first post

I have implemented logic to add setter dynamically in existing style like below :
listview1.ItemContainerStyle.Setters.Add(new Setter(Control.ContextMenuProperty,GetItemContextMenu(txtName.Text)));
But it gives me following error :
"After a 'SetterBaseCollection' is in use (sealed), it cannot be modified."
I think it is not possible to add new setter in sealed style. So I have got another temporary solution like assign context menu to whole listview rather than it's item on preview mouse right click event. So context menu will be same for all items then managed in coding based on data of selected item.

<ListView.ItemContainerStyle>
<Style>
<Setter Property="Padding" Value="0"></Setter>
<Setter Property="Margin" Value="0,0,0,-1"></Setter>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=EventType}" Value="2">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>

If you create a new Style() and "re-add" the the setters, you can get around this issue if you are using Telerik.
private static void AddColumnSetter(GridViewColumn gridColumn, SetterBase setter)
{
var setters = new SetterBaseCollection();
if (gridColumn.CellStyle != null)
{
foreach (SetterBase setterBase in gridColumn.CellStyle.Setters)
{
setters.Add(setterBase);
}
}
setters.Add(setter);
gridColumn.CellStyle = new Style();
foreach (var setr in setters)
{
gridColumn.CellStyle.Setters.Add(setr);
}
}

Related

Hiding context menu based on data trigger

I have a couple of grids in a user control. I want to hide the context menu in a grid based on a property in the DataContext. I have this code:
<Style TargetType="{x:Type ContextMenu}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsLockedNorthGrid}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
I know the IsLockedNorthGrid property works since I'm using it somewhere else in xaml. What am I missing?
Thanks
If you are using Style.Triggers to change the Visibility, be sure you are not setting Visibility to Context Menu inline. Since, inline property has higher priority over the style.
Check if the Visibility is set to a specific value (setting it in a specific element and triggering it won´t work). Also try
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsLockedNorthGrid}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
I was able to solve my problem like so:
<XamDataGrid ContextMenuOpeninig="OnContextMenuOpening">
<XamDataGrid.Resources>
<ContextMenu x:Key="GridContextMenu">...</ContextMenu> </XamDataGrid.Resources>
<XamDataGrid>
Code behind:
private void OnContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var logViewModel = (LogViewModelBase)DataContext;
var grid = sender as XamDataGrid;
var menu = grid.Resources["GridContextMenu"] as ContextMenu;
menu.Visibility = !logViewModel.IsLockedNorthGrid ? Visibility.Hidden : Visibility.Visible;
}
Not that pretty but it works.

How to make the first row of a xamDataGrid have a different background color?

I want to make the first row of a xamDataGrid a different color because it holds a different type of data than the rest of the grid.
I am not going to put null checks etc, just a few statements to to keep it concise..
var red = new SolidColorBrush(Colors.Red);
var item = dataGrid.Items[0];//or First(), FirstOrDefault()
var row = dataGrid.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;
row.Background = red;
or add a property to you class IsFirstRow and do something like below:
<Style TargetType="{x:Type telerik:GridViewRow}">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsFirstRow}" Value="True">
<DataTrigger.Setters>
<Setter Property="Foreground" Value="Red"/>
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>

How to make a WPF DataGrid with alternating row colors, and a fix color section (ignoring alternation)

I have a datagrid that I'm trying to make resemble:
I'm using the AlternatingRowBackground attribute to perform the alternating colors. For the fixed color section, I have XAML that resembles:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ShouldBeFixedColor}" Value="True">
<DataTrigger.Setters>
<Setter Property="Background" Value="Blue" />
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
The problem with this approach is that the "alternating color" takes precedence over the fixed color style trigger. So, at the bottom instead of blue-blue-blue it is blue-gray-blue.
Any ideas on how to archive the desired coloring? I'd rather do this all at the XAML level if possible.
Thanks!
Made some changes based upon other SO answers. Hopefully this helps someone in the future.
Yank AlternatingRowBackground=... from the grid. Add AlternationCount="2"
Add the block below to do the styling (manually doing the alternating rows)
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="0">
<Setter Property="Background" Value="White" />
</Trigger>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="WhiteSmoke" />
</Trigger>
<DataTrigger Binding="{Binding Path=Selectable}" Value="False">
<DataTrigger.Setters>
<Setter Property="Background" Value="LightGray" />
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
In case someone else is also looking for the same thing done in code:
Style rowStyle = new Style(typeof(DataGridRow));
Trigger rowTrigger = new Trigger();
rowTrigger.Property = DataGridRow.AlternationIndexProperty;
rowTrigger.Value = 0;
Setter rowSetter = new Setter(DataGridRow.BackgroundProperty, Brushes.Yellow);
rowTrigger.Setters.Add(rowSetter);
Trigger alternateRowTrigger = new Trigger();
alternateRowTrigger.Property = DataGridRow.AlternationIndexProperty;
alternateRowTrigger.Value = 1;
Setter alternateRowSetter = new Setter(DataGridRow.BackgroundProperty, Brushes.Pink);
alternateRowTrigger.Setters.Add(alternateRowSetter);
DataTrigger rowDataTrigger = new DataTrigger();
rowDataTrigger.Value = true;
rowDataTrigger.Binding = new Binding() { Path = new PropertyPath(nameof(MyObject.IsSomethingTrue))};
Setter backgroundSetter = new Setter(DataGridRow.BackgroundProperty, Brushes.Blue);
Setter foregroundSetter = new Setter(DataGridRow.ForegroundProperty, Brushes.White);
rowDataTrigger.Setters.Add(backgroundSetter);
rowDataTrigger.Setters.Add(foregroundSetter);
// the order of the triggers may not be changed as explained by CptCoathanger
rowStyle.Triggers.Add(rowTrigger);
rowStyle.Triggers.Add(alternateRowTrigger);
rowStyle.Triggers.Add(rowDataTrigger);
RootDataGridOrders.RowStyle = rowStyle;

Diffrent style for first three rows of WPF ListView

I want the top three items in my ListView to have special style. How can i achieve this?
I have tried this but item is always null:
if (tracklistQueue.Items.Count > 0) {
ListViewItem item = (ListViewItem)tracklistQueue.ItemContainerGenerator.ContainerFromIndex(0);
item.Style = (Style)FindResource("StyleName");
}
You can use AlternationIndex and AlternationCount properties.
Following example sets different background color for first three rows.
Add this style definition to your UserControl (or Window):
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#FFFF0000" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FF00FF00" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="#FF0000FF" />
</Trigger>
</Style.Triggers>
</Style>
Set AlternationCount of ListView to value which is greater than number of rows that ListView can actually contain:
<ListView AlternationCount="1000" />
Reference:
ItemsControl.AlternationCount Property
Solved it. The ItemContainerGenerator had not finished generating the items.
Added this to the constructor and put the code there:
this.tracklistQueue.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);

Underline Label in WPF, using styles

I have the following style:
<Style x:Key="ActionLabelStyle" TargetType="{x:Type Label}">
<Setter Property="Margin" Value="10,3" />
<Setter Property="Padding" Value="0" />
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
<Setter Property="FontFamily" Value="Calibri" />
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsEnabled" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Red" />
<Setter Property="TextBlock.TextDecorations" Value="Underline" />
</MultiTrigger>
</Style.Triggers>
</Style>
So basically, I want to have a label which is underlined when it is enabled and the mouse cursor is over it. The part of this style which is not working is the <Setter Property="TextBlock.TextDecorations" Value="Underline" />. Now, what am I doing wrong here? Thanks for all the help.
This is actually much more difficult than it appears. In WPF, a Label is not a TextBlock. It derives from ContentControl and can therefore host other, non-text controls in its Content collection.
However, you can specify a string as the content as in the example below. Internally, a TextBlock will be constructed to host the text for you.
<Label Content="Test!"/>
This internally translates to:
<Label>
<Label.Content>
<TextBlock>
Test!
</TextBlock>
</Label.Content>
</Label>
The simple solution to this would be for the TextDecorations property of a TextBlock to be an attached property. For example, FontSize is designed this way, so the following works:
<Label TextBlock.FontSize="24">
<Label.Content>
<TextBlock>
Test!
</TextBlock>
</Label.Content>
</Label>
The TextBlock.FontSize attached property can be applied anywhere in the visual tree and will override the default value for that property on any TextBlock descendant in the tree. However, the TextDecorations property is not designed this way.
This leaves you with at least a few options.
Use color, border, cursor, etc., instead of underlined text because this is 100% easier to implement.
Change the way you are doing this to apply the Style to the TextBlock instead.
Go to the trouble to create your own attached property and the control template to respect it.
Do something like the following to nest the style for TextBlocks that appear as children of your style:
FYI, this is the ugliest thing I've done in WPF so far, but it works!
<Style x:Key="ActionLabelStyle" TargetType="{x:Type Label}">
<Setter Property="Margin" Value="10,3" />
<Setter Property="Padding" Value="0" />
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
<Setter Property="FontFamily" Value="Calibri" />
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsEnabled" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Red" />
</MultiTrigger>
</Style.Triggers>
<Style.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Label}, Path=IsMouseOver}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="TextDecorations" Value="Underline"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
This works because it is overriding the default style of any TextBlock beneath a Label of this style. It then uses a MultiDataTrigger to allow relative binding back up to the Label to check if its IsMouseOver property is True. Yuck.
Edit:
Note that this only works if you explicitly create the TextBlock. I was incorrect when I posted this because I had already dirtied up my test Label. Boo. Thanks, Anvaka, for pointing this out.
<Label Style="{StaticResource ActionLabelStyle}">
<TextBlock>Test!</TextBlock>
</Label>
This works, but if you have to go to this trouble, you're just working too hard. Either someone will post something more clever, or as you said, my option 1 is looking pretty good right now.
Further to Jerry's answer, in order to avoid having to add the TextBlock into the template each time you can let the style do this too by adding the Setter property into the style:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<TextBlock>
<ContentPresenter />
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
Then your Label is back to:
<Label Content="Test!" Style="{StaticResource ActionLabelStyle}" />
Thanks Jerry!
Andrew.
I think the issue is that TextBlock.TextDecorations is not defined on Label.
You can use this approach if you're happy to use a TextBlock rather than a Label.
Just to add my workaround to the mix. I am currently using C# code and it works well enough. I just trap the MouseLeave and MouseEnter events and show the underline there.
void Control_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
WPFHelper.EnumerateChildren<TextBlock>(this, true).ForEach(c => c.TextDecorations = null);
}
void Control_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
WPFHelper.EnumerateChildren<TextBlock>(this, true).ForEach(c => c.TextDecorations = TextDecorations.Underline);
}
The WPFHelper class simply enumerates all the children of an DependencyObject and ForEach is an extension method that just does executes the action inside the lambda expression for each item.
An old question, but since I just fought with this, heres my method. Though it just uses a Trigger as opposed to a MultiTrigger
For XAML:
<Label Content="This text is for testing purposes only.">
<Style TargetType="{x:Type ContentPresenter}">
<Style.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextDecorations" Value="Underline" />
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
</Label>
For C# Codebehind:
public override void OnApplyTemplate()
{
if( !hasInitialized )
{
var tbUnderStyle = new Style( typeof(TextBlock), (Style)FindResource(typeof(TextBlock)) );
var tbUnderSetter = new Setter( TextBlock.TextDecorationsProperty, TextDecorations.Underline );
var tbUnderTrigger = new Trigger() { Property = Label.IsMouseOverProperty, Value = true };
tbUnderTrigger.Setters.Add( tbUnderSetter );
var contentPresenter = FindVisualChild<ContentPresenter>( this );
contentPresenter.Resources.Add( typeof(TextBlock), tbUnderStyle );
hasInitialized = true;
}
}
hasInitialized being a bool that is set to false in the constructor, and FindVisualChild sourced from here.

Resources