Getting value of TextBlock inside AutoCompleteBox DataTemplate - c#

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

Related

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.

Listview Checkbox.IsEnabled bind to Parent Checkbox.IsChecked

I try to bind the IsEnabled Property of a Checkbox to IsChecked of another Checkbox in a Listview. What is wrong?
<TreeView ItemsSource="{Binding Rulesets}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Rules}">
<StackPanel Orientation="Horizontal">
<CheckBox Name="rulesetCheckbox" IsChecked="{Binding IsActivated}"></CheckBox>
<TextBlock Text="{Binding Name}" />
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsEnabled="{Binding ElementName=rulesetCheckbox, Path=IsChecked}" IsChecked="{Binding IsActivated}"></CheckBox>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Thanks!

New line in ListBox with bound data

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>

Binding Selected TreeNode MVVM

all night racking my head - I can not figure out how to bind a selected segment TreeView variable Mvvm
<TreeView ItemsSource="{Binding Subunits}">
<!-- Шаблон подразделения -->
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}">
<TextBlock Text="{Binding SubunitName}"/>
<!-- Шаблон должности -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}" DataType="{x:Type local:Employee}">
<TextBlock Text="{Binding PositionName}"/>
<!-- Шаблон сотрудника -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" Margin="0,0,5,0" MouseDown="qwe"/>
<TextBlock Text="{Binding FirstName}" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Patronymic}" Margin="0,0,5,0"/>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
How can i do it?
sorry for my english :(

Passing data from selected item to viewmodel in WinRT

I am currently writing an application in WinRT where i need to pass an id from a selected item in a listview back to my viewmodel. My listview has an Observable collection as an itemsource so there will be a different id for each item in the listview.
my Xaml code look similar to this
<ListView Grid.Column="0" ItemsSource="{Binding VacationOverviewDisplay}" >
<WinRtBehaviors:Interaction.Behaviors>
<Win8nl_Behavior:EventToCommandBehavior Event="SelectionChanged"
Command="DetailsCommand"
CommandParameter="{Binding Path=DontKnow, Mode=TwoWay}"/>
</WinRtBehaviors:Interaction.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" FontWeight="Bold" FontFamily="Segoe UI" Text="{Binding VacationStart, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd MMM yyyy}' }" Margin="20,0,0,0"></TextBlock>
<TextBlock VerticalAlignment="Center" FontWeight="Bold" FontFamily="Segoe UI" Text="{Binding VacationEnd, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd MMM yyyy}' }" Margin="20,0,0,0"></TextBlock>
<TextBlock x:Name="id" VerticalAlignment="Center" FontWeight="Bold" FontFamily="Segoe UI" Text="{Binding VacationRequestId}" Margin="20,0,0,0"></TextBlock>
</StackPanel>
<TextBlock Text="{Binding StatusView}" Margin="50,0,0,0"></TextBlock>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Title: " Margin="50,0,0,0"></TextBlock>
<TextBlock FontStyle="Italic" Text="{Binding VacationCommentUser}" Margin="5,0,0,0"></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I am using the WinRTBehaviors to mimic the EventToCommand behavior but i have no idea how i should get a certain parameter from an item in my listview back to my viewmodel. for the Mvvm i am using MvvmLight.
Just create a property in a view model then bind it to SelectedItem in your list view like this:
SelectedItem={Binding MyProperty, Mode=TwoWay}
That's all. Whenever a user changed a value, your property will be updated.
Bind Selected Item To a Property in your ViewModel
<ListView Grid.Column="0" ItemsSource="{Binding VacationOverviewDisplay}" SelectedItem="{Binding SelectedVacation, Mode=TwoWay}">
You should use SelectedValuePath for extracting Id from SelectedItem:
<ListView Grid.Column="0" ItemsSource="{Binding VacationOverviewDisplay}"
SlectedValuePath="Id"
SelectedValue="{Binding SelectedVacationId, Mode=TwoWay}">
CommandParameter="{Binding ElementName=MyListBox, Path=SelectedItem}"
(You'll have to give your ListBox an x:Name value.)

Categories

Resources