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>
Related
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>
I am using WPF and I have DataTemplate that is i want to access into the codebehind how I can use this?
<DataTemplate x:Name="PersonDateTemplate">
<StackPanel Orientation="Horizontal">
<Label x:Name="lblhr" Height="40px" Width="50px"
Content="{Binding Path=hrvalueinitially}" FontSize="20px"
HorizontalAlignment="Left" Background="#555555" Foreground="White"
FlowDirection="LeftToRight"></Label>
<TextBlock x:Name="items" Text="{Binding}" Margin="35,0,0,0"></TextBlock>
</StackPanel>
</DataTemplate>
If you are having the DataTemplate in Resource and you have Key defined, you can access the resource in CodeBehind as follows,
DataTemplate dataTemplate = App.Current.TryFindResource("PersonDateTemplate") as DataTemplate;
or if you want create from scratch in CodeBehind, you should use FrameworkElementFactory
You can use dataTemplate to replace the visual appearance of a data item in a control like ListBox, ComboBox or ListView.
To understand how to work with dataTemplate I've done the following example:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ID}" FontSize="24"/>
<TextBlock Text=". Name: " FontSize="24"/>
<TextBlock Text="{Binding Name}" FontSize="24"/>
<TextBlock Text=" ,Age: " FontSize="24"/>
<TextBlock Text="{Binding Age}" FontSize="24"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
For better understanding about the data template you may follow the following link:
https://msdn.microsoft.com/en-us/library/ms742521(v=vs.110).aspx
<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.
I have seen a lot of questions and answers with almost the same problem, but none of these answers arent working for me. Soo, my code is:
<ListBox ItemsSource="{Binding Avakuvaandmed}" x:Name="lboxandmed" HorizontalAlignment="Left" Height="552" VerticalAlignment="Top" Width="970" SelectionChanged="lboxandmed_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" x:Name="spanVärviSeda">
I HAVE TO GET VALUE OF THIS --> <TextBlock x:Name="IDbox" Width="50" Text="{Binding Id}"></TextBlock>
<TextBlock Width="130" Text="{Binding Nrmärk}"></TextBlock>
<TextBlock x:Name="txtKehtivus" Width="130" Text="{Binding Lõpp}"></TextBlock>
<TextBlock Width="130" Text="{Binding Eesnimi}"></TextBlock>
<TextBlock Width="130" Text="{Binding Perenimi}"></TextBlock>
<TextBlock Width="130" Text="{Binding Mark}"></TextBlock>
<TextBlock Width="130" Text="{Binding Mudel}"></TextBlock>
<TextBlock Width="130" Text="{Binding Aasta}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And I have to get the value of the textblock named "IDbox".
Please can someone help me, or atleast give me a clue how.
Your code looks correct. If you want to access the Value of IDbox in code behind then you can do it by Avakuvaandmed.ElementAt(rowno).Id because you are binding Id to the IDBox. If you want to access the BoxId value in xaml. Then use binding as follows:
{Binding Avakuvaandmed[rowno],Path=Id}
You can also access Textblock value by using VisualTreeHelper class. You will need to go traverse all elements in ListBox.
I currently have a list box:
<ListBox HorizontalAlignment="Left"
ItemsSource="{Binding Data, ElementName=bookingDomainDataSource}"
Margin="158,134,0,45"
x:Name="bookingListBox"
Width="429"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay, ElementName=bookingComboBox}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=userId}"
Width="100" />
<TextBlock Text="{Binding Path=bookingName}"
Width="100" />
<TextBlock Text="{Binding Path=bookingDate}"
Width="100" />
<TextBlock Text="{Binding Path=showId}"
Width="100" />
<TextBlock Text="{Binding Path=paymentId}"
Width="100" />
<TextBlock Text="{Binding Path=ticketId}"
Width="100" />
<TextBlock Text="{Binding Path=ticketQuantity}"
Width="100" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And I would like to only show rows from the itemssource that have a certain userId, how can I do this?
Thanks.
I think the best solution to this would be to filter the data source BEFORE you get to the front end.
You want to define a filter for your listview.
Uodate: sorry missed the silverlight tag. However CollectionViewSource should still be useful to you. Here a sample using the CollectionViewSource in Silverlight.