I am having some trouble setting the style to a groupbox control element.
I have some XAML code that has styling defined at the top of the code. i am looking for a way to add the styling already defined to an element that i am creating dynamically.
Here is my styling section:
<Window.Resources>
<Style x:Key="GroupBoxStyle1" TargetType="{x:Type GroupBox}">
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupBox}">
<Grid SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="6"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="6"/>
</Grid.RowDefinitions>
<Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="3" Grid.Column="0" CornerRadius="8" Grid.Row="1" Grid.RowSpan="3"/>
<ContentPresenter Grid.Column="1" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Grid.Row="1" Grid.RowSpan="2" Grid.Column="1"
HorizontalAlignment="Center" VerticalAlignment="Center"
RenderTransformOrigin="0.5,0.5">
<ContentPresenter.RenderTransform>
<RotateTransform Angle="45"/>
</ContentPresenter.RenderTransform>
</ContentPresenter>
<Border BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="3" CornerRadius="4" Grid.Row="1" Grid.RowSpan="3">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
<Border BorderBrush="Red" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/>
</Border>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
This is what i have for my element creation:
$NewBox = New-Object System.Windows.Controls.GroupBox
$NewBox.Content = "L " + $counter
$NewBox.Header = "t"
$NewBox.Margin = "0"
$NewBox.Style = "GroupBoxStyle1"
$NewBox.Name = "FirstBox"
[System.Windows.Controls.Grid]::SetRow($NewBox,1)
[System.Windows.Controls.Grid]::SetColumn($NewBox,1)
I used the style property since that was what i could find using the 'Get-Member' command. i have a feeling i am not using the right syntax to call the target type. Because while i can get the groupbox to appear, i continually get the following error:
Exception setting 'Style': "Cannot convert the 'GroupBoxStyle1 value of type "System.String" to type "System.Windows.Style"."
您应该将Style
属性设置为Style
:
$NewBox.Style = $window.TryFindResource("GroupBoxStyle1")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.