ContentPresenter in ItemControl.ItemTemplate to show displaymemberpath of itemscontrol - c#

I want to know is there anyway to put contentpresenter in itemtemplate of an itemscontrol to display my data. I don't want hard code binding like Text="{Binding username}" cause I am building a custom control, I think ContentPresenter is what I want. But after I tried using contentpresenter, it give me stackoverflowexception.
<ItemsControl ItemsSource="{Binding SelectedItems, ElementName=listbox}" DisplayMemberPath={Binding DisplayMemberPath}">
<ItemsControl.ItemPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="Separator" Text=", "/>
<ContentPresenter/>
<!--<TextBlock Text="{Binding username}"/>-->
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
That's my code.
If without those seperator and itemtemplate, I able to display my data by just using the displaymemberpath, but it stack all the name together. I still finding any solution to solve it. I hope you can provide some ideas to do this.

The answer is no, you can't. A ContentPresenter is supposed to be used in a ControlTemplate, not a DataTemplate, so it is not the right control to use. From the linked page on MSDN:
You typically use the ContentPresenter in the ControlTemplate of a ContentControl to specify where the content is to be added.
What you can do alternatively, is to declare a number of DataTemplates in a Resources section (complete with Binding Paths) for different types of data and omit the x:Key directives, eg. do not name them. Also, do not specify one for the ItemsControl.ItemTemplate.
When doing this, WPF will implicitly select the correct DataTemplate for the relevant data type and so you can have different outputs for different data types. See the The DataType Property section of the Data Templating Overview page on MSDN for further explanation of this technique.

Yes, and it works well. Outside of a ContentControl's template, you must bind the Content by hand:
<ContentPresenter Content="{Binding username}"/>
I do this a great deal and it never misbehaves. ContentPresenter seems to be implemented for general use. I wonder if the API docs overstate its relationship to ContentControl.

I found an easier way to solve this problem by using horizontal listbox. Thanks for responses

Related

How do I create a group of different UI elements in XAML based on datatypes of the bound values?

I have a list of parameters in an Observable Collection in my ViewModel that I want to be displayed as textboxes or comboboxes dependent on the datatype, but I'm struggling to find a way to implement any kind of conditional functionality in XAML.
So, say I have an observable collection of parameters (title, datatype, value):
Valid,Boolean,
Name,String,
Age,UInt,
I currently have these generating a sequence of textboxes in the window next to labels indicating the type and parameter name using:
<ItemsControl ItemsSource="{Binding Parameters}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Textbox Text="{Binding Value}"
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But I don't want this to be a textbox every time; I want to be able to have something like an "if" or a "switch" to make a combobox instead if the datatype facilitates pre-defined responses like True/False.
I've seen similar posts where the answers seemed to suggest using Triggers or DataTriggers to do this, which looks exactly like what I need. However, they always seem to put it in a Style, with Style.Triggers, rather than any of the elements I currently have. Any attempts to replicate what I've seen have failed due to slight differences in what we've used; The example will use something like ContentControl and bind the style with "Content = {…}" which I can't do because "Content" isn't something I can apply to "ItemControl", but I need ItemControl due to the way it implements ObservableCollection in a way Content control doesn't.
Is there a simple way to put "If datatype == Boolean, make a combobox here" sort of thing? If I can make this happen in the ViewModel, I'm open to that too. Don't want to be doing anything in the code-behind, as MVVM doesn't seem to play nice with it and has always bit me in the behind later down the road when I try to mix them and I can't get my values to interact properly.
Would very much appreciate any help.
I realise I could just have the user TYPE true/false and interpret it in the viewmodel as Boolean, but there's several reasons this won't really work in my case. For one thing, there will be many, many options.
Really think you should be using DataTemplates:
... xmlns:sys="clr-namespace:System;assembly=mscorlib"
<ItemsControl ItemsSource="{Binding Parameters}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type sys:Boolean}">
<Checkbox IsChecked="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type sys:String}">
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" />
<ContentPresenter Content="{Binding Value}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
You can use your own types as well that it will auto detect and use the datatype for.

WPF Issues with binding to styled button

I have created a RadioButton style which I use across my application. The display part of which uses the content presenter to display whatever content I added to the button:
<ContentPresenter>
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{TemplateBinding Content}" />
</Grid>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
I'm then attempting to bind a decimal with a string formatter to the styled button like so:
<RadioButton Content="{Binding Stake, StringFormat={}{0:C}}" Style="{DynamicResource NeutralSelectorButtonStyle}" />
Stake is a decimal within a ViewModel which is set as the DataContext. When I run this up the content coming through is blank.
I made a change using a label in the DataTemplate rather than a TextBlock, this displayed the decimal but had not formatted it.
Can anyone explain why this is happening and possibly provide a solution.
If you require any more information just ask :)
Thanks in advance.
You are almost there just instead of setting the string format inside binding you should use ContentStringFormat property when in ContentControls.
Take a look at this Label (it works with any content control):
<Label Content="{Binding Path=MaxLevelofInvestment}" ContentStringFormat="Amount is {0}"/>
ContentPresenter also has this property:
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter.contentstringformat%28v=vs.110%29.aspx
Try it out. I hope it works for you.

Exception on adding ContentPresenter to a ContentTemplate

<ContentControl Content="Test">
<ContentControl.ContentTemplate>
<DataTemplate>
<Border>
<ContentPresenter />
</Border>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
It throws stackoverflow exception. However if i use any other control else than ContentPresenter it works fine even ItemPresenter also works.I knows it doesnt make any sense to have ContentPresenter there but just for Knowledge want to know. Why it throws StackOverFlow exception and also Why does Intellisense shows it can be added(I mean it comes there in Intellisense that means syntatically its not wrong to have ContentPresenter there). Any help will be highly appericiated. Or is it any flaw in Wpf.
The MSDN page for ContentPresenter says:
When a ContentPresenter object is in a ControlTemplate of a
ContentControl, the Content, ContentTemplate, and
ContentTemplateSelector properties get their values from the
properties of the same names of the ContentControl. You can have the
ContentPresenter property get the values of these properties from
other properties of the templated parent by setting the ContentSource
property or binding to them.
I imagine it gives a StackOverflow because it just keeps on trying to apply the template to the ContentPresenter in the ContentTemplate, and then on the next one inside it, and then the one inside it, etc.
You also can do things like the following, so this pattern probably isn't always wrong, even though IntelliSense probably doesn't do any checking for these sorts of scenarios.
<ContentControl Content="Test">
<ContentControl.ContentTemplate>
<DataTemplate>
<Border>
<ContentPresenter ContentStringFormat="{}{0}" />
</Border>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>

Accessing a list picker inside a PanoramaHeader Template

I have a panorama control in which I have create a header template to add a list picker inside it. (Just like the peoples hub to select social accounts)
<DataTemplate x:Key="PanoramaItemHeaderTemplate">
<ContentPresenter>
<StackPanel Height="129">
<TextBlock Text="{Binding}" FontSize="72" Margin="0,7,0,0" />
<toolkit:ListPicker x:Name="listPick" Margin="0,-21,0,0" BorderThickness="0">
<toolkit:ListPickerItem Content="twitter"></toolkit:ListPickerItem>
</toolkit:ListPicker>
</StackPanel>
</ContentPresenter>
</DataTemplate>
The panorama control is inside the MainPage.xaml file and I want to have access to the listpicker from the code behind to be able to populate it, and handle it selection events.
I don't know how to do this. I tried adding the x:name property to the list picker I don't have access to it in the MainPage code behind.
Any idea on how to approach this is very welcomed, thanks!
From what you have now, the quickest way to do what you want is to traverse the visual tree
See here for the implementation:
How to access a specific item in a Listbox with DataTemplate?
You cannot access the ListPicker by x:Name because it is not unambiguous: there is a ListPicker generated for each PanoramaItem in your Panorama. So the first question is, is it really the think you want to do? If so you need to populate it using a binding (ItemSource)
You can access an element inside another resource, consider this example:
<Grid Name="myGrid">
<StackPanel x:Name="stack1">
<TextBlock x:Name="abc"/>
</StackPanel>
</Grid>
We can only access the Grid myGrid in code by default. To get reference to the StackPanel we can do this:
StackPanel myStack=myGrid.FindName("stack1") as Stackpanel;
After that we can get reference to the TextBlock:
TextBlock myTextBlock=myStack.FindName("abc") as TextBlock;
You can modify myTextBlock after that as you may like. You can apply the same technique in your case and it will work.
Hope that helps :).

TextBlock Text-Binding from ListView items

How can I change the Text of a TextBlock when the selection in my ListView changes?
I don't want do this manually...
All Items of the ListView are LogEntry's (class)... Can I use Binding in the Text-Attribute of the TextBlock to get a specific property of the selected Item?
Yes, in fact there are multiple solutions, i give you the most "WPF" like answer, but imo also the least flexible.
First you need to set the IsSynchronizedWithCurrentItem="True" property
Now if you select an item, the bound CollectionView will set the item as the CurrentItem.
Now your TextBox/Block can bind to this specific item via a special binding syntax using a '/'.
For Example:
<TextBlock Text="{Binding LogEntries/}"/>
of course you can get a specific property from the current item via binding aswell
<TextBlock Text="{Binding LogEntries/WarningMessage}"/>
Hope that helps.
assuming you have a listview like this:
<ListView ItemSource="{Binding LogEntries}" Name="logs" IsSynchronizedWithCurrentItem="True">
</ListView>
<ContentControl Content="{Binding ElementName=logs, Path=SelectedItem}" ContentTemplate="{StaticResource logTemplate}"/>
Now you need to provide that logTemplate in the Resources.
<UserControl.Resources>
<DataTemplate DataType="{x:Type local:LogEntry}">
<TextBlock Text="{Binding Path=LogText}"/> <-- This is a Property-Binding of your custom class
</DataTemplate>
</UserControl.Resources>
The last thing missing is to provide the namespace to your local class LogEntry. If you use an awesome tool like Resharper, it will insert the namespace for you. Otherwise, here a sample declaration:
<UserControl xmlns:local="clr-namespace:My.App.Namespace.LogEntry;assembly=My.App"
... (rest of namespace declarations)

Categories

Resources