I want to draw some information's with Powershell on a background image. I use a WPF Form to write those informations, for better formatting/styleing. Problem is, that the DataGrid get cut in height, when I have to mush information's, which I not understand. I tryd with MaxHeight and different other propertys, but could not fix it.
Add-Type -AssemblyName PresentationFramework, System.Drawing
[void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
[xml]$xaml = #'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1" SizeToContent="WidthAndHeight"
Name="window" Height="Auto" Width="Auto" Background="Blue" WindowStyle="ToolWindow">
<Window.Resources>
<Style TargetType="Grid">
<Setter Property="ShowGridLines" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="Auto"/>
</Style>
<Style TargetType="TableCell">
<Setter Property="LineHeight" Value="Auto"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
<Style TargetType="RowDefinition" >
<Setter Property="Height" Value="Auto"/>
</Style>
<Style TargetType="TableColumn">
<Setter Property="Width" Value="Auto"/>
</Style>
<Style TargetType="TextBlock" >
<Setter Property="FontFamily" Value="Consolas" />
<Setter Property="FontSize" Value="20" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
</Style>
<Style TargetType="DataGrid">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="GridLinesVisibility" Value="All"/>
<Setter Property="HeadersVisibility" Value="None"/>
</Style>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Height" Value="Auto"/>
</Style>
<Style TargetType="DataGridCell">
<Setter Property="Height" Value="Auto"/>
<Setter Property="Foreground" Value="White" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FontFamily" Value="Consolas" />
<Setter Property="FontSize" Value="20" />
</Style>
</Window.Resources>
<Grid Name="Grid1" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Padding="0,0,0,20" FontSize="30"><TextBlock FontSize="30" Foreground="#FFCBCBCB">This</TextBlock> is my Header!</TextBlock>
<DataGrid Grid.Row="1" Grid.Column="0" Name="DG1" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="Auto" CanUserResize="False" CanUserReorder="False" />
<DataGridTextColumn Header="Spacer" Binding="{Binding Spacer}" Width="50" CanUserResize="False" CanUserReorder="False" />
<DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="Auto" CanUserResize="False" CanUserReorder="False" />
</DataGrid.Columns>
</DataGrid>
<TextBlock Grid.Row="2" Grid.Column="0" Padding="0,50,0,0">This is my Footer</TextBlock>
</Grid>
</Window>
'#
# Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{
$WpfApp1=[Windows.Markup.XamlReader]::Load( $reader )
}
catch{
Write-Error "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged or TextChanged properties (PowerShell cannot process them)"
throw
}
# Store Form Objects In PowerShell
$xaml.SelectNodes("//*[#Name]") | %{"trying item $($_.Name)";
try {Set-Variable -Name "WPF_$($_.Name)" -Value $WpfApp1.FindName($_.Name) -ErrorAction Stop}
catch{throw}
}
# Fülle Grid
for ($i=1; $i -le 60; $i++) {
$WPF_DG1.AddChild([pscustomobject]#{Name='Test';Value=$i})
}
$WPF_window.AllowsTransparency = $True
$WPF_window.Opacity = 0
$WPF_window.ShowInTaskbar = $False
$WPF_window.ShowActivated = $False
$WPF_window.WindowStyle = 'None'
$WpfApp1.Add_ContentRendered({
$WpfApp1.Close()
})
$WpfApp1.ShowDialog() | out-null
[int]$infoHeight = $WPF_Grid1.ActualHeight
[int]$infoWidth = $WPF_Grid1.ActualWidth
[string]::Format("WPF_Grid1 Größe: {0}x{1}", $infoWidth, $infoHeight) | Write-Host
# Speichere WPF-Grid in Bitmap
$rBmp = New-Object Windows.Media.Imaging.RenderTargetBitmap($infoWidth, $infoHeight, 96, 96, ([Windows.Media.PixelFormats]::Pbgra32))
$rBmp.Render($WPF_Grid1)
# Konvertiere Bitmap
$ms = New-Object System.IO.MemoryStream
$enc = New-Object System.Windows.Media.Imaging.BmpBitmapEncoder
$enc.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($rBmp))
$enc.Save($ms)
$infoImg = [System.Drawing.Image]::FromStream($ms)
$infoImg.MakeTransparent()
[string]::Format("infoImg Größe: {0}x{1}", $infoImg.width, $infoImg.height) | Write-Host
# Lese Quell-Bild
$srcImg = [System.Drawing.Image]::FromFile($Env:WinDir + "\Web\Screen\img100.jpg")
[string]::Format("srcImg Größe: {0}x{1}", $srcImg.width, $srcImg.height) | Write-Host
# Erstelle ein Bitmap in das gezeichnet wird
$dstBmp = New-Object System.Drawing.Bitmap(([int]($srcImg.width)),([int]($srcImg.height)))
# Erstelle Graphics Objekt
$gUnit = [Drawing.GraphicsUnit]::Pixel
$gImg = [System.Drawing.Graphics]::FromImage($dstBmp)
$gImg.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality
$gImg.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
$gImg.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$gImg.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
# Zeichne das Quell-Bild in unser gImg
$srcRect = New-Object Drawing.Rectangle(0, 0, $srcImg.Width, $srcImg.Height)
$gImg.DrawImage($srcImg, $srcRect, 0, 0, $srcImg.Width, $srcImg.Height, $gUnit)
# get scale ratio
$xpos = 150
$ypos = 150
$ratio = 0.1
while($($infoImg.Height*$ratio) -le $(($srcImg.Height-$ypos)*0.75))
{
$ratio += 0.1
#[string]::Format("if ({0} -le {1})", $($infoImg.Height*$ratio), $($srcImg.Height*0.75)) | Write-Host
}
[string]::Format("infoRect: X={0} Y={1} RATIO={2}", $xpos, $ypos, $ratio) | Write-Host
# Zeichne das Info-Bild in unser gImg
$infoRect = New-Object Drawing.Rectangle($xpos, $ypos, $($infoImg.Width*$ratio), $($infoImg.Height*$ratio))
$gImg.DrawImage($infoImg, $infoRect, 0, 0, $infoImg.Width, $infoImg.Height, $gUnit)
# Aufräumen
Remove-Item –Path "$($Env:Temp)\Test-*.jpg" -Force
# Neue Ausgabedatei erstellen
$dstFile = "$($Env:Temp)\Test-$(get-date -Format yyyy-MM-dd-HH-mm).jpg"
$dstBmp.save($dstFile, [System.Drawing.Imaging.ImageFormat]::Jpeg)
I added a full working test code (on win 10 - because of the background image, you can change also to other image), which shows the problem.
Check the resulting Test-*.jpg in %Temp% - we should see, the footer of the grid. When we change the for loop to max 30 the whole thing is working... but I need to display sometimes more informations.
Hope someone can point out what causes the problem. Found nothing identical on the net and was working on it for hours....
In WPF, when your app is rendered, the actual height is set to a maximum of the bounds of your monitor. It make sense, since in normal time you wouldn't want a window bigger than your monitor.
The snapshot you take of your window take only in consideration the rendered part, which is leaving out some elements in your case.
To circumvent this, you want to let the window go as big as it want.
Since you don't know the height of the window, what you can do here is to set an arbitrary MinHeight value to make sure your window encompass everything.
Since it won't be displayed on screen and we don't care if the window is out of bounds, we'll use 3000 here.
From there, we'll use your Add-ContentRendered scriptblock and calculate the actual height of our Grid (Header + Datagrid + Footer).
We'll then reset the MinHeight and Height attribute of our window to that size, so our snapshot does not include the empty space.
Here's the affected part of your code and what it looks with the modification
# Arbitrary value to make sure all of our content will be captured
$WPF_window.MinHeight = 3000
$WpfApp1.Add_ContentRendered( {
$MinHeight = ($WPF_Grid1.RowDefinitions.ActualHeight | Measure-Object -Sum) |
Select -ExpandProperty Sum
$WPF_window.MinHeight = $MinHeight
$WPF_window.Height = $MinHeight
$WpfApp1.Close()
})
Related
I need to handle the Ctrl+C function in my datagrid, especially I need to get the cell of a selected value. Here's the XAML code of the datagrid. Please note that I use DataGridTextColumn
<DataGrid x:Name="GridFormule" Grid.Row="0" BorderBrush="#abadb3" CanUserSortColumns="true" Sorting="GridFormule_Sorting" MaxColumnWidth="Infinity" Style="{DynamicResource DataGridStyle}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle}" ItemsSource="{Binding ElementName=ObjectsTree, Path=SelectedItem.Infos}" CellEditEnding="GridFormule_CellEditEnding" Margin="8,5,2,0" ContextMenu="{StaticResource cntextListe}">
<!-- OVERRIDE COPY CONTROL -->
<DataGrid.InputBindings>
<KeyBinding Key="C" Modifiers="Control" Command="Copy" />
</DataGrid.InputBindings>
<DataGrid.CommandBindings>
<CommandBinding Command="Copy" Executed="Comandi_Executed" />
</DataGrid.CommandBindings>
<!-- /OVERRIDE COPY CONTROL -->
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Field.Formula.Formula, Mode=TwoWay}" Header="#_57_Formule" Foreground="Black" Width="*" ClipboardContentBinding="{Binding Field.Formula.Formula, Mode=TwoWay}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Padding" Value="4" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="11.55" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsDirty}" Value="True">
<Setter Property="TextBlock.Background" Value="{StaticResource IsDirtyColor}" />
<Setter Property="Padding" Value="4" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="Background" Value="White"></Setter>
<Setter Property="Padding" Value="2,4,2,3"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
The code behind to handle the copy:
private void Comandi_Executed(object sender, ExecutedRoutedEventArgs e)
{
RoutedUICommand c = (RoutedUICommand)e.Command;
switch (c.Name)
{
// Copia
case "Copy":
DataGrid sel = (sender as DataGrid);
if (sel == null) return;
if (sel.CurrentItem != null) return;
var cc = sel.CurrentColumn;
Binding binding = (Binding)cc;
// Here I get the property Name!
string BoundPropName = binding.Path.Path;
try
{
//Clipboard.SetDataObject(text);
}
catch (Exception ex)
{
Global.LOG.Log(ex.Message);
}
break;
}
}
Basically my BoundPropName returns "Field.Formula.Formula" instead of the value visible in the cell of datagrid. How can I get the cell value?
I am new in WPF and I want to show data from database in WPF DataGrid. Every thing goes well except that in the heading and right corner of the rows, there are some extra borders (which I don't know the name). I don't know how to hide it.
The Code in Xaml file is as below
<Grid Grid.Row="0" VerticalAlignment="Bottom" FlowDirection="RightToLeft" Background="{DynamicResource brushWatermarkBackground}">
<DataGrid Name="DgDeviceList" ItemsSource="{Binding}" AutoGenerateColumns="False" GridLinesVisibility="None" HorizontalAlignment="Stretch" ></DataGrid></Grid>
Then I have such styles in App.xaml:
<Style TargetType="{x:Type DataGrid}">
<Setter Property="Margin" Value="0,0,0,10" />
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Normal"/>
</Style>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="SeparatorBrush" Value="Transparent"></Setter>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0,0,1,0"/>
<Setter Property="FontSize" Value="15"/>
</Style>
The way I fill the Datagrid in cs file is as below:
DgDeviceList.Columns.Clear();
DgDeviceList.Items.Refresh();
DataTable dt = AccessDb.GetDeviceList();
if (dt != null)
{
DgDeviceList.DataContext = dt;
DataGridTextColumn c1 = new DataGridTextColumn();
Binding b = new Binding("DeviceName");
c1.Binding = b;
c1.Header = "Device Name";
DgDeviceList.Columns.Add(c1);
DataGridTextColumn c2 = new DataGridTextColumn();
Binding b2 = new Binding("DeviceSIM1");
c2.Binding = b2;
c2.Header = "SIM Card1";
DgDeviceList.Columns.Add(c2);
DataGridTextColumn c3 = new DataGridTextColumn();
Binding b3 = new Binding("DeviceSIM2");
c3.Binding = b3;
c3.Header = "SIM Card 2";
DgDeviceList.Columns.Add(c3);
DataGridTextColumn c0 = new DataGridTextColumn();
Binding b0 = new Binding("PIN");
c0.Binding = b0;
c0.Header = "Pin Code";
DgDeviceList.Columns.Add(c0);
DataGridTextColumn c4 = new DataGridTextColumn();
Binding b4 = new Binding("MoreInfo");
c4.Binding = b4;
c4.Header = "More Info";
DgDeviceList.Columns.Add(c4);
}
}
catch (Exception ex)
{
LogEvents.InLogFile("FillDataGrid:" + ex.Message);
txtLog.Text = ex.Message + "\n" + txtLog.Text;
}
The output datagrid is as below Image.
It has some square in rows and columns divider that I think it's for showing us the rows and columns are resize-able.
Edit
After adding HeadersVisibility="Column" the right corner was removed same as below picture.
Just add Header Visibility to data grid
<DataGrid HeadersVisibility="Column" Name="DgDeviceList" ItemsSource="{Binding}" AutoGenerateColumns="False" GridLinesVisibility="None" HorizontalAlignment="Stretch" />
I have a assembly where I define several customs controls (based on existing control) with their own style.
For all of them I define the static constructor where I set DefaultStyleKeyProperty and add the style XAML file to Themes/Generic.xaml.
It's working fine for all of them except for my custom ListView.. It's driving me crazy !
Here is a short sample :
public class EmListView : ListView
{
/// <summary>
/// Constructor
/// </summary>
static EmListView()
{
// Set the default style type
DefaultStyleKeyProperty.OverrideMetadata(typeof(EmListView), new FrameworkPropertyMetadata(typeof(EmListView)));
}
}
XAML file declared in Generic.xaml :
<Style TargetType="{x:Type ui:EmListView}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ui:EmListView}">
<Border Name="Border" Style="{DynamicResource EMLV_ListViewBorderStyle}" Margin="50">
<ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
<ItemsPresenter />
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.3" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I don't know why but the only way to get my style applied on my EmListView is to force the style when using it, like :
<ui:EmListView Margin="5" Style="{DynamicResource {x:Type ui:EmListView}}">
I have EmWindow, EmButton, .. All are applying the style automatically except that ListView. Is there something special with ListView ?
Thank you.
EDIT :
I spotted something, it seem that is the declaration of my EmListView that cause the trouble.
Here is a sample declaration inside a window :
<ui:EmListView Margin="5">
<ui:EmListView.View>
<GridView>
<ui:EmSortableGridViewColumn Width="100" DisplayMemberBinding="{Binding artCode, Mode=OneWay}" SortPropertyName="artCode" Header="Code" IsDefaultSortColumn="True" />
<ui:EmSortableGridViewColumn Width="100" DisplayMemberBinding="{Binding artCode, Mode=OneWay}" SortPropertyName="artCode" Header="Code" IsDefaultSortColumn="True" />
<ui:EmSortableGridViewColumn Width="100" DisplayMemberBinding="{Binding artCode, Mode=OneWay}" SortPropertyName="artCode" Header="Code" IsDefaultSortColumn="True" />
<ui:EmSortableGridViewColumn Width="100" DisplayMemberBinding="{Binding artCode, Mode=OneWay}" SortPropertyName="artCode" Header="Code" IsDefaultSortColumn="True" />
</GridView>
</ui:EmListView.View>
</ui:EmListView>
The style doesn't apply and it constantly throw me a warning saying that it cannot define OverridesDefaultStyle property in the style.
If I simply declare it like this :
<ui:EmListView Margin="5" />
I have no warning and my style is set correctly.
I solved my issue, I did a crappy workaround in my EmListView constructor as follow :
public EmListView()
{
Style defaultStyle = (Style)Application.Current.TryFindResource(typeof(EmListView));
if (defaultStyle != null)
{
this.OverridesDefaultStyle = true;
this.Style = defaultStyle;
}
}
If someone can tell me why I have to do this trick only for my custom ListView control I will be happy to know.
I have a DataGrid.
Inside it contains some columns; 2 are related to this question, one is a DataGridTextColumn(x:Name="varTypeColumn") which show Variable type, another is a DataGridTemplateColumn(x:Name="varValueColumn") which could be a TextBox or ComboBox inside.
If varTypeColumn is Bool type, varValueColumn should show a ComboBox which contains 2 items: True, False. If varTypeColumn is Int Type, varValueColumn should show a TextBox which let user input string.
So my question is that, is it possible to do it in xaml? I find some implementation which do it in .cs code, it try to get Row and get the Cell, at last set TextBox/ComboBox instance to Cell's Content property. It can work, but if the DataGrid contains big number items(e.g., more than 5000), it is very very slow to display it.
below is the code part:
private void InitEditors()
{
for (int i = 0; i < _devLinkCollectionView.Count; i++)
{
DataGridRow row = devLinkDataGrid.GetRow(i);
InitEditor(row);
}
}
private void InitEditor(DataGridRow row)
{
DevLink link = row.Item as DevLink;
if (link != null)
{
if (link.HasErrors)
{
ToolTipService.SetShowOnDisabled(row, true);
row.IsEnabled = false;
return;
}
// Create binding first
Binding binding = new Binding("DefaultValue")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,
Source = link
};
DataGridCell cell = devLinkDataGrid.GetCell(row, 4);
switch (link.VariableType)
{
case CarelStandardDataType.Bool:
ComboBox comboBox = new ComboBox();
comboBox.ItemsSource = new[] {string.Empty, "TRUE", "FALSE" };
comboBox.SetBinding(ComboBox.SelectedValueProperty, binding);
cell.Content = comboBox;
break;
default:
TextBox textBox = new TextBox();
textBox.Style = (Style)FindResource("TextBoxInError");
cell.Content = textBox;
binding.ValidationRules.Add(new DevLinkValidationRule(link));
textBox.SetBinding(TextBox.TextProperty, binding);
break;
}
}
}
maybe this is what you want.
<Style x:Key="DataGridCellStyle1" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Height="21.96">
<ComboBox x:Name="cbCondition1">
<ComboBoxItem Content="1"/>
<ComboBoxItem Content="2"/>
</ComboBox>
<TextBox x:Name="tbCondition2" Text="text"/>
</Grid>
<ControlTemplate.Triggers>
<!--In your case, you should use custom Converter, just return type, or maybe you'd better add typeProperty in your model-->
<DataTrigger Binding="{Binding TypeColumn}" Value="Int">
<Setter Value="Visible" TargetName="cbCondition1" Property="Visibility"/>
<Setter Value="Hidden" TargetName="tbCondition2" Property="Visibility"/>
</DataTrigger>
<DataTrigger Binding="{Binding TypeColumn}" Value="Bool">
<Setter Value="Hidden" TargetName="cbCondition1" Property="Visibility"/>
<Setter Value="Visible" TargetName="tbCondition2" Property="Visibility"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<DataGrid Margin="160,106.5,119,139" ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding TypeColumn}"/>
<DataGridTemplateColumn CellStyle="{StaticResource DataGridCellStyle1}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
==================sample data===============================
<SampleData:SampleDataSource xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.SampleDataSource">
<SampleData:SampleDataSource.Collection>
<SampleData:Item TypeColumn="Int" ValueColumn="Row1"/>
<SampleData:Item TypeColumn="Bool" ValueColumn="Row2"/>
</SampleData:SampleDataSource.Collection>
</SampleData:SampleDataSource>
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.