I know there is tons of these questions out there. But most of these solutions don't work in my situation. However after spending a day on it, I finally found a solution which works in my case. I just need somebody with deeper knowledge of XAML to explain to me and possibly the others what exactly happens here.
<Style x:Key="ListBoxItemStyleNoHighlighting" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter x:Name="contentPresenter"
Margin="{TemplateBinding Margin}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If somebody out there needs the xaml of how to use it, here it is
<ListBox Background="Transparent"
BorderThickness="0"
ItemTemplate="{StaticResource MyDataTemplate}"
ItemsSource="{Binding MenuSubItems}"
SelectedItem="{Binding MySelectedItem}"
ItemContainerStyle="{StaticResource ListBoxItemStyleNoHighlighting}">
So what is happening in that style? I haven’t seen anything like this before. After some research I found out that TemplateBinding is just setting up its values to the parent. Then why is that ContentPresenter even needed if it does not add anything to the style. It's worth to mention that without ContentPresenter, it simply does not work.
Kind Regards
Daniel
The concept of ContentPresenter is quite simple – it is a placeholder for any XAML content and it can be used to insert content at runtime.
Here is more.
Related
Good evening. I have been chasing an issue with DocumentViewer and don't know how to address it.
When you display a document in a DocumentViewer control, it displays a border and a shadow around the document and I do not know how to get rid of it.
I have written a control template, which I would have expected to have addressed the issue and it does not. Here it is:
<Style x:Key="MyDocStyle" TargetType="DocumentViewer">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DocumentViewer">
<Border BorderThickness="5" >
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" x:Name="PART_ContentHost"
Background="Transparent"
BorderBrush="Transparent" BorderThickness="0" Padding="0" >
</ScrollViewer>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
What else am I missing? Thank you in advance.
Turns out, you don't need to alter the ControlTemplate at all. May I point out DocumentViewer.ShowPageBorders:
Indicates whether drop-shadow page borders are displayed
Usage:
<DocumentViewer ShowPageBorders="False"/>
Thank you very much. I'm not sure how I missed that. Nonetheless, thank you very much for your help!
I have a section header which contains two words to be displayed in XAML with a little bit of style with fontsize and fontfamily. I can implement this with TextBlock or ContentControl (by setting the content property to the header text).
The TextBlock is not derived from Control, so is the intent of TextBlock to perform better in XAML for text compared to a contentcontrol?
Your TextBlock is just that, a TextBlock as framework element when you invoke it is just an object. So when you write <TextBlock Text="Blah Blah Blah"/> that's literally all it is.
When you're using a ContentControl you're actually invoking a templated control that will have multiple elements in its tree that are added for every instance. So example you're using;
<Style TargetType="ContentControl">
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Cursor="{TemplateBinding Cursor}"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
So a short version to your answer would be Yes, TextBlock will perform better and is suggested to be used over templated controls like ContentControl or Label etc. wherever possible.
Hope this helps, cheers.
To directly answer your question -- if your intent is to only always render text, yes, use only a TextBlock. As others have pointed out using a ContentControl/ContentPresenter and setting to a string will wrap it anyway, and you have increased your element count +1 unnecessarily.
i want to draw/add an image as a part of text in textbox in windows phone 7. I m not using Expression blend.
So where i can find the drawing objects as well as paint events in silverlight?
You can apply a background image to a lot of Silverlight elements with the following:
<TextBox x:Name="SearchBox" Text="Search" Height="70" Width="390">
<TextBox.Background>
<ImageBrush ImageSource="Images/MagnifyingGlass.png" Stretch="UniformToFill" />
</TextBox.Background>
</TextBox>
There is no way to add an image as part of a TextBox. Although I'm not entirely sure what you want to achieve.
Do you really mean TextBox? If so, the only option will be to restyle it so it have the image included as well.
Do you mean TextBlock? If so, and you're trying to include an image part way through a piece of text, you can wrap the image and the text either side of it in a WrapPanel.
You might want to override the template in order to define your own template. You can do this in the style:
<Style x:Key="textboxImage" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="ApplicationIcon.png" />
</Grid.Background>
<ContentControl x:Name="ContentElement" Foreground="{TemplateBinding Foreground}" Margin="{TemplateBinding Margin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You just need to set the style of your textbox to StaticResources textboxImage.
I just tested and it works fine.
Hello
First sorry for my english.
I have started recently my first project on wpf. I´m styling a custom DataGrid who have been defined programatically (the xaml code doesn´t exists).
I have styled all that i need in my datagrid control except a checkbox that i wrapped inside.
The problem is that in other place of my application i defined a checkbox style how are applying correctly but i can´t apply inside my datagrid.
Actually my datagrid doesn´t throw syntax errors but when the datagrid runs the checkbox styles doesn´t apply.
The style code look like this (its defined in a stylesheet)
... <Setter Property="DataGridCheckBoxColumn.ElementStyle">
<Setter.Value>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Grid Width="13" Height="13">
<Border x:Name="Border" Background="Pink" BorderBrush="Black" BorderThickness="1,1,1,1" CornerRadius="2,2,2,2"/>
<Path x:Name="CheckMark" Stroke="Green" StrokeThickness="2" SnapsToDevicePixels="False" Data="M1.5000001,1.5833334 L9.7920001,9.6666667 M1.5420001,9.6666667 L9.7083333,1.5000001" Margin="1" ClipToBounds="False" StrokeEndLineCap="Round" StrokeStartLineCap="Round"/>
</Grid>
</BulletDecorator.Bullet>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</BulletDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>...
Its exactly the same that it`s applying in the apliccation.
I´ve read a lot about it but i can´t to apply it, i tried, also, setting the setter property to "DatagridBoundColum.ElementStyle" and also to "CellStyle" but it doesn´t work.
Any suggest??
Thank a lot.
Do it like you would do in xaml:
<UserControl.Resources>
<DataTemplate x:Key="CheckBoxTemplate">
<CheckBox Style="{StaticResource AnyResourceKeyInApplciation}"/>
</DataTemplate>
</UserControl.Resources>
<DataGrid x:Name="dataGrid" />
this.dataGrid.Columns.Add(new DataGridTemplateColumn
{
CellTemplate = this.Resources["CheckBoxTemplate"] as DataTemplate
}
);
Thanks for your Reply vorrtex.
I didn´t apply it exactly but it helped me to find the solution, however i would have liked not to modify the VB code and only to modify it the xaml style tag.
I find an object how simplify this task. The syntax it´s the following:
column2.ElementStyle = Application.Current.FindResource("CheckBoxStyle")
It´s applying style ok inside the datagrid. But actually it´s placing at left border of the cell. I´ll try to find why.
Thanks again.
You can try this
<Controls:DataGridCheckBoxColumn Header="Homme" Binding="{Binding Homme}">
<Controls:DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
<Setter Property="Margin" Value="4,0,0,0"/>
</Style>
</Controls:DataGridCheckBoxColumn.ElementStyle>
</Controls:DataGridCheckBoxColumn>
I'm customising the appearance of grouping in a ListBox. In ListBox.Resources, I have declared something like (formatting removed):
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel Orientation="Vertical">
<!-- Group label -->
<ContentPresenter />
<!-- Items in group -->
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The actual group label is not very readable and I'd like to use a value converter to make it more presentable. However I cannot find a way to obtain this text and convert it.
I figure that a Binding would let me use a converter.
I've tried replacing the ContentPresenter above with the likes of...
<TextBlock Text="{TemplateBinding Content}"/>
<TextBlock Text="{Binding}"/>
...and numerous other things, but to no avail. Any suggestions?
Well isn't that just typical. I found the answer shortly after posting...
<TextBlock Text="{Binding Path=Content.Name,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GroupItem},
Converter={StaticResource MyConverter}}"/>
Sometimes just the process of actually asking the question draws the answer out of thin air. In this case looking at the source code of GroupItem in .NET Reflector did the trick.
Hope someone else finds this edge case useful. Still, it would be a lot nicer if GroupItem exposed a property for this directly.
I'll still award a correct answer to anyone who knows a nicer way of doing this.