This
<ListBox x:Name="gLBxM">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Height" Value="21" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
sets the ListBox item height for a specific ListBox. What do add to
<Window.Resources>
so it applies to all ListBox'es?
The following implicit Style will be applied to all ListBoxItems in the window unless you set the ItemContainerStyle property to some other Style:
<Window.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Height" Value="100" />
</Style>
</Window.Resources>
...
<ListBox x:Name="gLBxM" />
Related
I've a TextBlock inside a Grid which is inside a PopupBox provided by MaterialDesignInXaml, a sample structure:
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
<materialDesign:PopupBox>
<Grid>
<TextBlock Text="Foo" />
</Grid>
</materialDesign:PopupBox>
I'm trying to apply the TextBlock foreground as a global resource in my App.xaml:
<Style TargetType="materialDesign:PopupBox">
<Setter Property="TextBlock.Foreground" Value="red" />
</Style>
but not seems to be working, any help?
You will need to define an implicit Style for the Grid. Try this:
<Style TargetType="materialDesign:PopupBox">
<Style.Resources>
<Style TargetType="Grid">
<Setter Property="TextElement.Foreground" Value="Red" />
</Style>
</Style.Resources>
</Style>
My XAML code is: <ListBox Name="Seq"><TextBlock Name="txt1" Text="Hello"/></ListBox>
Code-behind would be:Seq.Style = ...; or txt1.Style=...;
I want to make some items green, bold and italic. What should I do? Thanks.
You can add style in App.xaml as per your requirement, for example :
<Style TargetType="TextBlock" x:Key="mystyle">
<Setter Property="Foreground" Value="Green"></Setter>
<Setter Property="FontStyle" Value="Italic"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
</Style>
and then you can apply that style to text block of list box as below:
this.txt1.Style = (Style)Application.Current.FindResource("mystyle");
What you really should do is to bind the ItemsSource property of the ListBox to an IEnumerable<T> and then add properties to the type T that you can bind to in an ItemTemplate, e.g.:
<ListBox Name="Seq">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="txt1" Text="{Binding TextProperty}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding SomeOtherProperty}" Value="SomeValue">
<Setter TargetName="txt1" Property="Foreground" Value="Green" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
As a quick fix you could define a TextBlock style in your XAML markup:
<ListBox Name="Seq">
<ListBox.Resources>
<Style x:Key="style" TargetType="TextBlock">
<Setter Property="Foreground" Value="Green" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontStyle" Value="Italic" />
</Style>
</ListBox.Resources>
<TextBlock Name="txt1" Text="Hello"/>
</ListBox>
...and set the Style property of the TextBlock, either programmatically:
txt1.Style = Seq.Resources["style"] as Style;
...or directly in the XAML markup:
<TextBlock Name="txt1" Text="Hello" Style="{StaticResource style}"/>
I need to define a global style for all TextBlock and TextBox that exist within ItemsControl elements.
I need all the TextBlock elements to have a Width of 100 and Left aligned, and the TextBox elements to have a width of 50 and to be Right aligned.
How can I achieve this?
I don't understand your "that exist within ItemsControl elements" but if you are talking about your ItemsTemplate it should work like this
<Style x:Key="myTextBoxStyle">
<Setter Property="Width" Value="50"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
<Style x:Key="myTextBlockStyle">
<Setter Property="Width" Value="100"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
<ItemsControl>
<ItemsControl.ItemsTemplate>
<DataTemplate>
<Grid>
<TextBlock Style="{StaticResource myTextBlockStyle}"/>
<TextBox Style="{StaticResource myTextBoxStyle}"/>
<Grid>
<DataTemplate>
</ItemsControl.ItemsTemplate>
</ItemsControl>
This will display all items in your ItemsControl with a textbox which uses your myTextBoxStyle and your textblock with the myTextBlockStyle.
you can declare the style inside the ItemsControl.Resources as well
<ItemsControl ItemsSource="{Binding Persons}">
<ItemsControl.Resources>
<Style x:Key="TxtBlk1" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="red"/>
<Setter Property="FontSize" Value="56"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Style="{StaticResource TxtBlk1}"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
In WPF Datagrid, if row style is used, it does not detect the grid rows. If I remove the row style, it detects the cell and rows properly.
<DataGrid Grid.Row="1" x:Name="c_dataGrid"
ItemsSource="{Binding DataSource}"
AutoGenerateColumns="True"
RowStyle="{StaticResource rowStyle}">
<DataGrid.ItemContainerStyle>
<Style>
<Setter Property="AutomationProperties.Name" Value="{Binding Id}"/>
</Style>
</DataGrid.ItemContainerStyle>
</DataGrid>
<Style x:Key="rowStyle" TargetType="{x:Type DataGridRow}"
<Setter Property="Foreground" Value="Green"/>
</Style>
A shot in the dark here...
Does it make any difference if you apply the BasedOn property?
<Style
x:Key="rowStyle"
TargetType="{x:Type DataGridRow}"
BasedOn="{StaticResource {x:Type DataGridRow}}">
<Setter Property="Foreground" Value="Green"/>
</Style>
I made a custom styled list in WPF:
<DataTemplate x:Key="SelectedTemplate">
<StackPanel Background="#FF4E4E4E" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=Title}" Foreground="#FFD80000" />
</StackPanel>
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate"
Value="{StaticResource ItemTemplate}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate"
Value="{StaticResource SelectedTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
...
<ListBox
Name="lbSongs"
DockPanel.Dock="Left"
ItemsSource="{Binding Path=SongDirectory}"
SelectedItem="{Binding Path=Song, Mode=TwoWay}"
Visibility="{Binding Path=ListVisibility}"
ItemContainerStyle="{StaticResource ContainerStyle}"
HorizontalContentAlignment="Stretch"
Width="180px" Background="#FF333333" />
I tried to make a custom style for the selected item. To make the selection bar stretch to the width of the ListBox, I set the ItemContainerStyle's HorizontalContentAlignment property to "Stretch". The problem is that it does not stretch fully, a tiny bar on the left still remains and the original (blue) selection bar is still visible there. See the screenshot:
How can I make it to stretch to full size? Or how can I style the original selection bar?
Thanks in advance.
Just set the Padding for the ListBoxItem to zero. That blue bar should disappear.
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="Padding" Value="0"/>
...
</Style>
Another option is just to re-write the ControlTemplate for the ListBoxItem so that you'll have better control on how it looks like. But yeah, this may not be necessary in your case.