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.
Related
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();
});
My DataGrid columns are being added in code behind.
When the user is done editing rows and clicks on a Next button, DataGrid is disabled and user is taken to the next step which is in the same view..
The problem arises if user has a lot of rows and needs to scroll over the now disabled DataGrid to overview the rows.
I've tried wrapping the DataGrid (which is bound to a property that sets IsEnabled = false when user clicks on Next) in a ScrollViewer which gives me the desired effect but the scroll is then located outside the DataGrid which I find annoying.
I'm at my wits end and could really use some help how I should go about doing this.
I've also tried overriding the metadata for ScrollViewer which works somewhat but is unsafe from what I've read.
Any ideas?
Instead of using the IsEnabled property you can use the DataGrid.IsReadOnly property.
Mark the DataGrid as ReadOnly and apply the following style to get the appearance like disabled (if required).
<DataGrid.Style>
<Style TargetType="DataGrid">
<Style.Triggers>
<Trigger Property ="IsReadOnly" Value="True">
<Setter Property= "Foreground" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Style>
I ended up using the IsReadOnly property on the DataGrid and bound it to IsEnabled.
This was an apparent problem at first because IsEnabled was true making the DataGrid read-only in the wrong state.
So I created a helper class to convert the bool to false to reflect the correct states (enabled/disabled).
The components in the DataGrid have their IsEnabled property databound also which gives me the proper disabled styles for them when DataGrid is read-only.
Thanks for the help guys!
I have a custom window and would like to apply a custom style to the window when it cannot be accessed due to it having a child window (displayed via showdialog()). I assumed that the trigger property would be "IsEnabled", however this property is not set to false when showdialog() is called. I have also tried "Focusable" and looked through the list of properties in hope of finding the obvious solution. This led to me trying a data trigger which binds to "OwnedWindows.Count" but again this doesn't work! Surely this should be simple and I am missing something?
Triggers tried:
<Trigger Property="IsEnabled" Value="False">
<Trigger Property="Focusable" Value="False">
<DataTrigger Binding="{Binding Path=OwnedWindows.Count, RelativeSource={RelativeSource Self}}" Value="1" >
Note: Both IsEnabled and Focusable do the required job when I manually set the properties to False - so I know the trigger works, they just aren't being set when ShowDialog() is called.
There is no property like that. Use ComponentDispatcher.EnterThreadModal and ComponentDispatcher.LeaveThreadModal events instead. They are fired when a WPF modal dialog is shown or closed, respectively.
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>