How to always show specific item from combobox while scrolling - c#

I have a combo box which is filled with checkbox items.
When I scroll down I would like to have the first item of the combo box to always be visible.
It should kind of look like the the navigation bar on this or other websites.
Thanks in advance.

You can modify control template, see this post how to extract it.
Below is ugly (but working) solution to see current selected item on top of popup, which you can modify to your liking:
For this you have to extract combobox control template as mentioned before, then search for
<Popup x:Name="PART_Popup"
and modify its
<Border x:Name="DropDownBorder"
by adding inside a Grid like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ContentControl Content="{TemplateBinding SelectionBoxItem}" />
Now just set grid row for ealier existing scroll viewer:
<ScrollViewer x:Name="DropDownScrollViewer" Grid.Row="1">
don't forget to close Grid attribute after it:
</Grid>

Related

WPF Grid and content is pushed out of window after an animation

I have animated the height of the content of one of my Grid rows so that the row extends downwards when a button is pushed (flyout style from 0 to 80px). This causes the next Grid row to get pushed the same amount downwards and hence out of the window. I am using Prism WPF and the content in the rows are imported via <ContentControl>'s.
I've tried to use Snoop to figure it out, and it looks like the height of the third Grid row does not update when the animation of the first row is fired. The third row contains a <DataGrid> wrapped in a <Grid>.
The outer code looks something like this:
<Grid>
<Grid.RowDefinitions>
<!-- For flyoutButtons -->
<RowDefinition Height="{StaticResource FlyoutButtonRowHeight}"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentControl prism:RegionManager.RegionName="{x:Static inf:Regions.FlyoutRegion}" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"/>
<ContentControl prism:RegionManager.RegionName="{x:Static inf:Regions.DataGridRegion}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"/>
</Grid>
The animation is defined as a <Grid.Style> element within the first <ContentControl>, and it is triggered via a <DataTrigger>.
So my question is how can I make the height of the last Grid row upate when/after the animation is triggered?

ScrollBar is not showing in WPF ListView which is inside nested User and Content Controls

Please help me in this issue of scroll bar visibility in WPF listview.
I have a listview inside a Content Control.
This Content Control is inside a User Control.
This User Control is inside a TabItem.
The listview has around 12 columns to display, which exceeds the window width.
I tried so many ways to show the horizontal scroll bar in the listview.
Below shown is the XAML of the Outer UserControl [width is not set for this outer usrCrtl]
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" /> // Here I have a custom content control
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<MyCustomContentControl Grid.Row=1 VerticalAlignment="Stretch"......>
<TabControl>
<TabItem Header="One" Name="Tab1">
<my:usrAControl /> // I have listview inside this userctrl
</TabItem>
</TabControl>
<TabControl Header="Two" Name="Tab2" />
</MyCustomContentControl>
</Grid>
Now below is the usrAControl XAML details
<UserControl x:Class="MyProject.MyModule.usrAControl"
MinWidth="640">
// Again inside another custom user control as its child.
<usrBControl>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" /> // here another headers
<RowDefinition Height="*" /> // here my listview placed
</Grid.RowDefinitions>
<ListView ScrollViewer.HorizontalScrollBarVisibility="Auto" Grid.Row="1"
Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Width}">
// Around 12 columns which exceeds window width
</ListView>
</Grid>
</usrBControl>
</usrAControl>
I tried with lot of combination.
I initially put a scrollviewer control inside the tabitem and placed usrAControl inside it.
But it did not work.
But I want the listview should show its both scroll bars. Is any way to do it.?
Without seeing more code, my guess would be that the MinSize="640" is your problem: the ListView gets enough space from its container so it doesn't show the scroll, but the container gets clipped.
And you should get rid of the ListView Width binding, it's completely redundant.

Retain focus in TextBox if surrounding elements are clicked

I have a small user control that contains some Xaml markup that includes a TextBox. When the control is selected, I automatically set focus on the TextBox. However, when the user clicks on any other element (such as a border) the focus is removed from the TextBox.
For example,
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Fill="Blue" IsHitTestVisible="False" />
<TextBox Grid.Row="1" />
<TextBlock Grid.Row="2" Text="Something" IsHitTestVisible="False" />
</Grid>
Clicking either the Rectangle or the TextBlock causes the TextBox to lose focus.
What's the best way to retain focus on the TextBlock? I feel I'm missing something simple.
The click is probably going through the grid and giving focus to whatever is under it.
Try setting Background="Transparent" on the <Grid>
The only solution I found was to rebuild the Xaml surrounding the control in question so that custom control incorporated everything that I wanted to not affect the the focus of the text box. And then put a handler on the mouse event to ensure that the TextBox retained focus.
I was not happy with the solution but couldn't think of anything else.
( I did try to handle the LostFocus event and determine if the mouse was still the user control, but gave up with that route when I had to continually keep track of the mouse location ).

Wrap panel where items in a row share height

i want to add squares to a panel and have them wrapped like the wrap panel.
I then want to make each square horizontally resizable individually, but when it is resized vertically, i need it to affect all the items in it's row.
Basically, I'd want all items in a row to always share the same height, but give the user a method of choosing this height (of course, each row can have its own height, and when squares wrapped to a new row they will need to inherit the new height).
Btw, those "squares" are just user controls or data template applied to a listbox items source. Can i use the same binding on a wrap panel, ad maybe i need to choose a different solution?
Thank you
you can try and put each "rectangle" in a Grid with one row and one column, and then use SharedSizeGroup on the RowDefinition. Be sure to also put Grid.IsSharedSizeScope="True" on the container:
<WrapPanel Grid.IsSharedSizeScope="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="Group1" />
</Grid.RowDefinitions>
<Button Height="40" Content="Hello" />
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="Group1" />
</Grid.RowDefinitions>
<Button Content="Hello2" />
</Grid>
</WrapPanel>

Keep TabItems in a TabControl from repositioning?

In WPF, Is there a simple way to stop TabItems in a TabControl from being repositioned when the selected TabItem changes? So that clicking on a TabItem would simply display its contents, but not reposition the TabItems as it usually does (by moving the selected TabItem to the bottom row of tabs if it wasn't there already).
Edit: To clarify, I do want the tabs to be displayed in multiple rows, I just don't want the tab headers to be repositioned when a TabItem from a row other than the bottom row is selected. I'd like the collection of headers to remain completely static, but for the contents of that TabItem to still be displayed when its header is clicked.
Thanks!
I know its late but I had to figure this out today so here goes.
Basically you need to create your own control template for the tab control and use a different panel to contain your tab items.
Here is a simplified example using a WrapPanel.
<Style TargetType="TabControl" x:Key="MyTabControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabControl">
<Grid SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<WrapPanel x:Name="HeaderPanel" HorizontalAlignment="Center" VerticalAlignment="Top" IsItemsHost="true"
Grid.Row="0" KeyboardNavigation.TabIndex="1" />
<ContentPresenter Grid.Row="1" x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="10" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
You then use this as follows
<TabControl Style="{StaticResource MyTabControl}" ....
You can download the control template samples here
If you have Blend you can Edit a copy of the template for your tab control to remove this behavior I believe.

Categories

Resources