0
votes

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?

1
Tried it, but that doesn't work. The property is int and is set to 0, so it is never null. - Tomáš Zajda
@TomášZajda try not with SelectedIndex... - Spawn
Oh, sorry, I misread that. That could be the solution, but I'm not sure, what type (int, string, object, ...) the SelectedItem is. Is there a way I can find out? The DataGrid is filled from database and I don't know what type the items are. - Tomáš Zajda
Do you have access to this database? If you do, it should be fairly trivial to do so. If not, you can just set the type of SelectedItem to type object. - Daniel Hakimi
I believe if you provide us some of your code, it will be easier to find answer to your question. Is this done via binding? or are you doing everything programmatically? - tgpdyk

1 Answers

0
votes

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>