GridViewColumn content and VerticalAlignment - c#

i want to display some information in a listview using the GridView.
i have several GridViewColumns and everything works fine.
However, want the GridViewColumns content to have a VerticalAlignment (Top in this case)
but the gridvewcolumn intself doesnt offer a VerticalContentAlignment dependency property.
when using DisplayMemberBinding there is also no possibility to supply
VerticalAlignment information.
When using a custom DataTemplate as Celltemplate, i can add a VerticalAlignment="top" dp to e.g. some textblock. however this does not work.
is there any "nifty-grifty special magic trick" to fullfill this tasK?
(

You can apply this style to your ListView:
<Style TargetType="{x:Type ListView}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListViewItem">
<Setter Property="VerticalContentAlignment" Value="Top"/>
</Style>
</Setter.Value>
</Setter>
</Style>

Related

Lost previous combobox item style after applying style for cursor on hover

I want all clickable elements in the app to have hand cursor on hover. My problem is in comboboxes: all of them apply default (or what?) style when I set my custom style below (but hand cursor works!):
<Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
<StackPanel Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal">
<TextBlock Style="{StaticResource OptionTextBlockStyle}" />
<ComboBox x:Name="SortTypesComboBox" FontSize="14px" DisplayMemberPath="Description" ItemContainerStyle="{StaticResource ComboBoxItemStyle}">
</ComboBox>
</StackPanel>
The first image is what I want but with hand cursor on clickable items, the second one is what I actually have applying my comboboxitemstyle.
I don't know why I lost my previous style using this line ItemContainerStyle="{StaticResource ComboBoxItemStyle}. How can I save it and make right cursor on hover at the same time?
---UPG----
I've just added BasedOn="{StaticResource {x:Type ComboBoxItem}}" line and this didn't help me much. Now I have my items with wrong style and green lightning:
The way that you define your style overrides the default style for ComboBox. Instead, you should base your style on the default style using the BasedOn property.
<Style x:Key="ComboBoxItemStyle"
TargetType="ComboBoxItem"
BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
The reason for referencing the default style with {x:Type ComboBoxItem} is that it is an implicit style, which gets applied automatically to each control of that type in scope unless a different style is assigned explicitly and the x:Key of an implicit style is their TargetType.

Why ContentPresenter's render time is bigger than its children's sum?

See the below picture:
I just used Performance Profiler in VS2017 and i found that the listBoxItem's ContentPresenter render time is so bigger than its children's.
I have to reduce total render time (which is 1.1s roughly).
Sum of ListBoxItems render time is also 1.1s.
I don't know why time difference is made.
The Listbox's visual tree is like this:
ListBox(3057)
Border(3056)
ScollViewer(3055)
Grid(3054)
ScrollContentPresenter(3031)
ItemsPresenter(3029)
VirtualizingStackPanel(3028)
ListBoxItem(120)
ListBoxItem(84) ...
I can reduce visual tree's Control.
The way that i do is "redefine some Control's ControlTemplate"
Before redefine ControlTemplate, my ListBoxItem have Bd(Border), ContentPresenter, Grid,, etc. like below
After redefine ControlTemplate like below xaml
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter>
<ContentPresenter.Style>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="VirtualizingPanel.IsVirtualizing" Value="True"/>
<Setter Property="VirtualizingPanel.VirtualizationMode" Value="Recycling"/>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
Then i can remove "Bd"(Border) like below.
ListBoxItem
ContentPresenter
Grid ...
To reduce render time, I will find useless Controls and remove.

Wpf Custom ListBox with Rounded Border

I have extended ListBox control and wanted to give it rounded corner so I have applied styles as below and it works fine:
<control:MultiSelectionListBox SelectionMode="Extended" ItemsSource="{Binding Offerables,Mode=TwoWay}"
SelectedItemsList="{Binding SelectedOfferables, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name"
Grid.Row="6" Grid.Column="0" MaxHeight="150">
<control:MultiSelectionListBox.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"></Setter>
</Style>
</control:MultiSelectionListBox.Resources>
</control:MultiSelectionListBox>
but I have too many Listboxes and I don't wanted to add style in each and every control individually.
I wanted to define this style in app.xaml and wanted to reuse that design by name like Style = "{StaticResource RoundedListBoxStyle}".
I have tried like below in app.xaml but with no luck
<Style TargetType="ListBox" x:Key="RoundedListBoxStyle">
<Setter Property="Border" Value="10"></Setter>
</Style>
The member Border is not recognized or is not accessible.
Please guide me what I am doing wrong here.
Finally I managed to do that as Bradley Uffner mentioned in the comment Border is not the property of the ListBox and we cannot use it in style like that. I used Style.Resource and mentioned Style for Border elements as below:
<Style TargetType="ListBox" x:Key="RoundedListBoxStyle">
<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"></Setter>
</Style>
</Style.Resources>
</Style>
and it worked. :)

Set property in ContentPresenter child control

I want to set TextTrimming on TextBlock.
<Style TargetType="{x:Type dg.CellValuePresenter}">
<Setter Property="ContentTemplate" Value="{StaticResource Tmp}" />
</Style>
My template:
<DataTemplate x:Key="Tmp">
<ContentPresenter Content="{Binding}" >
<ContentPresenter.Resource>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
</Style>
</ContentPresenter.Resource>
</ContentPresenter>
</DataTemplate>
Content Presenter is XamTextEditor from Infragistic:
<Style TargetType="{x:Type igEditors:XamTextEditor}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igEditors:XamTextEditor}">
<TextBlock/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Setter from DataTemplate doesn't work. Do you know how to set this property?
How can I set TextTrimming property in Textblock of XamTextEditor cudtom style? I cannot do this in this style because it is used in other datatemplates where texttrimming must be turn off. So I tried to set it in DataTemplate but it not work (i see in snoop that it is set to none)
Unfortunately I cannot show more code because I don't have access to internet on dev and write down more code is time consuming :/
I'm not sure if I get your problem completely.
But If you are using explicit column definition. Then instead of CellvaluePresenter try create a EditorStyle For any column type(I've done it for string type column so the editor would be XamTextEditor)
<!--(xmlns:igWindows="http://infragistics.com/Windows")-->
<Style TargetType="{x:Type igEditors:XamTextEditor}" x:Key="DefaultXamDateTimeEditor">
<Style.Resources >
<Style TargetType="{x:Type igWindows:SimpleTextBlock}" >
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
</Style>
</Style.Resources>
</Style>
And use this style as columns EditorStyle. It should work in XamDatagrid as this is working for me. Please post specific situation If I didn't get the question right.

WPF ControlTemplate - default style

I want to use the default style of buttons and just want to add a Grid and Stackpanel with 2-3 Bindings. When i add the ControlTemplate in my Style all Trigger-, Border-, ...settings are overwritten. How can i just add my Grid/Stackpanel/Bindings while using the rest of the default settings?
<Style x:Key="listbutton">
<Setter Property="Button.Height" Value="40"/>
<Setter Property="Button.Margin" Value="0,3"/>
<Setter Property="Button.Template">
<Setter.Value>
<ControlTemplate>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The purpose of a ControlTemplate is to define how a Control must be displayed. You can't have a ControlTemplate based on another ControlTemplate (it wouldn't make sense). What you can do is take the default ControlTemplate and modify it at your will.
The default ControlTemplate for Button can be found here

Categories