WPF ComboBox is not collapsing dropdown once it loses focus - wpf

I have a very strange situation. I have ComboBox which is loaded with some items. When I click on it, the drop-down expands showing all items.
If I now open another application such as Notepad, the dropdown will not collapse which I would expect to happen. As a result, any overlapping application, such as opened Notepad, will appear as being located in between the WPF hosting the ComboBox and the ComboBox drop-down like on screenshot below. Probably the z-order is somehow totally wrong.
I believe the reason for this behavior is the fact that the dropdown did not collapse at the first place. My ComboBox is part of a Usercontrol and its xaml is like:
<ComboBox ItemsSource="{Binding ValueSet}"
SelectedItem="{Binding SelectedNode}"
DisplayMemberPath="Name"
BorderBrush="Green"
BorderThickness="1" SelectionChanged="ComboBox_SelectionChanged">
</ComboBox>

In my opinion there is no any problem with the z-order in your xaml. The actual problem is in the ComboBox template in your application. You should find the actual style(or control template) your Combo is based on and check the Popup(usually named "PART_Popup") control inside that control template. The IsOpen property of the Popup should be bound to the ComboBox IsDropDownOpened property, like that:
`IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"`
Any way you should search the solution for your problem in incorrect control template of the Combo control. There is something that make it be opened(probably by making the IsOpen property of the inner Popup control to be always true). Let me know if it you need more suggestion.

Related

Combobox Text property not changing in user control

I have a list box that has a list of user controls. Each user control has 5 combo boxes. I want to be able to read the selected text of each combo box in each user control from the main application. However, when I change the selection in the combo box, the text property of the combo box in the user control doesn't change when I read it in the main application.
Code-behind:
radQueryParamList.Items.Add(new TCardQueryParameters());
Xaml (This is just a data template for how to display a TCardQueryParameters object):
<DataTemplate x:Key="TCardViewQueryParamDataTemplate">
<tcardqueryparam:TCardQueryParameters x:Name="TCardViewerParamUC" />
</DataTemplate>
<telerik:RadListBox Grid.Column="1" ItemTemplate="{StaticResource TCardViewQueryParamDataTemplate}" Name="radQueryParamList" VerticalAlignment="Top" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3">
Where I loop over the list of user controls
string test = radTESTGACC.Text;//TEST combo box, Text property changes
//radQueryParamList is a listbox of user controls where TCardQueryParameters is the UC
foreach(TCardQueryParameters param in radQueryParamList.Items)
{
//Each UC has a radGACC combo box in it, and I am reading what the user
//selected for each user control here in the main app, but the text property
//never changes
String gacc = param.radGACC.Text; //Text property DOESN'T CHANGE
}
I thought that each instance of the user control would keep its own state and I would just be able to read what the user selected for that combo box, but that doesn't seem to be the case.
You have not bound the SelectedItem, SelectedValue, or SelectedIndex property of your internal ComboBox to anything so it maintains its selection.
An ItemTemplate is like a cookie cutter. It contains the definition of the object, but not the object itself. Properties specific to the object's state are lost unless they are bound to something on the DataContext behind the template.
This is important to note for two aspects.
First off, to improve performance WPF usually unloads items which are not visible, which often results in items being re-created from their template anytime they are reloaded. An example would be when you minimize an application to the taskbar, then maximize it again. This is usually better on performance and memory usage, however it does mean you have to be sure you store the state of items that were created with a Template somewhere.
And second, by default ListBoxes use something called virtualization. A simple way of explaining this would be this:
Suppose you have a ListBox of 100,000 items. In your ListBox, only 10 items can be visible at a time. WPF will render roughly 14 items (the 10 visible ones, and then a few extra for a scroll buffer so you don't see anything unusual while scrolling). When you scroll to new items, WPF just re-uses the existing items that are already rendered, and just replaces the DataContext behind those items.
As you can guess, it is far better on performance to render 14 UI items instead of 100k items.
So to answer your question, you will probably want to bind either SelectedItem, SelectedValue, or SelectedIndex of your TCardQueryParameters UserControl to a property on the DataContext (which in your case appears to be another different UserControl).
It should probably be noted that what you are essentially doing is creating a list of UserControls, assigning them to the ListBox, and then telling the ListBox that it should draw each UserControl with another separate UserControl. So although you are changing the SelectedItem in the template UserControl, that change is not being reflected to your ListBox.Items copy of the UserControl.
I'm guessing you probably don't want that, so would recommend removing your ItemTemplate completely.
Or better yet, create a new class object containing all the data for your UserControl, and adding that to your ListBox.Items. Then tell the ListBox to draw that item using a TCardQueryParameters UserControl as the ItemTemplate, like you have now.

WPF bind a control visibility to the focused property of another control

I have a combobox that displays a list of items, and I want to place a button next to it which triggers a command to see the details of the selected item. So far, so good. Now I want the button to be visible only if the combobox has focus (or is in "edit" mode, but not only when the popup is open).
I thought I could bind the visibility of the button to some focus property of the combobox, something like this:
<Button Content="Details" Visibility="{Binding ElementName=elementListComboBox,
Path=IsFocused, Converter={StaticResource Bool2VisibilityConverter}}"/>
But I found no way to know if the control I want is focused or not. I looked at the FocusManager.FocusedElement, but I don't know how to get the focused control I want inside the binding. Is there a way to achieve this in XAML?
Ok, the way to get this working as I wanted is this:
<Button Command="{Binding SomeCommand}"
Content="Details"
Focusable="False"
Visibility="{Binding ElementName=elementListComboBox,
Path=IsKeyboardFocusWithin,
Converter={StaticResource Bool2VisibilityConverter}}"/>
Two key factors here: bind the button's visibility to IsKeyboardFocusWithin property of the combobox, and set the button's Focusable property to false, else it will get collapsed when you want to click on it.
Hope this is useful.

How to make WPF DataGrid VerticalScrollBar Always Visible?

Do you knows how to make the WPF DataGrid VerticalScrollBar always visible, even if there is no data to display without putting DataGrid object instid a ScrollViewer?
You should be able to set the ScrollViewer.VerticalScrollBarVisibility attached property like so:
<DataGrid ScrollViewer.VerticalScrollBarVisibility="Visible" ...

Using expression blend to define the template of an item in a listbox. What am I doing wrong?

I am new to expression blend and defining some templates to my items in a WP7 page.
I have a listbox and I want to use EB to define the template of my items, the value (the text that should go in the listboxitem) is stored in a ObservableCollection which is itemsource of my listbox.
I opened my project in expression blend. I opened the page, selected the listbox and right-clicked, then I chose ItemTemplate (in another try I chose ItemContainerStyle). a new page appeared and I added a textbox to my template.
When I ran the project, in both cases (using ItemTemplate and ItemContainerStyle), I got a list with "TextBox" written, which is logic because of textbox I added to the template.
I imagine I have to change the TextBox's Text property to define a binding. But how can I define that the value used is stored in the Listbox.ItemSource property?
Thanks!
Just found it!
<TextBlock TextWrapping="Wrap" Text="{Binding}" TextAlignment="Center" FontWeight="Bold"/>
Text={Binding} solved my problem :)

Set WPF TabControl to show the same Content for every tab

I'd like to use a TabControl where each tab shows the same view just with different parameters. Therefore I do not want to create a new content control for each tab but reuse the same control for all tabs (binding some properties of it to the SelectedItem property of the TabControl)
I tried to my contained control as resource and set the Content property of the tab items to it, but this resulted in an exception, because the same element cannot appear as content in to different parents.
Any ideas?
<TabControl>
<TabControl.ContentTemplate>
<DataTemplate>
your view
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
I found a solution in this question
TabControl's TabItems sharing same content... Do Not Want
(even if the poster wanted specifically the opposite behaviour :) ...)

Resources