Combobox DropDownOpened event - c#

I have a combo box inside a list view. And different items (rows) in the list view need to display (based on some condition) one among 3 sets of combo box items in the respective combo box which is . Right now I am achieving this by using 3 data templates bound to 3 different properties of List<string>. I am hooking up the appropriate data templates during the DropDownOpened event of the combo boxes.
The problem that I am facing is that I am having to click the combo box twice each time to have it dropped down. I did a Debug.WriteLine() to see if my first click does the job of selecting a data template, I found that it indeed does. But just that it does not drop down at the first click itself.
I guess this is because I am changing the data template after the combo box has dropped down its popup and hence it refreshes again whose results are furnished only after I perform the second click.
My question is whether there is any other way of accomplishing what I am trying or do I have to make any changes in my existing DropDownOpened event handler.
Please suggest.
EDIT : I'm using WPF
Thanks

Would it be an option to use styles to set the items? e.g.
<ComboBox>
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding SomeConditionalProperty}"
Value="SomeConditionalValue">
<Setter Property="ItemsSource" Value="{BindingToFindRightItems}"/>
</DataTrigger>
<DataTrigger Binding="{Binding SomeConditionalProperty}"
Value="AnotherValue">
<Setter Property="ItemsSource" Value="{BindingToFindRightItemsForAnotherValue}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>

Related

MVVM - Hide Datagrid column based on column name with autogeneratecolumns = True

I have a DataGrid that's bound to Datatable, and I want to uniquely identify rows in the DataTable using the ID, but I don't want it to be shown in the DataGrid
What I reached so far by searching and excluding:
Data columns are not predefined, so, I have to use AutoGenerateColumns=True, hence, I can't define the columns manually and set the Visibility property to False.
I can't use List or ObservableCollection to define private ID member, because the data are dynamic.
I am following MVVM so, I can't use AutoGeneratingColumns event handler directly and can't expose the View to the ViewModel.
The closest I get to an answer is using DataTrigger to set Visibility to False using CellStyle, but it just hid the cells, not the entire column, and I also tried it for DataGridColumnHeader and it didn't work:
code:
<Style x:Key="ColumnStyle" TargetType="DataGridColumnHeader">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="id">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
How to do it while maintaining the previous conditions?
Thanks in advance
EDIT:
I fixed the code for DataGridColumnHeader using Path=Column.Header which doesn't make sense to me but it's irrelevant; Still, there and empty column standing there, with no idea how to remove it.
It sounds like you want to track the selected Item. If you want to track the "selected Element", you have to use a CollectionView.
WPF controls do not direcly bind to collections. They bind to a CollectionView. And if you do not give them one, they will create one themself from whatever collection you hand them. If you want sorting, filtering, ordering and selection tracking, CollectionView is the droid you are looking for:
https://msdn.microsoft.com/en-us/library/system.windows.data.collectionview.aspx
Just take control of it's creation and expose it (rather then the raw collection).
I found a solution by applying this style:
<Style x:Key="ColumnStyle" TargetType="DataGridColumnHeader">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="id">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
to DataGridCell and to DataGridColumnHeader and allocating the column in the end of the table this removed the empty column from the middle of the table.

Focus on first element in listbox

I am facing the following issue. When i am removing the first element from a list box which has keyboard focus(by refreshing the item source) the keyboard focus is moving to the parent window. I want to retain the keyboard focus on the fist listbox item. So i came up with the following code
<Grid.Resources>
<Style TargetType="{x:Type ListBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding FirstRowDeleted}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=MyListBox}" />
</DataTrigger>
</Style.Triggers>
</Style>
Using this code i am able to move the focus to the list box itself. How can i move the focus to the first listbox item? MyListBox[0] does not seem to work. Any suggestions?
Thanking you
Kaddy
I found answer here: How do you programmatically set focus to the SelectedItem in a WPF ListBox that already has focus?
var listBoxItem = (ListBoxItem) MainListBox
.ItemContainerGenerator
.ContainerFromItem(MainListBox.SelectedItem);
listBoxItem.Focus();
Thanks for #Jeff for the answer.
This code applies the focus function to the first input text when the page loads. try it out
$(document).ready(function () {
$("#input:text:visible:first").focus();
});

How do I set the text a ComboBox displays when no item is selected (selected index is -1)? [duplicate]

This question already has answers here:
How to display default text "--Select Team --" in combo box on pageload in WPF?
(24 answers)
Closed 7 years ago.
I have a ComboBox that is data bound where I cannot insert sentinel items. I would like to show a default string if there is no selected value in the ComboBox.
I already tried using a style for this:
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelectedIndex}" Value="-1">
<Setter Property="Text" Value="(unconstrained)" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
but this completely broke data binding (no values were selectable).
You could try this (not really fortunate) solution:
You could add another element to your list with value "(unconstrained)" at the first position and set SelectedIndex to 0 in your XAML. Then, when selection changes, you delete this element from the underlying list for ComboBox and move selected index one place up (since the head of the list has been deleted).
This is not elegant at all, but seems like the only solution since ComboBox does not have some kind of Header property and I am not sure what Text does on it (probably it is just inherited).

Call an event based on property value in WPF

I have a DataGrid and a button. I want to enable the button only when an entire row is selected. Otherwise it should be disabled.
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
//Here I want to enable the button which is outside the grid.
</Trigger>
</Style.Triggers>
</Style>
How can I do it?
I am not sure whether using a style trigger is a correct idea to solve your problem.
This issue can be solved using a delegate-event concept in your view model.
Create a property in your datagrid item source whose change raises an event. The handler of the event will in turn would set the button IsEnable property to true/false.
I have created a test application that would help you understand what I want to say.
Download it from : https://www.dropbox.com/s/hxzmpslt1bs0sei/WpfApplication2.rar?dl=0
try this and tell me if it did what you desired.

Select Multiple items in listbox in wpf using c#

I want to set multiple selection in a ListBox using c#.
For example, I have a list of values I want to set these values as selected in the ListBox.
How can I do this?
MyListBox.SelectedItems.Add(item1);
MyListBox.SelectedItems.Add(item2);
.....
You did not explain much, hopefully your are doing this the WPF way...
Create an IsSelected property on your data items then give the style to your ListBoxItems that selects them whenever the IsSelected is enabled:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
Then change the property on your data items, and raise the OnPropertyChanged event.

Categories

Resources