how to draw wpf ui with many many controls? - wpf

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>

Related

Change background color between ListBoxItems

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.

How can I bind Image control to ListBox dynamically?

I want to implement a ListBox which each item include a Image control and some other controls, then use it to binding some data. Due to this ListBox need to contains more than 2000 items, this means I have to do some optimization for it.
First, I noticed that most of the Image controls has one same picture(Default avatar), so I create a singleton ImageSource-object for the data. But although the Image control's source is the same object, you know I also need to create 2000 Image control in the ListBox with DataTemplate below:
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Avatar}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Is there have a way to reduce Image control object's number in my program? Thank you!

Use a user control as a DataTemplate within a WPF application

I am trying to create a user control within a WPF application that will serve as a DataTemplate for a ListBoxItem. The user control a grid with 4 TextBlocks. This control also contains other shapes and images more for visual aid than anything so I am omitting them from the code in this question for clarity.
When I drop the user control on my mainwindow.xaml I can see the control and the properly bound fields point to the first record in the datasource. What I want to do is have this control render repeatedly within either a listbox or a wrap panel for each record within the database.
Can anyone provide me with a pointer or sample of how to have a user control render as a DataTemplate within the ListBox control/other panel.
Up to this point I have tried the following with no avail:
Thanks in advance for any tips.
<!--within Window.Resource -->
<DataTemplate x:Key="myActivity">
<local:ucActivityItm /> <!--usercontrol -->
</DataTemplate>
<!-- Listbox within the window -->
<ListBox HorizontalAlignment="Stretch" ItemTemplate="{DynamicResource myActivity}" VerticalAlignment="Stretch">
<ListBoxItem>
<!-- control also added for testing to ensure it rendered out-->
<local:ucActivityItm />
</ListBoxItem>
</ListBox>
That DataTemplate isn't actually being assigned to your ListBox. There's three ways:
1: Replace the template in the Resources section with
<ListBox.ItemTemplate>
<DataTemplate>
<local:ucActivityItm />
</DataTemplate>
</ListBox.ItemTemplate>
in the ListBox.
2: Somewhat related:
<ListBox ... ItemTemplate="{StaticResource myActivity}">
3: Set the DataType parameter of the DataTemplate above to whatever the content of your ListBox is.
<DataTemplate x:Key="myActivity" DataType="{x:Type ...}">
I usually just do the first, but any should work.

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