I am confused with learning about WPF. I see ControlTemplate used to determine how each item in a listbox looks. Isn't that what the ItemTemplate is used for in listboxes? What is the difference?
A ControlTemplate let's you change the look&feel of existing controls, for example, make a normal everyday button look like Vista's taskbar shiny glass buttons or make a tab control look like the new Office 2007 ribbon.
The idea is that the control contains only the behavior and the ControlTemplate contains the look&feel - so a button is "something you click" not "a rectangle with some 3d effect you can click on".
You can replace the ListBox's control template to get something like a list box with rounded corners or custom scroll bars.
A DataTemplate (ItemTemplate is a property of type DataTemplate) on the other hand let's you control how the data rows displayed inside list boxes (and other similar controls), data templates are used to specify things like "Show last name, than a comma and the first name - and the last name should be bold and the first name should be green if it starts with an A".
And just for completeness there is also an ItemsPanelTemplate class that is used to change the internal layout of a list box - Charles Petzold's WPF book has an example of using it to make a normal list box display it's items in a circle.
An ItemTemplate describes how to render the data item in a listbox (for example, you might have your listbox bound to a List - the ItemTemplate will allow you to specify how to render a Foo). An ItemTemplate is a DataTemplate.
A ControlTemplate describes how the ListBox itself should be rendered
Related
I'm following this reference to implement an autocomplete textbox in my application:
http://www.broculos.net/2014/04/wpf-autocompletebox-autocomplete-text.html
I notice there is a difference when using an itemtemplate vs not using one. When using a template the items have more space between them. I need to use the itemtemplate but how can I make it look more like when not using a template, move the items closer to each other?
When not using an ItemTemplate, the single items in the dropdown list will be simple strings and will be represented by TextBlocks in the UI. TextBlocks don't have any margin by default, so the lines are close together.
Assigning an ItemTemplate will replace this visual appearance (with whatever the ItemTemplate defines). Most probably there is some margin/padding defined in the ItemTemplate, e.g. if an Label is used as representation (Labels have some default margin assigned).
Changing the margin/padding properties of the single elements of your ItemTemplate should help here.
I have to develop a control having one datagrid. now datagridtextcolumn have to be binded with one property of collection created in viewmodel.
based on data i have do one thing...
if for a row no of lines is more than 3 in last column's cell then show a button having text "More" with last cell else with text "Less" and on click of "More" button it should display full text data...
now this layout can be shrined as well as stretched at runtime according to window's height and width using viewbox control or any other way...
so how to achieve this thing any idea???
Basically you want a custom textblock control that displays a max of 3 lines and shows a more button (if there are more than 3 lines of text)*.
The more button expands the textblock and I imagine stays around to collapse it again?
As it sounds like you only have one column (more like a list), rather than customise the datagrid create a usercontrol (with a grid, textblock and button) that does what you want and bind that inside a templated column instead.
You will need to expose the Text property as a dependency property for it to bind to. Most of the details of creating this type of usercontrol are on my answer to this: putting Button inside a TextBlock(or maybe something else) to make it work like the one on video?
Just add your size logic and change the layout to suit your needs.
*Note: Personally I would use the MinHeight property of the usercontrol to set when the "More" button is needed, then it will be more flexible, but that is just a suggestion.
I was wondering if I can have a ComboxBox with a Treeview as the selector rather than a list?
I note that there is a ComboBox.ItemsPanelTemplate, but I don't know what I need to do to get this to work?
I could build a control 'from the ground-up' with a separate TextBox, Button and TreeView in a PopUp control, but wondered if there is an easier way.
Lee
Are you just wanting the combobox for presentation purposes regarding the treeview?
Why not use a treeview as is and just trigger the display via visibility or animation from a button or control of some sort?
I am working on similar theme as we speak. I am using the Expander control from the Silverlight Toolkit with a TreeView control inside it. When an item is selected in the TreeView, it sets the Header of the Expander and then collapses the Expander.
Initial tests indicate it's OK but as I say, just started with it myself. Will let you know if I hit any stumbling blocks.
I have a list of textual descriptions, each of which a user must score on a scale of 1-5. The number of textual descriptions is variable, so I can't just define a static Grid in XAML.
The following image shows approximately what I'm after:
In ASP.NET, I could bind my list of text items to a Repeater control. For each row, the text would be displayed in a Label, and next to it would be a DropDownList that contains a static list of items. Is there a similar control available in Silverlight?
Should I be using one of these?
DataGrid
ListBox
Custom control derived from ItemsControl
(Other)
I solved this by using an ItemsControl, and within the ItemTemplate I placed the TextBlock and ComboBox controls.
The only wrinkle for me was that the ComboBox depended on a separate DataContext, so I had to resort to using the DataContextProxy described in this article
I have to change Listbox's control templated in a way that it can look like below image
alt text http://www.freeimagehosting.net/uploads/05598e4d35.png
i have added togglebutton in listboxdatatemplate...
have one stackpanel in listbox style template...
so structure is like
border - stackpanel - grid - itemcontainer[boder-togglerbutton]
now to make first and last item curved... what are the ways...
if i make stackpanel border curved... when firstitem is selected and its background is changed... it shows a square inside outer border... so i am not able to get look showed below...
any idea?? how to get design done in the way with listbox tht can be showed like image above...
in image 1,3 and 4 items are selected and others are in normal mode...
-thanks in advance
I did it with a listbox in Blend inside of the ItemTemplate by creating a leftBorder and a right Border that are in the same space. The leftBorder is defined with cornerRadius 10,0,0,10 and the right with cornerRadius 0,10,10,0. I then set them both to opacity 0 (you can use visibility if you prefer) and set a ChangePropertyAction on both of thier loaded events conditional on the first and last item in your list.
That is the bad part, I did need to do it conditional on the items in your list but you could define another property and set that to first and last or create multiple templates that you set if there are no more items in the list from the control (not in the template).
If you think this may be interesting to you, let me know and I will post on my blog as well as try to make a couple of adjustments for your situation. I am not sure how to upload a project to here.