WPF datagrid template column combobox binding expression error

419 views Asked by At

I have a combobox in a datagrid template. I have an event trigger on this that calls a command. I have confirmed that the command is calling my function. When my function returns false, I want to set the background color of the combo box to yellow. It if returns true, I want it to remain white.

The problem appears to be in the following section:

"Path=DataContext.ApplicationProfilesCollection[].ValidEnvironment"

In the output window I get:

"System.Windows.Data Error: 40 : BindingExpression path error: '[]' property not found on 'object' ''ObservableCollection`1' (HashCode=44314665)'. BindingExpression:Path=DataContext.ApplicationProfilesCollection[].ValidEnvironment; DataItem='ProfilesUserControl' (Name=''); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')"

Currently there are 3 items in the collection. Do you know how I get WFP to recognize each one? Does anything go in the "[ ]" in the XAML? Also - I must adhere to the MVVM pattern.

Please see XAML below. Thanks for your time,

<DataTemplate DataType="models:ApplicationProfile">
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource 
                        Mode=FindAncestor,
                        AncestorType={x:Type UserControl}},
                        Path=DataContext.DsnCollection}"
          Validation.ErrorTemplate="{StaticResource ValidationTemplate}"
          SelectedItem="{Binding DataSource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,  ValidatesOnDataErrors=True}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ValidateDataSourceCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ComboBox.Style>
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ApplicationProfilesCollection[].ValidEnvironment}" Value="false">
                    <Setter Property="ComboBox.Background" Value="Yellow"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
</ComboBox>

1

There are 1 answers

0
mm8 On

ValidEnvironment is supposed to a public bool property of an item in the ItemsSource collection (DsnCollection) that you set to true/false for each item (Dsn or whatever you call it) in the collection.

You can then bind to it like this:

<Style TargetType="ComboBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ValidEnvironment}" Value="False">
            <Setter Property="ComboBox.Background" Value="Yellow"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

You can't bind a DataTrigger to a command because a command doesn't return anything.