Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTrigger on a specific Type

I have a scenario where I need to specify Functions like

void SomeFunction(int value)

For this I'm using two DataGrids.

  • The left DataGrid holds the Functions
  • The right DataGrid holds the Parameters for the selected Function

I only want the Parameter DataGrid to be enabled when a valid Function is selected in the Function DataGrid. If the NewItemPlaceHolder (the Last Row when CanUserAddRows="True" for a DataGrid) is selected or if the selection is empty I want it to be disabled. I experimentet with a DataTrigger but I couldn't get it to work

<Style TargetType="DataGrid">
    <Setter Property="IsEnabled" Value="False"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=functionDataGrid,
                                       Path=SelectedItem}"
                     Value="{x:Type systemData:DataRowView}">
            <Setter Property="IsEnabled" Value="True"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Is it possible to check if the value produced by the Binding is of a specific Type? Otherwise, does anyone have any other solutions to this? As of now, I'm handling this with the SelectedCellsChanged event but I would prefer not using code behind

Thanks

like image 779
Fredrik Hedblad Avatar asked Feb 23 '11 13:02

Fredrik Hedblad


2 Answers

If anyone else comes across the same problem, here's what I did to solve it. I created a TypeOfConverter that returns the Type of the value produced by the Binding.

<DataTrigger Binding="{Binding ElementName=functionsDataGrid,
                               Path=SelectedItem,
                               Converter={StaticResource TypeOfConverter}}"
             Value="{x:Type data:DataRowView}">
    <Setter Property="IsEnabled" Value="True"/>
</DataTrigger>

TypeOfConverter

public class TypeOfConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value == null) ? null : value.GetType();
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
like image 121
Fredrik Hedblad Avatar answered Oct 22 '22 21:10

Fredrik Hedblad


Have you considered a DataTemplate for the right DataGrid (parameters)?

Then you could bind the DataContext of your right DataGrid to the SelectedItem of the left DataGrid.

And in your DataTemplate, you can make your right side look like an enabled DataGrid parameter entry form when the DataTemplate's DataType={x:Type local:FunctionObject}. When a 'FunctionObject' is not selected you can have a DataTemplate for that too which shows a disabled DataGrid parameter entry form, or you could choose to display nothing as well.

like image 24
Scott Avatar answered Oct 22 '22 21:10

Scott