2017-09-15 97 views
0

我在數據網格模板中有一個組合框。 我有一個事件觸發器,它調用一個命令。我已確認該命令正在調用我的功能。 當我的函數返回false時,我想將組合框的背景顏色設置爲黃色。如果返回true,我希望它保持白色。WPF datagrid模板列組合框綁定表達式錯誤

的問題似乎是在以下部分:

"Path=DataContext.ApplicationProfilesCollection[].ValidEnvironment"

在輸出窗口,我得到:

"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')"

目前有集合中的3項。你知道我如何讓糧食計劃署認識到每一個? 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> 

+0

ComboBox中的項目(即DsnCollection中的項目)是否具有ValidEnvironment屬性?或者這個屬性在哪裏定義? – mm8

+0

你需要顯示你的數據模型,因爲異常消息顯示def你的數據綁定是錯誤的 – esiprogrammer

+1

顯然'[]'不是用XAML寫的有效的東西。你能澄清你想要做什麼嗎?我相信你需要使用一個轉換器,但我不確定你想要做什麼。 – Kilazur

回答

0

ValidEnvironment應該是一個項目的ItemsSource集合(DsnCollection)您爲每個項目設置爲true/false公共bool財產Dsn或任何你稱之爲)的集合。

然後,您可以綁定到它是這樣的:

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

不能綁定DataTrigger的命令,因爲命令不返回任何東西。