WPF DataGrid模板列组合绑定表达式误差



我在datagrid模板中有一个组合。我有一个事件触发器,可以调用命令。我已经确认该命令正在调用我的功能。当我的功能返回false时,我想将组合框的背景颜色设置为黄色。如果返回为真,我希望它保持白色。

问题似乎在以下部分:

" path = datacontext.applicationprofilescollection []。

在输出窗口中我得到:

" system.windows.data错误:40:绑定表达路径错误:'[]'' 在"对象"上找不到属性'observableCollection`1' (HashCode = 44314665)'。 bindingExpression:path = dataContext.ApplicationProfilesCollection []。 dataitem ='profileSuserControl'(name ='');目标元素是" Combobox" (名称='');目标属性是" notarget"(键入'对象')

当前集合中有3个项目。您知道我如何让WFP识别每个人吗?XAML中的" []另外 - 我必须遵守MVVM模式。

请参阅下面的XAML。感谢您的宝贵时间,

<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>

ValidEnvironment应该是 public bool 属性 ItemsSource集合( DsnCollection)中的项目,您将其设置为 true/false对于集合中的每个项目(Dsn或任何您称之为)。

您可以这样绑定到它:

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

您无法将DataTrigger绑定到命令,因为命令不返回任何内容。

最新更新