WPF ListView control is populated but invisible
I have a ListView, populated with ItemsSource pointing to a list of login histories, so that I can choose from previous successful logins instead of having to type user, host, etc. over. This seemed to be working fine, but for some reason now the databinding is working fine but the items in the control are invisible. I can tell the control is being populated correctly becau开发者_JAVA技巧se "ghosts" of the items are available and highlight when I mouseover them, in the correct number as in my ItemsSource, and they are bound correctly - for instance if my ItemsSource has LoginA, LoginQ, and LoginZ and click on the second 'invisible' item, LoginQ happens. I've tried messing with opacity, changing where ItemsSource is bound (after/before login dialog appears, etc). with no joy. Compared to an older working version I can't see anything that appears to be relevant. Hints, suggestions, and catcalls welcome.....
It sounds like the properties in your list item type are either not actually properties (they could be public fields instead, which you cannot bind to), or their values change later in your code and you haven't implemented INotifyPropertyChanged to notify the XAML binding engine that their values have updated.
devdigital's answer helped me find the problem; I was attempting to bind to the fields in my LoginHistory class which were defined as:
public string foo;
I had been previously defining the fields as:
public string foo {get; set; }
but changed them when I was having some issues with accessibility, and never changed them back. Having the { get; set; } defined allows them to be visible to the databinding again. Ken (I'm the OP, but somehow the question originally got posted as a 'new user', so I don't think I can marked these as answered).
I had the same problem and the problem was in the binding, in xaml file.
<GridViewColumn Header="Category" Width="Auto" DisplayMemberBinding="{Binding yourVariable}"/>
I had changed some lines in the code behind (xaml.cs) but not in the xaml, so the binding wasn't working properly (the listView in xaml.cs was populated but WPF didn't know how to put them in the ListView in xaml).
The moment I fix that in the xaml, the lines in the ListView turn visible.
Here is the class for my ItemsSource: List, and the XAML which references it. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public class LoginHistory { public string user; public string host; public string company; blah, blah, blah......... }
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<Border Name="mask" Opacity="0.8" Background="White" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" CornerRadius="0,0,0,10"/>
<ListView Background="White" BorderThickness="0" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Stretch"
x:Name="HistoryListView" VerticalAlignment="Stretch" Width="Auto"
Height="Auto" FontSize="14" SelectionMode="Single"
SelectionChanged="HistoryListView_SelectionChanged">
<ListView.OpacityMask>
<VisualBrush Visual="{Binding ElementName=mask}"/>
</ListView.OpacityMask>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Height" Value="40"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<!--
<GridView ColumnHeaderTemplate="{StaticResource HeaderTemplateNoArrow}" AllowsColumnReorder="True">
-->
User
<GridViewColumn DisplayMemberBinding="{Binding Path=company}">
<GridViewColumnHeader>Company</GridViewColumnHeader>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=host}">
<GridViewColumnHeader>Host</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Border BorderThickness="1" Margin="0" BorderBrush="Gray" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" CornerRadius="0,0,0,10">
</Border>
精彩评论