2014-01-23 50 views
0

我是wpf的初學者,我想幫助您製作這段XAML代碼。「Cascade」Databinding

<DataGrid ItemsSource="{Binding Elements[person]}" > 
     <DataGrid.Columns> 
      <DataGridTextColumn x:Name="headerPhone" Binding="{Binding Element[phone].Value}"> 
      <DataGridTextColumn.CellStyle> 
       <Style TargetType="{x:Type DataGridCell}" x:Name="headerPhoneCStyle"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding Element[phone].Attribute[changed].Value}" Value="yes"> 
         <Setter Property="Background" Value="Red"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </DataGridTextColumn.CellStyle> 
     </DataGridTextColumn> 
    </DataGrid.Columns> 
</DataGrid> 

有一些方法如何減少這樣的:

Binding="{Binding Element[phone].Attribute[changed].Value}" 

要只是這樣的事情:

Binding="{Binding Attribute[changed].Value}" 
+0

'Binding =「{Binding Attribute [changed] .Value}」'這是行不通的? – Sankarann

回答

0

要做到這一點,你需要改變項目的DataContext。目前,您的DataGrid.ItemsSource屬性是綁定到Elements[person]對象的數據...此必須是某種類型的集合,因此各項的DataContext設置爲來自此集合的元素

很難告訴你的數據類的確切結構,因爲你沒有告訴我們[you should always show all the relevant code],但如果你的作品Binding,那麼它好像每個項目有一個名爲它Element索引屬性。現在,如果你想將數據直接與正在使用該指數phone時返回的對象綁定,你可以添加到ItemsSource Binding代替:

<DataGrid ItemsSource="{Binding Elements[person].Element[phone]}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn x:Name="headerPhone" Binding="{Binding Value}"> 
      <DataGridTextColumn.CellStyle> 
       <Style TargetType="{x:Type DataGridCell}" x:Name="headerPhoneCStyle"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding Attribute[changed].Value}" 
          Value="yes"> 
          <Setter Property="Background" Value="Red" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </DataGridTextColumn.CellStyle> 
     </DataGridTextColumn> 
    </DataGrid.Columns> 
</DataGrid> 

但是,如果Elements[person].Element[phone]的集合,有沒有太多的意思用DataGrid來顯示它...如果它一個集合,那麼這個應該工作得很好。讓我知道你是怎麼辦的。此外,您應該在MSDN上看到Property Path SyntaxBinding.Path Property頁面,以獲取有關Binding.Path語法的更多幫助。