How can I right align numbers in an ItemsControl - wpf

In the xaml I have :
<ItemsControl Grid.Row="1" Grid.Column="3" x:Name="SelectedCountCollection" ItemsSource="{Binding SelectedCountList}" Foreground="Purple" Margin="0,0,1,0" HorizontalContentAlignment="Stretch"/>
I end up with data looking like so :
20
5
7
I need to shift the 5 to the unit's position. I need to shift 7 to the unit's position.

Related

StackPanel items don't wrap to new lines

I have an ItemsControl with a StackPanel as the ItemsPanelTemplate and the items which get displayed inside (the Button) are not wrapped to new lines. I've tried adding the HorizontalContentAlignment property to the ItemsControl and play with the options but it doesn't help. What am I doing wrong?
<ItemsControl ItemsSource="{Binding RecipientsNames}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="btnContact" Click="BtnContact_Click"
Width="Auto" Height="14" Padding="0" BorderThickness="0" Margin="0 0 6 0" HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock Text="{Binding Path=Name}" FontSize="12" Margin="0 -2 0 -2"/>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
you are getting your panels mixed up,
A StackPanel expands infinity in a linear direction.
a WrapPanel stacks elements until they hit the edge of the visible
area then starts a new stack under the previous expanding
perpendicular to the stacking direction
a UniformGrid sizes itself to fit the entire area and then splits it
into a set number of rows and columns, so if you set it to 3 columns
and add 4 items the 4th will appear in column 1 of a second row,
because the uniform grid unlike the WrapPanel this isn't affected by item size i generally find it produces more visually appealing layouts to the wrap panel but does need to be configured to make best use of the available space

ScrollViewer is not working for TextBlock in WPF

Friends, below is my code. I don't know why my scroll bar is not working.
<Window x:Class="Seris.Views.Help"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Help" Height="400" Width="400">
<DockPanel>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="350" Width="372"><InlineUIContainer>
<Image Height="100" Width="100" RenderTransformOrigin="1.37,0.46" Source="../Images/help.jpg"/>
</InlineUIContainer><Run/><LineBreak/><Run Text="There are some validations on the Vehicle Form as below."/><LineBreak/><Run Text="P"/><Run Text="lease note that all the fields are mandatory."/><LineBreak/><Run/><LineBreak/><Run FontWeight="Bold" Text="Vehicle No"/><LineBreak/><Run Text="It should be always 7 character long con"/><Run Text="taining "/><Run Text="first 3 digits as alphabet in capital form and remaining 4 numerical. "/><LineBreak/><Run Text="i.e. GHI1234"/><LineBreak/><Run FontWeight="Bold" Text="Model"/><LineBreak/><Run Text="It can be anything except null."/><LineBreak/><Run Text="i.e. Fluid"/><LineBreak/><Run FontWeight="Bold" Text="Manufacturing Date"/><LineBreak/><Run Text="It should not be a future date."/><LineBreak/><Run FontWeight="Bold" Text="IU No"/><LineBreak/><Run Text="It must be 10 digit numerical number."/><LineBreak/><Run FontWeight="Bold" Text="Personnel Name"/><LineBreak/><Run Text="You must select Personnel Name from combo box."/><LineBreak/>
</TextBlock>
</ScrollViewer>
</DockPanel>
</Window>
Remove Height="350" & Width="372" from the TextBlock, these are preventing ScrollViewer to work properly
if you want to restrict the size you may apply the same to DockPanel or ScrollViewer as needed.
so in short if you apply width or height to TextBlock that will restrict the size of the element and ScrollViewer may not work as expected.
additionally you may also remove HorizontalAlignment="Left" & VerticalAlignment="Top" from the TextBlock as they might not be required as well

How to set the number of ListView columns dynamically

I got a window containing two usercontrols, a usercontrol with a listview and a usercontrol with some other controls.
Something like this:
+-------------------------------------------------+
| header stuff |
+---------------------------+---------------------+
| usercontrol with listview | another usercontrol |
+-------------------------------------------------+
| footer stuff |
+-------------------------------------------------+
Problem: on screens with low resolution its only possible to see around 2 of the 3 columns and you need to scroll to see the thrid column. I want to avoid the horizontal scrolling by dynamically setting whether to show 1,2 or 3 columns depending on the width. Another problem is that the names can
be very long, so the width of the items in the listview all have same width as the the longest name.
Code for listview:
<ListView Name="lstContacts"
ItemsSource="{Binding Path=Contacts}"
IsSynchronizedWithCurrentItem="True"
ItemContainerStyle="{StaticResource RoundedItem}"
SelectionMode="Single"
HorizontalContentAlignment="Center"
VerticalAlignment="Top"
HorizontalAlignment="Stretch" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="3">
<TextBlock FontWeight="Bold" FontSize="20" Text="{Binding Path=Identifier}" HorizontalAlignment="Center" />
<TextBlock FontSize="16" Text="{Binding Path=Name}" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
Any ideas on how to sort this out ?
Perhaps the listview is a bad choice ?
Thanks.
You could use a WrapPanel as the ItemsPanel:
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
Note that there is no native virtualizing WrapPanel, so this will not perform well for a large collection of items.
since your names can be long you can :
1- use textbox having textwrapping and Max/Min Height.
2- decrease fonts size for Names.
3- put Names in a ViewBox for them to always fit. (wrap also)

Silverlight ItemsControl formatting

I have a ItemsControl in Silverlight to display a list of objects. These objects contain the strings Name, Value and Unit such as "Load", "100" and "MW". The control is within a grid column which can vary in size depending on the size of the browser window. I am trying to format the DataTemplate to allow the Name string to be on the left with the Value and Unit to be on the right. eg...
|Load 100 MW |
|Load2 50 MW |
|Unit1 20 X |
|Unit2 130 YXZ|
After a lot of trial and error I have managed to get this to work by using a grid with two columns. The left containing the name and the right containing a stack panel containing both the value and unit. This seems to work but if there are any units which differ in length the alignment of the text doesn't work. eg...
|Load 100 MW|
|Load2 50 MW|
|Unit1 20 X|
|Unit2 130 YXZ|
I'm running out of ideas on how to format this. Can anyone suggest anything? The main point is that I do not know in advance the length of the name, value or unit strings and, when the main column changes size, the name must stay on the left with the value and unit on the right.
Thanks in advance, Cap
(here is the code so far)
<ItemsControl Name="DataTypesGrid" ItemsSource="{Binding}" Margin="0,8,0,0" BorderBrush="{x:Null}" Foreground="White" Background="{x:Null}" IsEnabled="True">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" Margin="0,2,0,0" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left" Grid.Column="0"/>
<StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{Binding Value}" HorizontalAlignment="Right" Margin="0,0,4,0"/>
<TextBlock Text="{Binding Unit}" HorizontalAlignment="Right"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
At the moment you basically start again with each new row, calculating grid column widths based on a single data entry.
The problem is you want width behaviour that spans rows "just like a datagrid column". Sounds like you actually want to customise a datagrid instead and strip out any headings you don't want instead.
Some alternatives:
Set a minimum width on your units box so that smaller units, at least, align.
Calculate the actual width of the widest unit and apply that value to a width binding (used on all "units" textblocks)

Easy way to change Grid.Row and Grid.Column in many controls in WPF

I have this situation 1000 times already:
I have grid with 2 columns and many rows. Mostly i have TextBlock in first column and TextBox in second but not always. Now when I have 50 rows and I decide to move row 49 to the first row in grid, and I don't want to swap rows, I want to insert it to first place. Then I need to change values of Grid.Row and Grid.Column of all the rest of controls. This i driving me crazy. How can I make this easier?
You could use something like the markup extension i outlined in this question right from the beginning, if you then need to insert a new row that should be less trouble.
If you have formatted your xaml code well, you can hold Alt key to select and cut Grid.Row="x" in every line and hold Alt key to select and paste them one line down.
eg.
<TextBlock Grid.Row="0" Text="a"/>
<TextBlock Grid.Row="1" Text="b"/>
<TextBlock Grid.Row="2" Text="c"/>
<TextBlock Grid.Row="3" Text="d"/>
you want to move <TextBlock Grid.Row="3" Text="d"/> to the top. Just hold "Alt" key to select and copy:
Grid.Row="1"
Grid.Row="2"
Grid.Row="3"
Use Alt key again to paste them to replace the original
Grid.Row="0"
Grid.Row="1"
Grid.Row="2"
so you well get
<TextBlock Grid.Row="1" Text="a"/>
<TextBlock Grid.Row="2" Text="b"/>
<TextBlock Grid.Row="3" Text="c"/>
<TextBlock Grid.Row="3" Text="e"/>
Then just move <TextBlock Grid.Row="3" Text="d"/> to the top and change it Grid.Row to 0
It sounds like you'd be better off using a ListBox or ListView.

Resources