2016-06-08 43 views
0

我有一個XamDataGrid,其中ObservableCollection的Items爲DataSource。 一個項目有兩個屬性A和B,A和B都有一個Value屬性。我想將這兩個Value屬性綁定到我的兩個字段。爲XamDataGrid使用複雜的字段

顯然,這樣的事情不工作:

<Field Name="A.Value"/> 

我看了一些關於AlternateBinding,但是,這並不爲我工作之一:

<Field AlternateBinding="A.Value"/> 

我還沒有找到如何一個很好的教程使用AlternateBinding,所以我真的不知道如何使用它。

編輯:

當我使用的是UnboundField這樣它的工作原理:

<UnboundField Name="Value" BindingPath="A.Value" BindingMode="TwoWay"/> 

但是它說,UnboundField已被廢棄,我應該使用領域。我希望我知道如何。

回答

1

將項目的屬性設置爲公共,並將字段的名稱設置爲屬性的名稱。

例子:

<Window x:Class="WPFXamDataGrid.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-mpatibility/2006" 
      xmlns:igDP="http://infragistics.com/DataPresenter" 
      xmlns:local="clr-namespace:WPFXamDataGrid" 
      mc:Ignorable="d" 
      Title="MainWindow" Height="350" Width="525"> 
     <Grid> 
      <igDP:XamDataGrid x:Name="xamDataGrid" Loaded="XamDataGrid_Loaded"> 
       <igDP:XamDataGrid.FieldLayouts> 
        <igDP:FieldLayout> 
         <igDP:Field Name="Name"/> 
         <igDP:Field Name="Date"/> 
        </igDP:FieldLayout> 
       </igDP:XamDataGrid.FieldLayouts> 
      </igDP:XamDataGrid> 
     </Grid> 
    </Window> 


using System; 
using System.Collections.ObjectModel; 
using System.Windows; 

namespace WPFXamDataGrid 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void XamDataGrid_Loaded(object sender, RoutedEventArgs e) 
     { 
      ObservableCollection<Item> list = new ObservableCollection<Item>(); 

      list.Add(new Item {Name = "First", Date = DateTime.Now}); 
      list.Add(new Item { Name = "Second", Date = ateTime.Now.AddDays(-1); 

      xamDataGrid.DataSource = list; 
     } 
    } 

    class Item 
    { 
     public string Name { get; set; } 
     public DateTime Date { get; set; } 
    } 
} 

編輯一個:

<Style x:Key="CellStyle" TargetType="{x:Type igDP:CellValuePresenter}"> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <StackPanel DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}}"> 
         <TextBlock Text="{Binding Path=Value.Length}" FontWeight="Bold" /> 
        </StackPanel> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
</Style> 

<igDP:Field Name="Name" CellValuePresenterStyle="{StaticResource CellStyle}"/> 
+0

請再看看我的問題。在你的例子中,我不想綁定'Name',而是'Name.Length'。 – gartenriese

+0

您可以將Name.Length作爲Item的公共屬性公開。然後綁定(簡單地聲明)與該屬性具有相同名稱的XamDataGrid的Field。 –

+0

但是,我正在尋找一種直接綁定到孩子的方式,而不創建新的類型。有點像'UnboundField'。 – gartenriese