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.)
Related
I want to have the ListItems to extend with their Stackpanel the full width of the Listbox.
Currently they are only as wide as the textblocks.
What do I have to do so that the Stackpanel of the ListBoxItems fill the width of the ListBox?
<ListBox Name="feedListBox" VerticalAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionMode="Single" SelectionChanged="feedListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontFamily="Segoe UI Symbol" Text="⛽" FontSize="25"/>
<StackPanel Orientation="Vertical">
<TextBlock Name="txtDate" Text="{Binding Date}" FontSize="15" Margin="20,0,0,0"/>
<TextBlock Name="txtDitance" Text="{Binding Distance}" FontSize="15" Margin="20,5,0,0"/>
</StackPanel>
<TextBlock Name="txtPrice" Text="{Binding Price}" FontSize="15" Margin="30,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
delete: VerticalAlignment="Top"
and use: HorizontalContentAlignment="Stretch"
My XAML Code is
<ComboBox x:Name="ComboBoxTemplateCategory" Grid.Column="3" Grid.Row="2" SelectedValuePath="Text" ItemsSource="{Binding}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="5,0,0,0" Text="{Binding Path=TemplateName}" Width="100"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
and
C# Code
string mySelectedValue = ((TextBlock)((ComboBoxItem)
ComboBoxTemplateCategory.SelectedItem).Content).Text;
not working..
string SelectedValue = ((BulkEmailNew.BulkEmailService.TemplateCategory)(ComboBoxTemplateCategory.SelectedItem)).TemplateName;
Like this
Basically, I want a list in which I can select the entries to display more details for that entry.How can I do that ??
You should make use of ListBox,
<ListBox Grid.Row="0" ItemsSource="{Binding Settings}"
SelectionMode="Single" SelectedItem="{Binding CurrentSelectedSetting, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SettingName}"></TextBlock>
<TextBlock Width="5"></TextBlock>
</StackPanel>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have a Listbox within my WP7 application, and inside the DataTemplate I have a toolkit:WrapPanel which then has a context menu (toolkit). How can I get the text of a textblock inside the listbox datatemplate when they click on the contextmenu menuitem?
Hope this makes sense heres some code:
<ListBox x:Name="FirstListBox" Margin="0,0,-12,0">
<ListBox.ItemTemplate>
<DataTemplate>
<toolkit:WrapPanel Margin="0,0,0,17" Width="432" Height="auto">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="copy" Click="copytoCB_Click"></toolkit:MenuItem>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<TextBlock Text="{Binding author}" TextAlignment="Left" TextWrapping="NoWrap" Style="{StaticResource PhoneTextExtraLargeStyle}" MinWidth="430" />
<TextBlock Text="{Binding content}" TextWrapping="Wrap" Name="txtContent" Style="{StaticResource PhoneTextSubtleStyle}"/>
</toolkit:WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Thanks,
Michael
Here you can see
WPF Commands
in action, maybe a good place to start. :)
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