WPF ListBox: Vertical scrollbar and focus - wpf

I have two problems regarding a ListBox/WrapPanel.
First of all, my setup:
In XAML I have a ListBox. Inside of the ListBox is a WrapPanel. Programatically I add ListBoxItems to the WrapPanel.
Problem one
The ListBoxItems [StackPanel with Image and TextBlock] fills left to right and up to down. However, a vertical scrollbar does not appear if there are more ListBoxItems than there is space in the ListBox. The following XAML code makes the vertical scrollbar visible. However, it remains disabled:
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<WrapPanel Name="wp1StandortLinks" ItemHeight="80" ItemWidth="150" Width="755" />
</ListBox>
How to get a functional vertical scrollbar when needed?
Problem two
When clicking into the ListBox but not at a ListBoxItem [for example, in between two of them], the background of the whole ListBox gets highlighted in blue. Initially the background is transparent and it should remain like this even if the ListBox itself was clicked. Catching the Click-Event and setting the background back to transparent seems not to help. The blue highlight-color does disappear, but instead of the ListBox background going back to transparent, it then remains grey.
Can I change the style so that the color does not change at all when clicking at the ListBox? I never really changed styles in WPF, though, so I'd need a precise explaination.

this may work for problem one :
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
for problem 2 :
this may work too (change style for your listbox)
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
</ListBox.Style>
</ListBox>

Related

Operating a slider inside a ListBox by touch

I have an ItemTemplate of a ListBoxItem that contains a Slider. The UI should be operated on a touch screen. However, the slider doesn't work - the thumb cannot be dragged.
The problem can be demonstrated like this:
<StackPanel>
<Slider Height="40" Width="300" Margin="20,10,20,10"/>
<ListBox>
<Slider Height="40" Width="300" Margin="20,10,20,10"/>
</ListBox>
</StackPanel>
The slider inside the ListBox cannot be dragged by touch (although it works for mouse or keyboard input). The other slider outside of the ListBox works just fine (mouse, keyboard, touch).
What needs to be done to "enable" the slider inside the ListBox for touch input?
The accepted answer of Slider \ ScrollViewer in a touch interface not working properly will help here as well.
This is probably due to the ListBox and ItemsControl containing a ScrollViewer internally. Obviously this ScrollViewer is important to drag the ListBox itself (so I cannot get rid of it). However, the ScrollViewer can be tricked by the CustomThumb class of the other post.
Replacing the Thumb in the style is as easy as:
<Window
...
xmlns:l="clr-namespace:WpfApplication1"
...>
<Window.Resources>
...
<Style x:Key="SliderStyle1" TargetType="{x:Type Slider}">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
...
<Track x:Name="PART_Track" Grid.Row="1">
<Track.Thumb>
<l:CustomThumb x:Name="Thumb" .../>
</Track.Thumb>
</Track>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
You can do this by editing the style created by Expression Blend.

ProgressBar in TabItem Width Issue

Hello dear Stack Overflow users.
I'm experiencing an issue when i place a ProgressBar within the TabControl ItemContainerStyle Template.
The issue only happens if the TabItem headers is stretched to the TabControl width. If there is only few headers, then their width is not changed and the progressbar works fine.
When the ProgressBar value changes the width of the TabItem changes with it from it's current width up to the maximum width possible for a item on that current Tab line. Other tabs on the same header line is either moved or shrinked.
So, two questions:
How do I avoid the headers changes their size to fit the ProgressBar?
Why does the ProgressBar want to be as big as possible?
Visual Example!!
ItemContainerStyle
(All Controls uses stretch as width)
<Style x:Key="TabItemStyle"
TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid ...>
<ProgressBar BorderThickness="0"
Background="{x:Null}"
Value="{Binding Progress}">
<ProgressBar.Style .../>
<ProgressBar.Clip .../>
</ProgressBar>
<Border ...>
<ContentPresenter .../>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
TabControl
<TabControl Name="TabController"
TabStripPlacement="Bottom"
Padding="0"
helpers:TabItemGeneratorBehavior.ItemsSource="{Binding TabPageCollection, Mode=TwoWay}"
helpers:TabItemGeneratorBehavior.SelectedItem="{Binding SelectedTabPage, Mode=TwoWay}"
ItemContainerStyle="{StaticResource TabItemStyle}"/>
Problem fixed! Wraped ProgressBar in a Canvas and set the ProgressBar size to bind to Canvas Actual Width/Height.

Changing the border colors of a WPF combobox

So I am trying to change the style of my combobox in Expression blend.
What I did was create a combobox, and went RightClick > Edit Template > Edit a Copy
And I can change the colors of the combobox, except there is a white border in between the background of the combobox, and the border of the combobox. Here is a screen so you can see:
As you can see, there is a while border between the blue and red. As far as I can tell, the code to change the color of combobox is the following:
<ToggleButton Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource
ComboBoxReadonlyToggleButton}" BorderBrush="Red" Background="Blue"/>
But no matter what, there is always a white border. How do i get rid of it?
I know this is an old question, and it's specific to blend but when googling for this problem, this is one of the first things I found.
A really simple example of a way to fix this, that is a little less complex than the first answer mentioned is to set "Style" Properties. (Not sure this applies to blend since I don't use blend, but for simple wpf in visual studio, this works)
For example, this code below creates a window just like the one mentioned in the question, but with the white lines (in the drop down items) editable.
<ComboBox Background="Blue" BorderBrush="Red">
<ComboBox.ItemContainerStyle>
<!-- Without this style section, there are white lines around the borders. Even if you set these properties in the comboBoxItem areas -->
<Style TargetType="ComboBoxItem">
<Setter Property="Background" Value="Green"/>
<Setter Property="BorderBrush" Value="Purple"></Setter>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBoxItem MouseMove="schedule" Name="cbi1">schedule</ComboBoxItem>
</ComboBox>
The problem is when you Edit a Copy, you're editing a copy with Microsoft's built-in chrome components. In order to change that outside border, you'll need to replace those bits with normal WPF controls so that you can change the values. For a combo box, you would want use the code here: http://msdn.microsoft.com/en-us/library/ms752094
e: This is the part you want to edit
<Border x:Name="Border"
Grid.ColumnSpan="2"
CornerRadius="2"
BorderThickness="1">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0,1"
StartPoint="0,0">
<GradientStop Color="{DynamicResource BorderLightColor}"
Offset="0" />
<GradientStop Color="{DynamicResource BorderDarkColor}"
Offset="1" />
</LinearGradientBrush>
</Border.BorderBrush>

Change a ListView image item's source when selected

Below is a sample of what I am after:
I have decided to implement that using a ListView (tried a custom control based on Selector but I could not managed to output anything satisfying).
My list displays fine but I am struggling to find how to change the image source when the item gets selected. Here is my code:
<UserControl.Resources>
<DataTemplate x:Key="PagingIndicatorTemplate">
<Image Width="20" Height="20">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="/MyProject;component/Resources/Images/ic_paging_button_normal.png" />
<!-- I guess that's where I need to put my stuff to change the image ? ... -->
</Style>
</Image.Style>
</Image>
</DataTemplate>
</UserControl.Resources>
<ListView Name="PagingIndicator"
Height="30"
ItemTemplate="{DynamicResource PagingIndicatorTemplate}"
ItemsSource="{Binding Path=News}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
</ListView>
There is one thing wrong... you are using ListView but targetting style to ListBoxItem. It should be ListViewItem.
In Image's style use a DataTrigger where check the binding on RelativeSource ListViewItem and Path=IsSelected (if its True) and change the Source of the image.
I have decided to solve the problem like that:
Make two different item templates (each with their own images, styles for mouse over, mouse pressed, ...)
Change the Template property of the ListViewItem with a trigger on its IsSelected property.

Setting background colour of Silverlight Listbox

How do I set the background colour of a listbox? I have a listbox with textblocks in it and there does not appear to be anyway that actually works to set the background colour of these controls, why is this seemingly so hard?
In the interests of full disclosure I asked a similar question earlier
You can do this using the ListBox.ItemContainerStyle property. Very nice explanation of this can be found here. Based on that example, we can set the ItemContainterStyle to have a transparent background color and then wrap the ListBox in a Border (the ListBox doesn't display its background color).
<Border Background="Green">
<ListBox Background="Red">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
</Style>
</ListBox.ItemContainerStyle>
<TextBlock Text="Hello" />
<TextBlock Text="Goodbye" />
</ListBox>
</Border>
If you just want to set the actual items you can set the Background to an actual color and then skip the border.

Resources