DataGrid SelectedIndex set 0 by default - c#

I am using WPF to make a program with DataGrid. I have a Delete button and when I select one row in DataGrid, the button enables and I can use it. So if SelectedIndex is bigger than -1 the button enables. But I found out, the SelectedIndex is set to 0 by default, so it's like the first row is always selected.
Is there any other property of DataGrid I could use? Or is there some other way to make this work?

You can use DataTrigger on button to handle the disable/visible of buttons, something like :
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=nameOfDataGrid, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="IsEnabled"Value="False"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>

Related

Wpf datagrid : Add button on selected Cell after binding and set background on specific empty cells

I've got a DataGrid into a WPF window.
On loaded the window, my datagrid is empty.
After selected the Excel file, I've got a function to fill my datagrid.I create a new DataTable, adding my columns and fill my data. Finnaly, I do a my_dataGrid.DataContext = dataTable;
Everything is ok.
Now, I'd like to do 2 things :
For a spectific column, I'd like to set the Background color of empty cells. I found this code to color all empty cells but not for a spectific column...
<DataGrid.CellStyle>
<Style TargetType="DataGridCell" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}}" Value="" >
<Setter Property="Background" Value="Gray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
When I click on cell of a specific column, I'd like to show a button, I don't know how
Could you help me please?
Here I my phases on captures...

Bind visibility of a control to combobox selection

I have a combobox which is bound to an Enum datatype. Right now the combobox binding works fine but when I tried to bind the visibility of a checkbox to the combobox selection, this binding is not working as expected. What I wanted to do was whenever the combobox selection is "Restore", I want a checkbox to be visible. Below is the code that I am using.
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cmbOperation, Path=SelectedValue}" Value="Restore">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
I tried changing the Path between SelectedValue, SelectedItem , SelectedValue.TosString() (hopelessly) but I am not getting the checkbox to change its visibility whenever the combobox has "Restore" as its selection. Should I be making any changes in the Enum that I am binding to the Combobox ? If not, what else am I doing wrong?
I'm willing to bet that you've set Visibility on the CheckBox in the XAML:
<CheckBox
Visibility="Collapsed"
>
However, due to the rules of Dependency Property Value Precedence in WPF, that will override anything that happens in the Style. This is by design and it's not a bad idea when you think through all the implications, but it bites everybody who's new to WPF.
It's an easy fix: Just set the starting value in a Setter in the Style. What the Style does, the Style can undo.
<CheckBox
>
<CheckBox.Style>
<Style TargetType="CheckBox">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cmbOperation, Path=SelectedValue}" Value="Restore">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>

Collapse details in listbox by clicking a button

I'm trying do make something like this: when you click the button, the listboxitem collapses and shows some additional information:
I have already made something similar: details are showed when the whole item item is clicked.
Any ideas how to modify this style so collapsing will take place only if the button (or near it) is clicked?
Code:
<Style x:Key="collapsingGridStyle" TargetType="{x:Type Grid}">
<Style.Triggers>
<DataTrigger
Binding="{Binding
Path=IsSelected,
RelativeSource=
{
RelativeSource
Mode=FindAncestor,
AncestorType={x:Type ListBoxItem}
}
}"
Value="False">
<Setter Property="Grid.Visibility"
Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
There are many ways how can you achieve this behavior. For example, you can use expander in listboxitem template.

how to write ParentListBox.SelectedIndex in xaml?

I have a ListBox having ComboBoxes as ListItems. The ComboBox in each ListItem is created using ListBox's ItemTemplate.
Now, suppose I have 5 ListItems. i.e. 5 ComboBoxes.
For the 1st ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource.
For the 2nd ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource.
For the 3rd ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
For the 4th ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
For the 5th ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
So, I think I have to use different DataSource for different ComboBoxes.
And for that to happen I have the following starting code:
<ComboBox....>
<ComboBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="ParentListBox.SelectedIndex" Value="0">
<Setter Property="DataSource" Value="{Binding Path=ListCorrespondingToValue1}"/>
</DataTrigger>
<DataTrigger Binding="ParentListBox.SelectedIndex" Value="Non-0">
<Setter Property="DataSource" Value="{Binding Path=ListCorrespondingToValue2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
<ComboBox.Style>
</ComboBox>
Now, my question is what should I use insead of ParentListBox.SelectedIndex in the above code and what should I replace Non-0 with?
You don't use a DataTrigger for the non zero part... you just set it as a Setter in the Style, so that becomes the default value and then the DataTrigger changes the DataSource property only when the value is 0. Try this:
<ComboBox....>
<ComboBox.Style>
<Style>
<Setter Property="DataSource" Value="{Binding
Path=ListCorrespondingToValue2}"/>
<Style.Triggers>
<DataTrigger Binding="ParentListBox.SelectedIndex" Value="0">
<Setter Property="DataSource" Value="{Binding
Path=ListCorrespondingToValue1}"/>
</DataTrigger>
</Style.Triggers>
</Style>
<ComboBox.Style>
</ComboBox>

WPF Datagrid highlight row and column

Is there a way to programmatically highlight a row and column in the WPF datagrid? I am using the scrollIntoView method to jump to that row and column. I'd like to highlight that row as well to highlight to the user that this is what's important. Thanks!
If you have a business object for whatever is represented in the datagrid, in the DataGrid.RowStyle I would use a DataTrigger bound to a boolean in the object representing the row. Then when you ScrollIntoView you could set this boolean and let XAML handle setting the row color for you. It might get weird because then you would have to reset any other ones set. But I think this could be an easy solution.
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger >
</Style.Triggers>
</Style>
</DataGrid.RowStyle>

Categories