How to restrict length of cell in ListView? - c#

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>

Related

How to bind properties of one ObservableCollection of ListView to properties of SelectedItem of another ListView?

So I have a few ListViews. The first is binded to ObservaleCollection<ComPort>. All properties of ComPort may take some predefined values. Other ListViews are responsible for that properties: they show all that possible (predefined) values and SelectedItem should be the current value of that property of ComPort from the first ObservaleCollection.
I can't attach images so here is an external picture, it would make the situation clean: http://i.stack.imgur.com/ZBRRx.png
<Window.Resources>
<ResourceDictionary x:Name="rd">
<l:ComPorts x:Key="vComPorts"/>
<l:SystemPorts x:Key="vSystemPorts"/>
<l:BaudRates x:Key="vBaudRate"/>
<l:Parities x:Key="vParities"/>
<l:DataBits x:Key="vDataBits"/>
<l:StopBits x:Key="vStopBits"/>
<l:Timeouts x:Key="vTimeouts"/>
<l:ComPort x:Key="vSelectedPort"/>
</ResourceDictionary>
</Window.Resources>
...
<ListView
Name="PortsList"
Grid.Row="1"
Grid.Column="0"
Margin="5"
VerticalAlignment="Stretch"
ItemsSource="{StaticResource vComPorts}"
DataContext="{StaticResource vComPorts}"
SelectedValuePath="PortName"
SelectedValue="{Binding ElementName=SystemPortsList, Path=SelectedItem.Value}"
SelectionChanged="PortsList_SelectionChanged"
MouseDoubleClick="PortsList_MouseDoubleClick">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox />
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView
x:Name="SystemPortsList"
Margin="5"
VerticalAlignment="Stretch"
DataContext="{Binding Source={StaticResource vSelectedPort}}"
ItemsSource="{Binding Source={StaticResource vSystemPortsView}}"
SelectedItem="{Binding Source={StaticResource vSelectedPort}, Path=PortName}"
MouseEnter="SystemPortsList_Refresh"
MouseLeave="SystemPortsList_Refresh"
Grid.Row="1"
Grid.Column="1" SelectionChanged="SystemPortsList_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="tb" Margin="5,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</ListView.ItemTemplate>
</ListView>
I've tried to make an instance of class ComPort for saving current value of selected item from the first ListView, but anyway I can't cope with it without help. How this task should be solved?
1) Instead of handling SelectionChanged on the PortsList ListView, bind your checkbox to the ListViewItemsPanel like so:
<CheckBox IsChecked={Binding IsSelected, RelativeSource=Parent/>
2) Add an x:Name to your first ListBox, say x:Name="ComPortLB";
3) Remove DataContext on SystemPortsList;
4) Fix SelectedItem on SystemPortsList like so:
SelectedValue="{Binding ElementName=ComPortLB, Path=SelectedValue.PortName}"
I haven't tested any of this code and I haven't done this kind of stuff for a while, so I apologize for errors, but it should get you closer. I've also had to make some assumptions about your classes since you don't provide enough information.

How can I get chart to update with filter(ListCollectionView) in WPF?

I'm able to apply filter to an ObservableCollection and it applies to a listbox and a datagrid that I have in the XAML-code, but the chart that I have doesn't update. All have the same DataContext.
XAML-code:
<ListBox Name="lst" ItemsSource="{Binding Measurements}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding sDate}"></TextBlock>
<TextBlock Text="{Binding Place}"></TextBlock>
<TextBlock Text="{Binding Consumtion}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<tk:Chart Margin="0,20,0,0" Name="chart" Title="{Binding ElementName=place, Path=SelectedItem.Content}" Height="279">
<tk:ColumnSeries IndependentValuePath="sDate" DependentValuePath="Consumtion" Title="Consumtion" ItemsSource="{Binding}"></tk:ColumnSeries>
<tk:Chart.Axes>
<tk:CategoryAxis Orientation="X" Title="Personer"></tk:CategoryAxis>
<tk:CategoryAxis Orientation="Y" Title="Förbrukning (kWh)"></tk:CategoryAxis>
</tk:Chart.Axes>
</tk:Chart>
<DataGrid Margin="0,20,0,0" Name="grid"></DataGrid>
C# code:
DataContext = vm;
chart.DataContext = vm.Measurements;
grid.ItemsSource = vm.Measurements;
I'm applying the filter by pressing a button and the listbox and the gridview updates but not the chart, is there a way to also update the chart?

What control I use to repeat List<string> in WPF

I have a news model which has Title and Excerpt properties and need to display them like this image.
ObservableCollection<NewsModel>(getNews())
my question is what control should I use to accomplish this view?
You could use ListView to accomplish your task, Something like this,
<ListView Name="listView1" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Path=Title}" MinWidth="80" />
<TextBlock Text="{Binding Path=Description}" MinWidth="80" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>

How to change value of control inside pivot datatemplate on listpicker_selectedIndexChagned event

<phone:Pivot Title="Bank" x:Name="pivotBank"
ItemsSource="{Binding PivotItems}">
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding StrBankName}"/>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<toolkit:ListPicker x:Name="listPickerAccountNumber" SelectionChanged="listPickerAccountNumber_SelectionChanged"
ItemsSource="{Binding listAccountDetails}" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StrAccountNumber}" />
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StrAccountNumber}" />
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
<TextBlock Text="Account Detail"></TextBlock>
<TextBlock Name="textBlockAccountNumber" Text="{Binding StrAccountNumber}"></TextBlock>
<TextBlock Name="textBlockBalance" Text="{Binding DblBalance}"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
Above pivot control contains headers as bank names and pivot datatemplate contains list of accounts. I want to change the control values of textBlockAccountNumber and textBlockBalance on SelectedIndexChanged event of listPicker.
My question is, is there any way to find control by name from the pivot. I used INotifyPropertyChanged Interface and implemented event with handler but not able to get the rid of it.

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>

Categories