WPF use static resource in DataTemplate - c#

I have a user control defines in my Application.Resources and want to use it as a DataTemplate for a ComboBox.
The user control:
<TextBlock
x:Key="ListItemView"
Text="{Binding Name}"
ToolTip="{Binding ToolTip}"/>
The Combox Box in my Window:
<ComboBox
ItemsSource="{Binding ComboBoxItems}"
SelectedItem="{Binding SelectedComboBoxItem}">
<ComboBox.ItemTemplate>
<DataTemplate>
<!-- TODO how to use StaticResource ListItemView in here? -->
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

You must put your user control into DataTemplate
<DataTemplate x:Key="ListItemView">
<TextBlock Text="{Binding Name}"
ToolTip="{Binding ToolTip}"/>
</DataTemplate>
Use your data template
<ComboBox ItemsSource="{Binding ComboBoxItems}"
SelectedItem="{Binding SelectedComboBoxItem}"
ItemTemplate="{StaticResource ListItemView}">
</ComboBox>

Related

Using ReactiveUI to bind to WPF TreeView

I am trying to bind to a TreeView with ReactiveUI in WPF, but nothing is being displayed. Can anyone tell me where I am going wrong, or give me some advice on how to debug it?
Binding in View constructor:
this.OneWayBind(ViewModel,
vm => vm.NonTableItemCommentsViewModels,
view => view.CommentTree.ItemsSource);
Xaml:
<TreeView Name="CommentTree" ItemsSource="{Binding}">
<TreeView.Resources>
<!-- Template for Items -->
<HierarchicalDataTemplate DataType="{x:Type viewModels:NonTableItemCommentsViewModel}"
ItemsSource="{Binding Path=Comments, Mode=OneWay}">
<TextBlock Text="{Binding Path=ItemId, Mode=OneWay}" />
</HierarchicalDataTemplate>
<!-- Template for Comments -->
<DataTemplate DataType="{x:Type viewModels:CommentViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Text, Mode=OneWay}"
Margin="3,0" />
<TextBlock Text="{Binding Path=Author, Mode=OneWay}"
Margin="3,0" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Items and Comments are both ObservableCollections.

WPF ComboBox in grid - how to specify search path in viewmodel

We have a DataGridTemplateColumn that uses a ComboBox in the DataTemplate:
<DataGridTemplateColumn Header="Things">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
IsTextSearchEnabled="True"
TextSearch.TextPath="Name"
ItemsSource="{Binding Things}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Index}" Margin="0,0,12,0" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
We need to let the user specify the search path for the ComboBox.
TextSearch.TextPath="{Binding SearchPath}"
How do I specify that this binding refers to the top-level viewmodel, not the current item from the Things collection (which Name and Index refer to)?
Thanks for any insights --

How to get selected value of ComboBox in Silverlight if Value in DataTemplate using C#?

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;

How todo nested Databinding with Windows Phone (Pivot and ListBox)

I am pretty new in the usage of the DataBinding concept in Windows Phone. I am trying to bind a Pivot page with a list of items, as well as a ListBox in each PivotItem with another list (of criterias). I have a Page with the following XAML:
<phone:Pivot Name="ItemsPivot" Title="MY APPLICATION">
<phone:Pivot.ItemTemplate>
<DataTemplate>
<StackPanel Margin="12,17,0,28">
<ListBox Name="CriteriasListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<TextBlock Text="{Binding Name}"/>
<tk:Rating />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
I then try to specify the ItemSource of each binded control in the C# code like this:
...
ItemsPivot.ItemsSource = ItemList;
CriteriasListBox.ItemsSource = CriteriaList; // <-- CriteriasListBox not accessible !!
...
But there is an error stating that "the name 'CriteriasListBox' does not exist in the current context"... As I mention, I am pretty new with this technology.
Can I have some advice, solution or resources on how do make this nested binding work?
Thank you !
Do like this,
<UserControl.Resources>
<DataTemplate x:Key="MyPivotItemTemplate">
<controls:PivotItem Header="first" >
<ListBox x:Name="CriteriasListBox" Margin="0,0,12,0" ItemsSource="{Binding CriteriaList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
</DataTemplate>
</UserControl.Resources>
Then in your pivot
<Pivot ItemsTemplate="{StaticResource MyPivotItemTemplate}" Items="{Binding ItemList}"
One of the ways if you have different criteria list for different items is to add criteria list property to your PivotItemViewModel. Your c# code will look like this
ItemsPivot.ItemsSource = ItemList;
where every item in ItemList contains CriteriaList. Your xaml code will look like
<phone:Pivot Name="ItemsPivot" Title="MY APPLICATION">
<phone:Pivot.ItemTemplate>
<DataTemplate>
<StackPanel Margin="12,17,0,28">
<TextBlock Name="PivotTextBlock" Text="{Binding Name}"/>
<ListBox ItemsSource="{Binding Criterions}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<TextBlock Name="ListTextBlock" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
Binding for PivotTextBlock retrieves Name property from the PivotItemViewModel. Binding for ListTextBlock retrieves Name property from Criteria.
If criteria list the only in your application here is a good start
Here is another good way I found...
http://www.codeproject.com/Articles/47229/A-LINQ-Tutorial-WPF-Data-Binding-with-LINQ-to-SQL
Here is a code snippet:
<DataTemplate DataType="{x:Type LINQDemo:Category}">
<Border Name="border" BorderBrush="ForestGreen"
BorderThickness="1" Padding="5" Margin="5">
<StackPanel>
<TextBlock Text="{Binding Path=Name}"
FontWeight="Bold" FontSize="14"/>
<ListView ItemsSource="{Binding Path=Books}"
HorizontalContentAlignment="Stretch" BorderThickness="0" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink Click="LoadIndividualBook"
CommandParameter="{Binding}"
ToolTip="Display book details">
<TextBlock Text="{Binding Path=Title}"/></Hyperlink>
...

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