c#


WPF - TextBlocks in ListView are not horizontal aligned


I have made a ListView in a WPF-application with different TextBlocks. Each row can contain several TextBlocks, but when a row contains more than one text block, the first one fits to the row, but the following TextBlocks does not align horizontally (see picture). I have no idea what could be the cause of this, so I hope someone could provide some clarification.
Below is the XAML-code.
<ListView ItemsSource="{Binding DataContext.Mechanics, RelativeSource={RelativeSource AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}" Height="auto" Width="1330"
Grid.Row="2" Grid.ColumnSpan="13" BorderThickness="0" Background="#FFF2F2F2">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="18"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="22"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="1200"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding MecID}" Grid.Column="0" Padding="0" FontSize="11" Background="#FFF2F2F2"/>
<Label Content="{Binding Name}" Grid.Column="1" Padding="0" FontSize="11" Margin="2, 0, 0, 0"/>
<ListView ItemsSource="{Binding MecJobs, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Padding="-1" Height="18" Width="1200" Grid.Column="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="White">
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListView.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<ListViewItem Width="{Binding EstimatedTimeWidth}" Canvas.Left="{Binding Margin}" Background="{Binding Color}" Height="18"
Padding="0" Margin="0" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<TextBlock TextAlignment="Center" Height="18" Padding="0" Margin="0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="RegNumber" />
<Binding Path="CustomerName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ListViewItem>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
EDIT: The problem was solved when the ListView was set with to include a Stackpanel with Orientation="Horizontal".
I think if you add a style for a listview item to the item container style your problem gets fixed.
If not just move the setter to your list box item style.
<ListView ItemsSource="{Binding DataContext.Mechanics, RelativeSource={RelativeSource AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged}" Height="auto" Width="1330"
Grid.Row="2" Grid.ColumnSpan="13" BorderThickness="0" Background="#FFF2F2F2">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="18"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="22"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="1200"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding MecID}" Grid.Column="0" Padding="0" FontSize="11" Background="#FFF2F2F2"/>
<Label Content="{Binding Name}" Grid.Column="1" Padding="0" FontSize="11" Margin="2, 0, 0, 0"/>
<ListView ItemsSource="{Binding MecJobs, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Padding="-1" Height="18" Width="1200" Grid.Column="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="White">
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListView.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<ListViewItem Width="{Binding EstimatedTimeWidth}" Canvas.Left="{Binding Margin}" Background="{Binding Color}" Height="18"
Padding="0" Margin="0" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<TextBlock TextAlignment="Center" Height="18" Padding="0" Margin="0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="RegNumber" />
<Binding Path="CustomerName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ListViewItem>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>

Related Links

C# HttpWebRequest SEC_I_RENEGOTIATE Intermittent Errors
Entity Framework Timeouts
SqlException Transaction was deadlocked on communication buffer resources
Delete multiple rows from Grid
Image overlay in WPF?
Ribbon ApplicationMenu AuxilaryPane Size
How to manage multiple display setting in Windows 7 programatically?
Iterating a List in an Object Initializer
Using extension methods through the TryInvokeMember override of a DynamicObject
Restrict access to some .aspx pages
Subscribe to an event many times with EventHandlerList
How should i add data to a database in an application that implements Forms Security?
Cannot read the property “Patient” from NULL
Pass windows credentials when invoke wcf service from other wcf service
Align text vertically with VisualStyleRenderer for VisualStyleElement.ToolTip.Standard.Normal
Disabling or making it readonly a part of the values in a Text Box (.net)

Categories

HOME
asterisk
http
model-view-controller
google-cloud-pubsub
twitter-bootstrap
redux
azure-active-directory
teradata
netty
mobile
virtual-machine
pelican
x86-64
arguments
confluence-rest-api
sugarcrm
markdown
rethinkdb
theory
kairosdb
urllib2
email-attachments
derby
github-flavored-markdown
vivado-hls
google-calendar-api3
md2
audio-streaming
glibc
single-page-application
connectiq
screen-scraping
scrapy-spider
symlink
large-files
boosting
typeorm
custom-fields
jboss-arquillian
slim-lang
rtos
tunnel
pyenv
ponylang
border-layout
pygraphviz
file-conversion
socketcluster
google-cloud-powershell
mtm
taiga
activeandroid
hierarchical-clustering
stream-socket-client
lucee
serialversionuid
extjs3
sharpdevelop
activesupport
jtextarea
bipartite
ios-keyboard-extension
suitesparse
ownership
symantec
tinybox2
cosign-api
falcon
django-taggit
aws-kms
jde
python-rq
treelist
google-app-engine-php
ewam
gtkwave
maven-antrun-plugin
gitk
serial-communication
consul-template
transparentproxy
wingdb
numerical-recipes
chunks
pysvn
fedena
agent-based-modeling
ios-enterprise
object-oriented-database
pthreads-win32
nvcc
carrier
google-mirror-api
ia-32
nsnumber
kohana-orm
violation
simplemembership
algol
certificate-revocation
flatten
nsscrollview
django-1.4
numerical-computing
wtsapi32
weak-typing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App