I'm following this tutorial on how to use a ListView ItemTemplate to display a list of person : http://www.wpf-tutorial.com/listview-control/listview-data-binding-item-template/
I understood the general concept but I am stuck on one point.
Here is the ItemTemplate sample :
<Grid>
<ListView Margin="10" Name="lvDataBinding" ItemsSource"={Binding MyPersonsList}">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=", " />
<TextBlock Text="Age: " />
<TextBlock Text="{Binding Age}" FontWeight="Bold" />
<TextBlock Text=" (" />
<TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
<TextBlock Text=")" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
What I cannot understand, is how to replace the TextBlocks with a custom control like this :
<Grid>
<ListView Margin="10" Name="lvDataBinding" ItemsSource"={Binding MyPersonsList}">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<MyPersonDisplayer Person="{Binding ???}"/>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
How to tell the template I want to set the MyPersonDisplayer control property Person with the source item ?
EDIT :
I tried to add only Person={Binding}, but it displays me this error.
I think it might have problem about the implementation MenuItemViewModel property in MenuItemView class. Do you have implement the MenuItemViewModel property as DependencyProperty? Maybe you can refer this post.
Related
I have a single Grid that contains a ListView. The ListView is populated with 2 records of employees. How can I detect if focus is lost. E.g the empty space below records or another part of the window is clicked. The record should then be deselected.
<Grid HorizontalAlignment="Left" Height="128" Margin="10,39,0,0" VerticalAlignment="Top" Width="267" LostFocus="Grid_LostFocus">
<ListView Margin="0" Name="listviewEmps" MouseDoubleClick="listviewEmps_MouseDoubleClick" SelectionChanged="listviewEmps_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=", " />
<TextBlock Text="dob: " />
<TextBlock Text="{Binding DOB , StringFormat={}{0:d}}" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
I'm putting data to ListBox from a list of objects using this simple code:
<ListBox Name="lbxListaZadan">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel xml:space="preserve" Orientation="Horizontal">
<TextBlock Text="{Binding title}" />
<TextBlock Text="
" />
<TextBlock Text="Priorytet: " />
<TextBlock Text="{Binding priority}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Instead of expected output:
Task1
Priorytet: 1
Task2
Priorytet:2
Task3
Priorytet: 3
I'm getting:
Task1Priorytet: 1
Task2Priorytet: 2
Task3Priorytet: 3
Why is the newline in a place I would not expect it to be, and how the hell can I reach the expected output in listbox.
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding title}" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Priorytet: " />
<TextBlock Text="{Binding priority}" />
</StackPanel>
</StackPanel>
</DataTemplate>
A newline character can only work inside a single TextBlock, not between two of them.
You can put 2 TextBlocks in vertical StackPanel
<StackPanel>
<TextBlock Text="{Binding title}" />
<TextBlock Text="{Binding priority, StringFormat='Priorytet: {0}'}" />
</StackPanel>
also instead of 2 TextBlocks for priority you can use one with StringFormat
The Environment.NewLine would only have the expected effect, if the texts are in the same TextBlock.
WPF then renders the Text property of the TextBlock with the new line in mind, which causes a line break.
To achieve the wanted behavior, you'd have add a vertical layout element:
<ListBox Name="lbxListaZadan">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel xml:space="preserve" Orientation="Vertical">
<TextBlock Text="{Binding title}" />
<StackPanel Orientation="horizontal">
<TextBlock Text="Priorytet: " />
<TextBlock Text="{Binding priority}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have this XAML code:
<ListView Name="ListBoxWithNews" ItemsSource="{Binding News}" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="2">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding imageURL}" Width="75" Height="75" />
<StackPanel>
<TextBox Text="{Binding Title}" Width="200" />
<TextBox Text="{Binding Body}" Width="200" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The controls are binded using the MVVM pattern. The user can change the content of the two text boxes. Is there a possible way to get the updated text from these text boxes at some point when I need them?
Your ViewModel should implement INotifyPropertyChanged and Use TwoWay Binding as below
<ListView Name="ListBoxWithNews" ItemsSource="{Binding News,Mode=TwoWay}" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Grid.ColumnSpan="2">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding imageURL,Mode=TwoWay}" Width="75" Height="75" />
<StackPanel>
<TextBox Text="{Binding Title,Mode=TwoWay}" Width="200" />
<TextBox Text="{Binding Body,Mode=TwoWay}" Width="200" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
How do I get the value of a TextBlock inside an WPF AutoCompleteBox container within a DataTemplate?
Below is my AutoCompleteBox XAML
<my:AutoCompleteBox Name="acLastName"
FilterMode="StartsWith"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
SelectionChanged='acLastName_SelectionChanged'
ValueMemberPath="LastName">
<my:AutoCompleteBox.ItemTemplate>
<DataTemplate x:Name='UserDetails'>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name='UserId' Text="{Binding UserDetailsId}"/>
<TextBlock Text="{Binding LastName}" />
<TextBlock Text="{Binding FirstName}" />
<TextBlock Text="{Binding UserId}" />
<TextBlock Text="{Binding Comapany}" />
</StackPanel>
</DataTemplate>
</my:AutoCompleteBox.ItemTemplate>
</my:AutoCompleteBox>
acLastName.SelectedItem will be your UserDetails object.
So cast that puppy up and access UserDetailsId through that:
((UserDetails)acLastName.SelectedItem).UserDetailsId
This seems basic, but I want to display a representation of some CLR objects that currently exist in a DataContext.
I've already set up DataTemplates for how I want them to look, I just want to plop them into the visual space.
I tried this, but it doesn't help:
<StackPanel>
<Binding Path="CalibrationA" />
<Binding Path="CalibrationB" />
</StackPanel>
The template, for reference (its for a sensor calibration):
<DataTemplate DataType="{x:Type ns:CalibrationTable}">
<StackPanel>
<TextBlock Text="{Binding TableName}" />
<ListBox ItemsSource="{Binding}" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding KeyName}" />
<TextBox Width="50"></TextBox>
<TextBlock Text="{Binding ValueName}" />
<TextBox Width="50"></TextBox>
<Button Content="Add" />
</StackPanel>
</StackPanel>
</DataTemplate>
Any suggestions?
The class you're looking for is ContentPresenter:
<StackPanel>
<ContentPresenter Content={Binding Foobar1} />
<ContentPresenter Content={Binding Foobar1} />
<StackPanel>