ListBox instead of ItemsPresenter in WPF Custom Control? - wpf

I'm writing a generic control template for my WPF Custom Control.
But with ItemsPresenter I only got raw list of Data..
Compared to the ListBox, the ListBox has all features I need.
Is it wrong to use a ListBox instead of ItemsPresenter ?
What I'm after is that
if I write a generic Template that uses a ListBox and in code behind I register some ListBox specific events and somebody overrides my generic Template with his own ControlTemplate witn an ItemsControl inside that does not possess that event, it will raise an Exception. In case of ItemsPresenter, everyone could use what he wants to.
Thanks.

I think you could add some test to see if the ItemsControl in the template is a ListBox or not. For example:
var itemsControl = this.Template.FindName("PART_Items", this);
if(itemsControl is ListBox)
{
// wire additional event handler here
}

Related

Issue : ListBox hides all the stack panel and text boxes with in them, Wpf

I want to bind my textboxes in wpf xaml. That is doing perfectly but when i get textbox with his x:Name="myTextbox" in codebehind. He says
"The name 'myTextbox' does not exist in the current context"
Using WPF, you cannot access items by name if there are inside a DataTemplate, like in your case, probably. However is not a good practice to modify the template using code behind. If you want to use correctly WPF, take a look at MVVM pattern.
In your case, the ListBox should be bound to an ObservableCollection and inside the template you can bind your textbox or whatever you have to the Item object.
Don't access the textbox like this : x:Name="myTextbox"
Instead access it like myTextbox.Text = "Hello World";.

WPF binding to a non dependency property on a control template

I'm creating a WPF custom control as an auto learning exercise. My control has a ListView inside the template. I wanto my control user be able on defining the needed columns in his own Xaml, but I did not get the strategy on how to pass the columns to the inner listview since binding with FindAncestor complain that "Columns" is not a DependencyProperty.
Wekk the questions are:
How to achieve bind a property from xaml to the template when it is not a DP
Correct my design: I think there is something wrong: if someone would change completely my template, how should I let him use the Column collection ?
why not inherit from ListView directly? Then you have all the properties you need for the ListView and can also add you own properties to the class.
Then you can apply a custom Style to your control to make it look like you want. (Here you have a basic ListView Style that you can use and expand to your needs)
Sometimes binding to a property that is not a dependency property can be solved using the Binding Mode OneWayToSource
Have you tried that?

Silverlight Listbox selectable ItemTemplate

I have a Usercontrol that contains a ListBox (lstClients) and a ComboBox
The ListBox has 2 DataTemplates setup as Resources called "LowDetailTemplate" and "HighDetailTemplate"
I need to be able to switch between the 2 DataTemplates when I change the value in a ComboBox from "Low" to "High" and vice versa. In the SelectionChanged event of the ComboBox I'm guessing I need to change the ItemTemplate of the ListBox but I'm struggling with the code to assign the DataTemplate in code behind. My latest attempt is shown below but it fails at runtime.
lstClients.ItemTemplate = (DataTemplate)this.Resources["LowDetailTemplate"];
It would be easier to define both of the views inside the same data template and then switch which is visible by use of a simple variable. Then your combo box change would just update that simple variable and so cause the view shown for each template instance to change.

WPF - databinding label on listbox ismouseover

I'm still new to WPF, and I'm trying to do something that's beyond my knowledge at the moment.
I have a listbox databinded to the source collection, and a label. I'd like to bind the label's Content value to the listbox's item over which is mouse hovered.
Say I have DataTemplate binded to the class MenuItem:
<DataTemplate DataType="{x:Type local:MenuItem}" x:Key="MenuListTemplate">
Which has member Text. I want my Label to display Text from element which is mouse overed in list. I have the IsMouseOver trigger for my textbox, but have no idea how to bind Label.Content to it.
Any tips?
I don't think that binding can achieve your goal with ease. I think it's easier to do with routed events.
Subscribe to the MouseMove event at the ListBox level. Check if the source of the event is a ListBoxItem, and if it is use this item to update the label.

WPF HierarchicalDataTemplate

Could anyone explain how HierarchicalDataTemplate works
What Controls supports HierarchicalDataTemplate?
What does a control need to support HierarchicalDataTemplate?
UPDATE
What causes the TreeView to render
the parent and child nodes when the
same HierarchicalDataTemplate in a
HeaderedItemsControl only causes the
parent to be rendered?
What Controls supports HierarchicalDataTemplate?
All controls that inherit HeaderedItemsControl, such as TreeViewItem or MenuItem
What does a control need to support HierarchicalDataTemplate?
Inheriting from HeaderedItemsControl should be enough
Such control needs to be of type HeaderedItemsControl or derived from it. The current framework controls that do are MenuItem, ToolBar and TreeViewItem.
The HeaderedItemsControl overrides the PrepareContainerForItemOverride method and somewhere along that call path checks for HierarchicalDataTemplate.

Resources