1

I have a list view that shows some logs (of type LogItem). The ListView.View is set to a GridView in XAML. The GridView has two columns where the data templates for each column is set to a textbox. The first column binds to a DateTime property of the LogItem, so the width of the first column could be set to Auto (preferred) or even a fixed width i required.

The second gridviewcolumn is binding to a log message string, so it could have any width.

My question is, how can I make sure all text is visible in the listview such that I get a scrollbar in the listview if the log message width is larger than the listview's width, and that all the text is visible?

At the moment the size of the second column seems to be set by the first item added to the collection that the ListView binds to, then the width never updates. So typically I just see a small part of the log messages and the second column header does not even span the complet ListView width.

I have looked around for a solution to the problem. There seems to be more people asking the same question, but so far none of the answers I have found has solved the problem. I hope someone could help to guide me in the right direction, what have I missed.

XAML code below

<ListView Grid.Row="1"
          HorizontalAlignment="Stretch"
          IsSynchronizedWithCurrentItem="False" 
          ItemsSource="{Binding LogItems}"  
          x:Name="LogListView" 
          FontSize="14">
    <!--:GridViewColumnResize.Enabled="True" -->
    <ListView.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Gray" Offset="1"/>
            <GradientStop Color="White"/>
        </LinearGradientBrush>
    </ListView.Background>

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Background" Value="{Binding BackColor}"/>
            <Setter Property="TextElement.Foreground" Value="White"/>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected" Value="true" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="Gold" />
                    <Setter Property="BorderBrush" Value="Black" />
                    <Setter Property="TextElement.Foreground" Value="Black"/>
                </MultiTrigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsMouseOver" Value="true" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="Gold" />
                    <Setter Property="BorderBrush" Value="Black" />
                    <Setter Property="TextElement.Foreground" Value="Black"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.View>
        <GridView>
            <GridViewColumn Header="{Binding TimeText}" Width="Auto">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="TimeLabel" Text="{Binding CreationTime, StringFormat='{}{0:hh:mm:ss.fff}'}" Grid.Column="0"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Header="{Binding MessageText}">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Message}" Grid.Column="1"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>                            
</ListView>

Edit: The full xaml code pastebin.com/24HgAcML

Johan
  • 502
  • 4
  • 18
  • I think you can give the Columns a fixed size for example `Width="0.5*"` and if you need to have all the text visible in a `Textblock` you can take a look at [this](https://stackoverflow.com/questions/1192335/automatic-vertical-scroll-bar-in-wpf-textblock). – Philipp Marquardt Oct 22 '18 at 14:34
  • @PhilippMarquardt, I already tried to set the second column's width to 1*. It did not solve it. Wrapping the textblock in a ScrollViewer in the datatemplate is not an option. I don't want a scroll viewer / logitem only one horizontal scroll for the complete list view. – Johan Oct 22 '18 at 14:41
  • Can you please share your code on LogItems are generated to check in my machine on how the List is getting displayed. – G K Oct 22 '18 at 16:43
  • @Johan somehow I could able to create a dummy class and am able to launch the window and can see some data in the ListView. Intentionally I have added some large text to the LogMessage and I could see the horizontal scrollbar appears for the whole ListView. Is that your expecting or some other thing? – G K Oct 22 '18 at 16:57
  • It looks like some issue with your xaml, if you can post your complete xaml, this helps to figure out the issue. For your reference, my xaml has just a ....and a ListView at Row1. That's all. – G K Oct 22 '18 at 17:00
  • @GK Yes getting the horizontal scrollbar for the complete listview is exactly what I expect (but don't get) for long messages. – Johan Oct 29 '18 at 18:41
  • @GK okay I will post my code as soon as I get back to that computer. I have some ViewBoxes, maybe that is causing the problem... – Johan Oct 29 '18 at 18:42
  • @Johan Okay, share your code. I am on vacation and returned yesterday morning. So couldn't able to reply back. – G K Nov 06 '18 at 15:54
  • @GK Please find the important part of the XAML file here https://pastebin.com/24HgAcML – Johan Nov 12 '18 at 16:47

0 Answers0