New line in ListBox with bound data - c#

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>

Related

TreeView subcollection autosort when properties changes

I would like to sort the second level of my TreeView. The sorting depends on two different conditions depending on the FieldType and the Order properties. In my case it is possible that the order increases or decreases by the user. The user uses arrow up and down buttons. How can I keep the list sortet if an item is added or removed and the user changes the Order property?
My current TreeView:
<TreeView Grid.Row="0" x:Name="tvImages" ItemsSource="{Binding SelectedTemplate.Images}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type entities:TemplateImageDTO}" ItemsSource="{Binding FieldSections}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Order, StringFormat=Image {0}}" Margin="0,3,0,0"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type entities:FieldSectionDTO}">
<DockPanel LastChildFill="False" HorizontalAlignment="Stretch" Width="Auto">
<StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
<TextBlock Text="{Binding FieldType, Converter={StaticResource enumConverter}}" Margin="0,3,0,0" />
<TextBlock Text=" - Field " Margin="0,3,0,0" />
<TextBlock Text="{Binding Order, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,3,0,0" />
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Right" Margin="0,0,10,0">
<Button Width="24" Height="24" Style="{DynamicResource MahApps.Metro.Styles.MetroCircleButtonStyle}">
<iconPacks:PackIconFontAwesome Kind="AngleUpSolid" Foreground="{DynamicResource AccentColorBrush}" />
</Button>
<Button Width="24" Height="24" Margin="10,0,0,0" Style="{DynamicResource MahApps.Metro.Styles.MetroCircleButtonStyle}">
<iconPacks:PackIconFontAwesome Kind="AngleDownSolid" Foreground="{DynamicResource AccentColorBrush}" />
</Button>
</StackPanel>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
I found my Solution here: https://stackoverflow.com/a/19363888/6751312
After the user clicked the arrow up and down button, I increase and decrease the order property and resort the list.

WPF ListView deselect item

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>

MVVM ItemTemplate with custom control

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.

Getting value of TextBlock inside AutoCompleteBox DataTemplate

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

WPF: Displaying templated objects

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>

Categories

Resources