Add an expander as an item in listview - c#

I have a ListView into which I would like to insert a Expander control as an item (e.g in the third column). What function/method in ListView helps me do this?

You can change the ItemTemplate like this:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Title}">
<TextBlock Text="{Binding Description}" />
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

Related

How to restrict length of cell in ListView?

I have such a list view
And there is a .xalm
...
<ListView
x:Name="LVLog"
ToolTip="Log of task(s) execution"
Background="WhiteSmoke"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
...
And there is how I add items in LVLog
LVLog.Items.Add(message.Log);
As you can see if line a long enough it goes out of the borders and user need to scroll horizontaly in order to read log up to the end.
Question is: is there is a way to write line at the next line if it came to the borders?
You could use a TextBlock as an ItemTemplate and set TextTrimming on it. So the text will be trimmed
Long long lo..
and the full text you will see in tooltip:
<ListView ItemsSource="{Binding YourCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ...}" TextTrimming="CharacterEllipsis" ToolTip="{Binding ...}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
If you want to have more lines, then just set TextWrapping="Wrap" in the TextBlock:
<ListView ItemsSource="{Binding YourCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ...}" TextWrapping="Wrap"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

WPF ListView ItemTemplate too large space per Label

I have a data bound ListView that uses a rather strange heights for every ListViewItem after I start using an ItemTemplate.
<ListView ItemsSource="{Binding Path=AllTvShows}">
<ListView.ItemTemplate>
<DataTemplate>
<Label FontStretch="Normal" VerticalAlignment="Center" Content="{Binding Path=Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is how it looks like:
Without the ItemTemplate the labels have the normal height (more adjacent). How should I specify the Label so that it would render normal?
Use a TextBlock instead of a Label:
<ListView ItemsSource="{Binding Path=AllTvShows}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

Binding visibility to isChecked value of a checkbox

I'm trying to bind my stackpanel's visibility to the checkbox's isChecked value. It's a common problem, but I just can't figure it out.
Converter:
<Page.Resources>
<common:BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Page.Resources>
StackPanel I want to mess with(I removed some code, so don't you worry about listview's binding, I set it in c#):
<ListView x:Name="aktualniGracze" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="stackLiczymy" Visibility="{Binding isChecked, ElementName=czyLiczymy, Converter={StaticResource BoolToVis}}">
<TextBlock Text="{Binding ileWypil}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
CheckBox:
<CheckBox Name="czyLiczymy"/>
Nothing happens when I change the state of the checkbox, any clues?
Thanks a lot in advance.
If you have your correct DataContext in CheckBox, I think you are just missing the DataContext.isChecked
<ListView x:Name="aktualniGracze" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="stackLiczymy" Visibility="{Binding DataContext.isChecked, ElementName=czyLiczymy, Converter={StaticResource BoolToVis}}">
<TextBlock Text="{Binding ileWypil}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

XAML layout ItemsControl

I have searched extensively on Google and am struggling to find an easy example to follow for an ItemsControl which I think I need to be able to do the following.
I currently have a Grid with a scrollable Listbox containing Checkboxes which works as expected using the following XAML layout
<Grid>
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Selections}" Margin="12,22,12,94">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}"
Content="{Binding Path=Item.SelectionName}">
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I now want to add a TextBox to the right of each Checkbox so it's aligned horizontally with 1 Checkbox and 1 Textbox per row. I thought I would have to use an ItemsControl to allow two controls but using the ItemsControl as below I lose the ability to scroll
<Grid>
<ItemsControl ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Selections}" Margin="12,22,12,94">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}"
Content="{Binding Path=Item.SelectionName}">
</CheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Also, if I try and add a textbox similar to
<DataTemplate>
<CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
<TextBox />
</DataTemplate>
I get an error The object 'DataTemplate' already has a child and cannot add 'TextBox'
Essentially, I want it to look like this
Can anyone give me a few pointers on how to structure the controls in XAML to get my desired layout?
Just use a StackPanel in your DataTemplate and set the orientation to horizontal.
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
<TextBox />
</StackPanel>
</DataTemplate>

How to bind a ListBox item to a user control?

People use frequently something like:
<ListBox ItemsSource="{Binding ElementName=thisControl, Path=ListIndexes}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Path=IndexName}"/>
<Label Content="{Binding Path=IndexValue}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But I would like to use, instead of labels, a control, like this:
<ListBox ItemsSource="{Binding ElementName=thisControl, Path=ListIndexes}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:Index Item="**{Binding}**"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My doubt is what to put into this Binding to include the whole item from the collection.
The syntax for this is:
<local:Index Item="{Binding}"/>
This will tell the data binding functions to bind the entire datacontext for each ListBox Item to the Item property in your Index control

Categories