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();
});
Related
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).
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.
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.
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>
I am migrating some win forms panels to WPF.
In WinForms, you can tab into to the combo box using the keyboard, select an item by typing the first character and then hit tab again and the combo to lose focus.
In WPF the second tab will move the focus rectangle down the list, instead of moving onto the next control.
Is there a way to get the old behavior? Or has anyone implemented an ItemTemplate that achieves this behvaiour?
thanks
There is a ComboBoxItem which has an IsTabStop property.
So I just applied the appropriate style:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="IsTabStop" Value="False"/>
</Style>