Getting binding expressions from code behind from Datatemplate Triggers - c#

I've been set to maintain a wpf application where there is a listbox for logging purposes.
The items displayed using listbox are of type TextMessage, i.e. the listbox is bound to these text messages via
ObservableCollection<TextMessage> Messages;
listBox.DataContext = Messages;
Messages are then added with something like
Messages.Add(new TextMessage("Test", TypeOfMessage.Headline));
This is the definition of the class TextMessage
public enum TypeOfMessage
{
Normal,
Headline,
Focus,
Important,
Fail,
Success
}
public class TextMessage
{
public TextMessage(string content, TypeOfMessage typeOfMessage)
{
Content = content;
TypeOfMessage = typeOfMessage;
CreationTime = DateTime.Now;
}
public string Content { get; }
public TypeOfMessage TypeOfMessage { get; }
public DateTime CreationTime { get; }
}
The xaml definition for the listbox is something like this:
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="196" Margin="101,77,0,0" VerticalAlignment="Top" Width="256" ItemsSource="{Binding}" SelectionMode="Multiple">
<ListBox.InputBindings>
<KeyBinding
Key="C"
Modifiers="Control"
Command="Copy"
/>
</ListBox.InputBindings>
<ListBox.CommandBindings>
<CommandBinding
Command="Copy"
Executed="DoPerformCopy"
/>
</ListBox.CommandBindings>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="TextToShow" Text="{Binding Content}"></TextBlock>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding TypeOfMessage}" Value="Normal">
<Setter TargetName="TextToShow" Property="Foreground" Value="Black"/>
</DataTrigger>
<DataTrigger Binding="{Binding TypeOfMessage}" Value="Focus">
<Setter TargetName="TextToShow" Property="Foreground" Value="Black"/>
<Setter TargetName="TextToShow" Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding TypeOfMessage}" Value="Headline">
<Setter TargetName="TextToShow" Property="Foreground" Value="RoyalBlue"/>
<Setter TargetName="TextToShow" Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding TypeOfMessage}" Value="Important">
<Setter TargetName="TextToShow" Property="Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding TypeOfMessage}" Value="Fail">
<Setter TargetName="TextToShow" Property="Foreground" Value="Red"/>
<Setter TargetName="TextToShow" Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding TypeOfMessage}" Value="Success">
<Setter TargetName="TextToShow" Property="Foreground" Value="Green"/>
<Setter TargetName="TextToShow" Property="FontWeight" Value="Bold"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This works nicely (i.e messages are displayed in the listbox in different font weight and color depending on their type), but now for the question :
Is there any way using BindingExpression or any other means to get the font formatting and coloring from code behind from the xaml definitions ?
The reason is that I want to just have the formatting in one place (just in the xaml as it is right now) but still be able to reuse it when I want to copy the contents (using code behind) including font formatting to the clipboard.
Example:
private void DoPerformCopy()
{
RichTextBox rtb = new RichTextBox();
foreach (TextMessage message in (listBox as ListBox)?.SelectedItems.Cast<TextMessage>().ToList())
{
TextPointer startPos = rtb.CaretPosition;
rtb.AppendText(message.Content);
rtb.Selection.Select(startPos, rtb.CaretPosition.DocumentEnd);
//
// Here it would be very nice to instead having multiple switch statements to get the formatting for the
// TypeOfMessage from the xaml file.
SolidColorBrush scb = new SolidColorBrush(message.TypeOfMessage == TypeOfMessage.Fail ? Colors.Red);
//
rtb.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty, scb);
}
// Now copy the whole thing to the Clipboard
rtb.Selection.Select(rtb.Document.ContentStart, rtb.Document.ContentEnd);
rtb.Copy();
}
Since I'm new to wpf, I'd really appreciate if someone has a tip for solving this. (I've tried hard to find an solution here at stackoverflow, but so far I've been unsuccessful)
Thanks in advance,
King regards
Magnus

Make a ContentPresenter with Content set to your TextMessage. Set the ContentTemplate to listBox.ItemTemplate and apply the template. It will create the visuals (TextBlock in this case). Then, just parse off the values from the TextBlock.
Also, your RichTextBox selection code wasn't working quite right so I fixed that by just inserting TextRanges to the end of it instead of trying to get the selection right.
private void DoPerformCopy(object sender, EventArgs e)
{
RichTextBox rtb = new RichTextBox();
foreach (TextMessage message in (listBox as ListBox)?.SelectedItems.Cast<TextMessage>().ToList())
{
ContentPresenter cp = new ContentPresenter();
cp.Content = message;
cp.ContentTemplate = listBox.ItemTemplate;
cp.ApplyTemplate();
var tb = VisualTreeHelper.GetChild(cp, 0) as TextBlock;
var fg = tb.Foreground;
var fw = tb.FontWeight;
var tr = new TextRange(rtb.Document.ContentEnd, rtb.Document.ContentEnd);
tr.Text = message.Content;
tr.ApplyPropertyValue(RichTextBox.ForegroundProperty, fg);
tr.ApplyPropertyValue(RichTextBox.FontWeightProperty, fw);
}
// Now copy the whole thing to the Clipboard
rtb.Selection.Select(rtb.Document.ContentStart, rtb.Document.ContentEnd);
rtb.Copy();
}

Related

How to Convert XAML to c#?

I'm using accordion control.
<dxa:AccordionControl.Resources>
<Style TargetType="dxa:AccordionItem">
<Setter Property="Foreground" Value="Orange"/>
<Style.Triggers>
<Trigger Property="IsMouseOverHeader" Value="True">
<Setter Property="Foreground" Value="black"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</dxa:AccordionControl.Resources>
But I don't use it anymore ↑
because I changed the contents of the item dynamically.
Setter setter = new Setter();
Style style2 = new Style();
style2.TargetType = new AccordionItem().GetType();
setter.Property = AccordionItem.ForegroundProperty;
setter.Value = Brushes.Red;
style2.Setters.Add(setter);
ResourceDictionary resourceDictionary = new ResourceDictionary();
resourceDictionary.Add(style2.TargetType, style2);
//Trigger trigger = new Trigger();
//trigger.Property = AccordionItem.IsMouseOverHeaderProperty;
//trigger.Value = true;
accordionControlHistoryMenu.Resources = resourceDictionary;
How Can I express it this xamlcode convert xaml to C# source?
You don't need to store the style in a ResourceDictionary, just assign it directly:
Xaml:
<dxa:AccordionControl.Resources x:Name="myControl">
Code:
myControl.Style = style2;
While this answers your question, it's almost never the correct way to do this. Your styles should be binding to dynamic data that your view model layer is creating.

How to properly color the last rows in a datagrid [duplicate]

Can you guy tell me a great way of coloring the last row in a datagrid in WPF, I have to color the first and last rows, I found a way to do the first one by doing the code below, but I need a way to do the last row.
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource Mode=PreviousData}}"
Value="{x:Null}">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
This is a tough one.
You may use the alternation index with a multi-binding on the ItemsSource and the AlternationCount, a IMultiValueConverter, and finally fail because the alternation index may not start from 0. (also, you loose the benefit of an AlternationCount of 2).
You may use code behind and hack the DataGrid to display the hard coded color you want. But that is not MVVM.
You may put a special boolean on your view model to mark those items as special. But this feels to be the wrong place.
...
My approach is to inherit the DataGrid object in a class that will keep up-to-date a ‘IsAnExtremity’ attached property (a pretty touchy pattern) on it's DataGridRow:
public class DataGridEx : DataGrid
{
private static readonly DependencyPropertyKey IsAnExtremityPropertyKey =
DependencyProperty.RegisterAttachedReadOnly(
"IsAnExtremity",
typeof(bool),
typeof(DataGridEx),
new FrameworkPropertyMetadata(defaultValue: false,
flags: FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty IsAnExtremityProperty = IsAnExtremityPropertyKey.DependencyProperty;
public static bool GetIsAnExtremity(DataGridRow dataGridRow)
{
return (bool)dataGridRow.GetValue(IsAnExtremityProperty);
}
private static void SetIsAnExtremity(DataGridRow dataGridRow, bool value)
{
dataGridRow.SetValue(IsAnExtremityPropertyKey, value);
}
private IReadOnlyList<DataGridRow> _extremities = Array.Empty<DataGridRow>();
protected override void OnLoadingRow(DataGridRowEventArgs e)
{
base.OnLoadingRow(e);
UpdateExtremities();
}
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
UpdateExtremities();
}
private void UpdateExtremities()
{
// Current extremities
var extremities = new[]
{
ItemContainerGenerator.ContainerFromIndex(0),
ItemContainerGenerator.ContainerFromIndex(Items.Count - 1)
}.OfType<DataGridRow>()
.Distinct()
.ToArray();
// Remove the flag from old extremities (if any).
foreach (var oldExtremityContainer in _extremities.Except(extremities))
{
SetIsAnExtremity(oldExtremityContainer, false);
}
// Ensure the flag for new extremities.
foreach (var extremityContainer in extremities)
{
SetIsAnExtremity(extremityContainer, true);
}
_extremities = extremities;
}
}
Then you can use this attached property almost like any other properties in the xaml:
<controls:DataGridEx ItemsSource="{Binding Path=Items}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="LightBlue" />
<Style.Triggers>
<Trigger Property="controls:DataGridEx.IsAnExtremity" Value="True">
<Setter Property="Background" Value="Blue" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</controls:DataGridEx>
A working implementation is available here.

DataGridHyperlinkColumn text color when highlighted

I have a simple WPF application which displays reddit links in a DataGrid:
Notice however that the link in the DataGridHyperlinkColumn isn't visible when a row is selected, due to the color of the link and the color of the row highlight.
What's a good way to resolve this? Change the link text color? Change the row highlight color?
If possible, please show your suggestion in terms of C# code as opposed to XAML as this application isn't using XAML. Otherwise, a XAML solution is fine; I'll just convert it to C#. :-)
For reference, here's the code used for the Title column:
var event_setter = new EventSetter()
{
Event = Hyperlink.ClickEvent,
Handler = (RoutedEventHandler)((sender, e) =>
{
System.Diagnostics.Process.Start((data_grid.SelectedItem as Link).Url);
})
};
var style = new Style();
style.Setters.Add(event_setter);
var hyperlink_column = new DataGridHyperlinkColumn()
{
Header = "Title",
Binding = new Binding("Title"),
ElementStyle = style,
Width = 600
};
data_grid.Columns.Add(hyperlink_column);
You could add an implicit Hyperlink style to your DataGrid:
const string Xaml = "<Style TargetType=\"Hyperlink\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
"<Style.Triggers>" +
"<DataTrigger Binding=\"{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridCell}}\" Value=\"True\">" +
"<Setter Property=\"Foreground\" Value=\"White\" />" +
"</DataTrigger>" +
"</Style.Triggers>" +
"</Style>";
data_grid.Resources.Add(typeof(Hyperlink), System.Windows.Markup.XamlReader.Parse(Xaml) as Style);
data_grid.Columns.Add(hyperlink_column);
The Selector.IsSelected property of DataGridHyperLink Column can be used and when the selection on particular item changes you can update the style with trigger.
<DataGridHyperlinkColumn.CellStyle>
<Style TargetType="{x:Type Hyperlink}">
<Setter Property="Foreground" Value="Blue"/>
<Style.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Trigger.Setters>
<!--change the value for the property based on your needs-->
<Setter Property="Foreground" Value="Yellow"/>
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</DataGridHyperlinkColumn.CellStyle>
Pure XAML solution:
<DataGrid>
<DataGrid.Resources>
<Style TargetType="{x:Type Hyperlink}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridCell}}"
Value="True">
<DataTrigger.Setters>
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridHyperlinkColumn Width="180"
Header="Url"
Binding="{Binding Path=Uri, Mode=OneWay}" />
</DataGrid.Columns>
</DataGrid>
Here's a version of the answer provided by #mm8 converted from XAML to C#:
var data_trigger = new DataTrigger()
{
Binding = new Binding()
{
Path = new PropertyPath("IsSelected"),
RelativeSource = new RelativeSource() { AncestorType = typeof(DataGridCell) }
},
Value = true
};
data_trigger.Setters.Add(new Setter(ForegroundProperty, new SolidColorBrush(Colors.White)));
var style = new Style(typeof(Hyperlink));
style.Triggers.Add(data_trigger);
data_grid.Resources.Add(typeof(Hyperlink), style);
Here's a version of the answer provided by #mm8 converted from XAML to C# which uses some extension methods to avoid intermediate variables:
data_grid.Resources.Add(
typeof(Hyperlink),
new Style(typeof(Hyperlink))
.AddTrigger(
new DataTrigger()
{
Binding = new Binding()
{
Path = new PropertyPath("IsSelected"),
RelativeSource = new RelativeSource() { AncestorType = typeof(DataGridCell) }
},
Value = true
}
.AddSetter(new Setter(ForegroundProperty, new SolidColorBrush(Colors.White)))));

WPF XAML datatrigger fails to fire after programmatically changing property value C#

I have this code in my xaml which says to color my button when I hover my mouse and click my mouse over the button.
<Border x:Class="DatasetGrid.RowHeaderButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" MinWidth="30" Width="Auto">
<Border.Resources>
<SolidColorBrush x:Key="ButtOverBrush" Color="#53C3D5" Opacity="0.2"></SolidColorBrush>
<SolidColorBrush x:Key="ButtPressedBrush" Color="#53C3D5" Opacity="0.5"></SolidColorBrush>
</Border.Resources>
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background" Value="Transparent"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource ButtOverBrush}"></Setter>
</Trigger>
<DataTrigger Binding="{Binding IsMouseDown, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="{StaticResource ButtPressedBrush}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
This works all well and good, but I find that as soon as I change the Background color in code behind, the above MouseOver and MouseDown triggers don't fire anymore.
RowHeaderButton rhb = RowHeadersColumn.VisibleRowHeaders[cell.CellInfo.RowIndex];
rhb.Background = new SolidColorBrush(Color.FromArgb(100, 83, 195, 213));
I'm quite new to WPF so I'm not sure what's going wrong.
Edit:
So to give some more information, my control above is a RowHeaderButton, i.e the row header to a grid. Each row in the grid has it's own row header button. So when the user hovers over or clicks it, it should change from white to the specified SolidColorBrush above.
In the code behind of another control, DataGrid.xaml.cs, I have the below code (simplified) which will change the color of the row header when when a cell in the same row of the grid is selected or not.
void UpdateSelectedCells() {
foreach (Cell cell in VisibleColumns.SelectMany(c => c.VisibleCells))
{
int cellRowIndex = cell.CellInfo.RowIndex;
cell.IsSelected = SelectedCells.Contains(cell.CellInfo);
foreach (RowHeaderButton rhb in RowHeadersColumn.VisibleRowHeaders)
{
int rowHeaderIndex = Convert.ToInt16(rhb._default.Text) - 1;
if (cellRowIndex == rowHeaderIndex)
{
if (cell.IsSelected)
{
rhb.Background = new SolidColorBrush(Color.FromArgb(100, 83, 195, 213));
}
else
{
bool rowselected = false;
//need to check if any other cell in the row is selected, if not then color row header white
foreach (CellInfo celll in SelectedCells)
{
if (celll.RowIndex == cellRowIndex)
{
rowselected = true;
break;
}
}
if (rowselected == false)
rhb.Background = Brushes.White;
}
}
}
}
}
I don't have a ViewModel for this.
The triggers are firing, but their setters are being overridden.
This is due to Dependency Property Value Precendence. If the Background property is set programmatically or as an attribute in the XAML, that value will override anything value any style setter gives it. In general, this is desirable behavior: You want to be able to override what the style does on an individual control.
The solution to this is to do all of your background brush changes in style triggers. Your code behind must have some reason for setting the background brush when it does. Whatever that is, find a way to do it with a trigger. Set a property on the viewmodel and write a trigger on that property.
If you need help translating that high level abstraction into your own code, please share enough code for me to understand why and where the codebehind is setting the Background, and what (if anything) you have for a viewmodel.
I solved the issue by creating a new Dependancy Property and binding it to a data trigger.
public bool IsCellSelected
{
get { return (bool)GetValue(IsCellSelectedProperty); }
set { SetValue(IsCellSelectedProperty, value); }
}
public static readonly DependencyProperty IsCellSelectedProperty =
DependencyProperty.Register("IsCellSelected", typeof(bool), typeof(RowHeaderButton), new PropertyMetadata(null));
In my xaml I have:
<DataTrigger Binding="{Binding IsCellSelected, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Background" Value="{StaticResource ButtPressedBrush}"></Setter>
</DataTrigger>
And in my code behind I set the value using:
RowHeaderButton rhb = RowHeadersColumn.VisibleRowHeaders[cell.CellInfo.RowIndex];
rhb.IsCellSelected = true; //or false
Now my button hover and button click events are not overridden.

WPF Datagrid DataBinding DataView

Pre-Warning sorry WPF new guy here:
I have a DataGrid bound to a DataTable's DefaultView
ResultDataGrid.ItemsSource = resultTable.DefaultView;
I know the column names, and I need to change a column's foreground if another column is 1 (always 0 or 1)
Currently what I have:
private void ResultDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.Column.Header.ToString() == "columnName")
{
e.Column.CellStyle = FindResource("columnStyle") as Style;
}
}
and in XAML:
<Window.Resources>
<Style TargetType="DataGridCell" x:Key="columnStyle">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding resultTable, Path={StaticResource otherColumnName}}" Value="1">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
Where otherColumnName is set in the constructor
public ResultsCustom(DataTable resultclass, CustomQuery query)
{
// Some other stuff
this.Resources.Add("otherColumnName", COLUMN_NAME);
}
The XAML Style seems to not have the correct path, any help would be appreciated!
I'm not sure about this line of code:
Path={StaticResource otherColumnName}
Are you attempting to compare the property "otherColumnName" of the resultTable to see if this is 1? This wouldn't be a static resource, and you could change your binding to:
Path=otherColumnName

Categories

Resources