0
votes

In Silverlight (but will probably equally apply to wpf) I have a datagrid which has validation and can go into an invalid state. I check this by:

if(grid.IsValid)
{
    ...
}

I have a checkbox that triggers saving when unchecked, I wish for this checkbox to be disabled, or at least not uncheck if the grid is in an invalid state.

Something like:

BeforeUncheckEvent
{
    if(datagridisvalid)
        allow;
    else
        disallow;
}

Binding to the IsEnabled based on the grids IsValid would be even better. This does not work:

IsEnabled="{Binding ElementName=grid, Path=IsValid, UpdateSourceTrigger=PropertyChanged}"

UPDATE>>

I have tried binding another checkbox to the grids IsValid and that works, even when the other checkbox is almost identical to the one that does not work.

SOME CODE>>

When I check this :

    <CheckBox x:Name="chkAllowManual" Checked="chkAllowManual_Checked" Unchecked="chkAllowManual_Unchecked" Content="Allow Manual" VerticalAlignment="Center" Margin="3,0,7,0" IsEnabled="{Binding ElementName=grdPrices, Path=IsValid}">
    </CheckBox>

The grid gets its itemsource set, has some errors after edit then this gets disabled properly (not the original checkbox but the one next to it) :

    <CheckBox x:Name="chkAllowManual1" Checked="chkAllowManual_Checked" Unchecked="chkAllowManual_Unchecked" Content="Allow Manual1" VerticalAlignment="Center" Margin="3,0,7,0" IsEnabled="{Binding ElementName=grdPrices, Path=IsValid}">
    </CheckBox>

Any ideas?

1
where is IsValid defined? in Viewmodel or Code behind?Nitin
@nit its part of the datagrid in silverlightsprocket12
bro will need to see your code xaml and codebehind to crack this.. ideally it should workNitin
What error did you get when you tried the IsEnabled binding that you say didn't work? (You can find errors in the Output Window in Visual Studio.)Sheridan
@Sheridan no error. I am beginning to think the binding gets removed as a converter attached to it does not even get called when the IsInvalid=true ... all I do after the initial binding and it not working is reset the itemsource of the grid, maybe that is doing it somehow...sprocket12

1 Answers

0
votes

If you have access to a reference of your DataGrid wherever the Checked handler is, then you could try doing this:

private void chkAllowManual_Checked(object sender, RoutedEventArgs e)
{
    if (!dataGrid.IsValid) e.Handled = true;
    else
    {
        // do your stuff here
    }
}

This should stop it from becoming checked when dataGrid.IsValid is flase.