2

I have a XamDataGrid with a ObservableCollection of Items as DataSource. An Item has two properties A and B and both A and B have a Value property. I want to bind these two Value properties to my two Fields.

Obviously something like this does not work:

<Field Name="A.Value"/>

I read something about AlternateBinding, but that does not work for me either:

<Field AlternateBinding="A.Value"/>

I have not found a good tutorial on how to use AlternateBinding, so I don't really know how to use it.

EDIT:

It works when I'm using an UnboundField like this:

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

However it says that UnboundField is deprecated and I should use Field. I wish I knew how.

gartenriese
  • 4,131
  • 6
  • 36
  • 60

2 Answers2

1

Set the properties on your Items as public and set Fields' name as your properties' name.

Example:

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

EDIT ONE:

<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}"/>
  • Please have another look at my question. In your example, I don't want to bind 'Name', but 'Name.Length', for example. – gartenriese Aug 29 '16 at 06:55
  • You can expose Name.Length as a public property of Item. Then bind (simply declaring) a Field of XamDataGrid with the same name to that property. – Juan J. Sáez Aug 29 '16 at 07:15
  • I was looking for a way to bind directly to the child propertly, without creating a new type, however. Kind of like with an `UnboundField`. – gartenriese Aug 29 '16 at 07:18
  • You can accomplish that creating a new Style and applying it to the Field like I do in EDIT ONE. – Juan J. Sáez Aug 29 '16 at 07:52
1

Actually you almost got it.

<Field Name="Test" AlternateBinding="{Binding A.Value}"/>
Yurii
  • 11
  • 1