Change background color between ListBoxItems - wpf

This is part of my window: MyListBox.
I would like the space between the items to be green as well (if the upper and lower item is green) but I don't know how to change it.
This is my XAML code (tried to put the background colour in the StackPanel but nothing changed):
<ListBox Background="LightCyan" Name="MondayListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="{Binding backgroundColor}">
<TextBlock Text="{Binding Time, StringFormat={}{0:hh}:{0:mm}}" Background="{Binding BackgroundColor}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Any suggestion would be very appreciated.
EDIT:
I setted the backgorundColor to be a Brushand initialized it with Brushes.LightCyan but I still have the white spaces between the items.

Where is backgroundColor coming from? Are you sure that it's a Brush object, as required for Control.Background, rather than just a Color value?
You can use a ValueConverter class, such as in this answer to convert from one to the other.

Related

There has to be a simple way to wordwrap the items in a combobox dropdown

I've spent about a day searching for this with no luck. I just expected this to work but while the combobox shows the text content wordwrapped when the dropdown list is not showing, the individual items in the dropdownlist are not wordwrapped. I can still change the style for all the line items, but the text does not wrap. I've played with all sorts of variations of ItemsPanelTemplate to no avail.
My existing item template is trivial.
<ComboBox.ItemTemplate>
<DataTemplate DataType="entities:Client">
<StackPanel>
<TextBlock Background="LightCoral" Text="{Binding Description}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
Help! Thanks
From the comments on the question:
StackPanel allows infinite layout space. You need to set a MaxWidth or other similar constraint on your TextBlock. If you want the width of your ComboBox, you can use the following:
MaxWidth="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=ComboBox},
Path=ActualWidth}"

how to draw wpf ui with many many controls?

My application UI use Tab control.
One of the tab has 20 groupboxes (in scrollviwer), and each groupbox contents 300 textbox with a name (label) above the textbox (not one on one match).
When the app running (not yet), each textbox will display a byte value from the buffer.
I am manually drawing this groupbox, it is too much work. I am trying use itemcontrol to draw the textboxes, but don't know how to make a new line since there are name-value pair.
Any solution will be appreciated.
You would use an ItemControl with a DataTemplate, where the DataTemplate represents one key-value pair.
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Hi I am a label" />
<TextBox />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Silverlight RichTextBox/ListBox/ScrollViewer strange behaviour

I have a user control with the following XAML:
<ScrollViewer>
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<RichTextBox>
<Paragraph>
<Run Text="{Binding}"/>
</Paragraph>
</RichTextBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
And code behind:
public partial class MainPage {
public MainPage() {
InitializeComponent();
Items = new ObservableCollection<string>(Enumerable.Range(0, 100).Select(x => "some text"));
DataContext = this;
}
public ObservableCollection<string> Items { get; set; }
}
When this code runs, the vertical scroll bar for the ScrollViewer goes down to the bottom. However, if I remove the binding in the Run in the RichTextBox and hard-code the text:
<Run Text="some text"/>
Now the scroll bar stays at the top (as I would expect).
Is this a bug? If not, what is going on? How can I fix this (note: this is simplified XAML, I need the ScrollViewer because the ListBox is actually in a grid)?
I canĀ“t tell you why the ScrollViewer behaves like that, but i would change the XAML to the following. Then is the scroller at the top, if you use binding or not in the DataTemplate.
XAML:
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<RichTextBox>
<Paragraph>
<Run Text="{Binding}"/>
</Paragraph>
</RichTextBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Make it Fixed width and Height of scrollviewer depends upon the grid row and column size. it help to fixed size in run time. like that
<ScrollViewer VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" HorizontalScrollBarVisibility="Auto" Width="135" Height="463">
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Vertical">
<RichTextBox>
<Paragraph>
<Run Text="{Binding}"/>
</Paragraph>
</RichTextBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I hope its helpful
try setting the listbox's height to auto and make the scrollviewer height fixed. that way, the scrollbars only show when the listbox's height is greater than the scrollviewer's height.
But, looking at the way the objects are defined. You are gonna have one big problem in the future. That is, in SL4, Listboxes take up height and don't give it back. So if you have something that expands inside the listbox (i.e. Accordion items) or allow deleting inside the listbox, the listbox would expand to show all it's items. But once an item is deleted, it will never give back the height. The result would be your scrollbar always shows even when you have nothing more to show at the bottom.
That is completely out of topic but I felt that I should let you know.
I hope I helped, if not now, then for the future.
I finally came up with a solution to this problem. I removed the ScrollViewer from the RichTextBox template.
Set the MaxHeight on the Listbox, this will allow the scrollviewer to only show up when the screen dimensions are too small.
Thank you so much! You just saved me days of pain and suffering... :)
For those (like me) who wonder how to remove the scrollviewer from the rtb template :
Extract the template with blend.
Find the scrollviewer element and replace it with a stackpanel (keep the x:name attribute).

wpf: making textblock height expand when text gets too big for 1 line

I have a listview with an itemtemplate:
<ListView x:Name="messages" HorizontalAlignment="Left"
Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Style="{DynamicResource h3}" Text="{Binding}"
Margin="10" MaxWidth="850"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This listview is in a vertical stackpanel. So its width is the same as the stackpanel's width.
The listview must show messages that could be very long. I'm trying to make sure that when a message is too long for the available width, the textblock gets extra height and the text gets displayed on 2 lines.
I can google a lot of ways to have this achieved with a fixed height, but since I don't know in advance if I'll need more than 1 line, I'd like to make sure it happens automaticly.
I don't want every item to have the height of 2 lines, only when it's needed.
How can I achieve this?
Have you tried the TextWrapping property? It seems that it would do what you want.

center align contents of a listbox in silverlight

I have a list box which is of a certain fixed width. The number of items in the listbox varies. Is there a way to center the contents of the list box? The "Content Presenter" of the ListBoxItem ,centers each item inside its Template instead of centering it with respect to the entire listbox width.
Sorry about not replying earlier. The issue was with the width of the ItemsPanelTemplate which I was using in my Listbox. Earlier Width was set to 925. Changing this Width to MaxWidth worked. The code:
<ItemsPanelTemplate x:Key="ItemsPanelKey">
<Contact:AnimatedWrapPanel HorizontalAlignment="Center" MaxWidth="925">
<Contact:AnimatedWrapPanel.Interpolation>
<interpolate:BackInterpolation Amplitude=".5" Suppression=".2" EdgeBehavior="5"/>
</Contact:AnimatedWrapPanel.Interpolation>
</Contact:AnimatedWrapPanel>
</ItemsPanelTemplate>
Not sure, but sounds like what you want is a custom item template that centers each item. If I'm right, the only tricky thing is that the template has to be the same fixed width as the listbox. So if your listbox contains some object Foo and Foo.Text is a simple text property to be displayed, you could do it like so in Xaml:
<ListBox x:Name="TheListBox" Width="300">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="300">
<TextBlock Text="{Binding Text}" HorizontalAlignment="Center" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and the code behind contains something like:
List<Foo> ListOfFoo = new List<Foo>();
ListOfFoo.Add(new Foo(){Text="Something"});
ListOfFoo.Add(new Foo(){Text="Something Else"});
ListOfFoo.Add(new Foo(){Text="Something Completely Different"});
TheListBox.ItemsSource = ListOfFoo;
If it's more complex or you can't make it work, post some code and we'll go from there.

Resources