We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Binding HeaderText to ViewModel

Thread ID:

Created:

Updated:

Platform:

Replies:

143819 Apr 5,2019 09:43 PM UTC Apr 9,2019 12:25 PM UTC UWP 3
loading
Tags: SfDataGrid
Alexander
Asked On April 5, 2019 09:43 PM UTC

Hello,

I'm having trouble binding the Column Header Text to a value of the the view model.
Here is my code:

XAML:

<Page
    x:Class="JACB.Views.Backtest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:JACB.Views"
    xmlns:VM="using:JACB.ViewModels"
    xmlns:sfgrid="using:Syncfusion.UI.Xaml.Grid"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{StaticResource Black}">
    <Page.Resources>
        <VM:VMBacktest x:Key="viewModel"/>
    </Page.Resources>
    <ScrollViewer
            x:Name="BalancesXAML"
            Padding="5,0,0,0"
            IsTabStop="False"
            UseSystemFocusVisuals="False"
            VerticalScrollBarVisibility="Auto"
            VerticalScrollMode="Auto">

            
<!--Other stuff-->
            <sfgrid:SfDataGrid x:Name="ResultGrid" ShowRowHeader="True" Visibility="{Binding ComputedPoints.Count, Converter={StaticResource ItemsSourceCountVisibilityConverter}}" SelectionMode="None" MaxHeight="400" AllowSorting="False" Margin="0,10,0,25" Grid.Column="1" Grid.Row="3" ItemsSource="{Binding ComputedPoints}" IsReadOnly="True" AutoGenerateColumns="False">
                <sfgrid:SfDataGrid.Resources>
                    <Style TargetType="sfgrid:GridRowHeaderCell">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="sfgrid:GridRowHeaderCell">
                                    <Border x:Name="PART_RowHeaderCellBorder"
                                        Background="{TemplateBinding Background}"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="0,0,1,0">
                                        <Grid>
                                            <TextBlock HorizontalAlignment="Center"
                                               VerticalAlignment="Center"
                                               Text="{Binding RowIndex,RelativeSource={RelativeSource TemplatedParent}}"
                                               FontSize="16"
                                               Foreground="{StaticResource DataForeground}"/>
                                        </Grid>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </sfgrid:SfDataGrid.Resources>
                <sfgrid:SfDataGrid.Columns>
                    <sfgrid:GridDateTimeColumn
                        ColumnSizer="AutoLastColumnFill"
                        HeaderText="Date"
                        TextAlignment="Center"
                        MappingName="Date"/>
                    <sfgrid:GridTextColumn
                        ColumnSizer="Auto"
                        HeaderText="Type"
                        TextAlignment="Center"
                        MappingName="Type"/>
                    <sfgrid:GridTemplateColumn
                        ColumnSizer="Star"
                        HeaderText="{Binding Path=Metadata.BaseAsset, Source={StaticResource viewModel}}"
                        TextAlignment="Center"
                        MappingName="BaseBalance">
                        <sfgrid:GridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding BaseBalance, ConverterParameter=6, Converter={StaticResource RoundDecimalsConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="{StaticResource DataForeground}"/>
                            </DataTemplate>
                        </sfgrid:GridTemplateColumn.CellTemplate>
                    </sfgrid:GridTemplateColumn>
                    <sfgrid:GridTemplateColumn
                        ColumnSizer="Star"
                        HeaderText="{Binding Path=Metadata.QuoteAsset, Source={StaticResource viewModel}}"
                        TextAlignment="Center"
                        MappingName="QuoteBalance">
                        <sfgrid:GridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding QuoteBalance, ConverterParameter=6, Converter={StaticResource RoundDecimalsConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="{StaticResource DataForeground}"/>
                            </DataTemplate>
                        </sfgrid:GridTemplateColumn.CellTemplate>
                    </sfgrid:GridTemplateColumn>
                    <sfgrid:GridTemplateColumn
                        ColumnSizer="AutoLastColumnFill"
                        HeaderText="Change"
                        TextAlignment="Center"
                        MappingName="Change">
                        <sfgrid:GridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="{Binding Change, Converter={StaticResource PositiveNegativeFormatConverter}}">
                                    <Run Text="{Binding Change}"/>
                                    <Run Text=" %"/>
                                </TextBlock>
                            </DataTemplate>
                        </sfgrid:GridTemplateColumn.CellTemplate>
                    </sfgrid:GridTemplateColumn>
                </sfgrid:SfDataGrid.Columns>
            </sfgrid:SfDataGrid>
        </Grid>
    </ScrollViewer>
</Page>


ViewModel:
        private CandleMetadata metadata;
        public CandleMetadata Metadata
        {
            get { return metadata; }
            set
            {
                metadata = value;
                OnPropertyChanged();
            }
        }
I've tried binding the values the following ways, but none worked:
StackTrace: Error: BindingExpression path error: 'BaseAsset, QuoteAsset' property not found on 'JACB.Models.CandleMetadata'. BindingExpression: Path='BaseAsset, QuoteAsset' DataItem='JACB.Models.CandleMetadata'; target element is 'Syncfusion.UI.Xaml.Grid.GridColumnWrapper' (Name='null'); target property is 'Value' (type 'Object')
  • HeaderText="{Binding Path=Metadata.QuoteAsset, Source={StaticResource viewModel}}" with
    <Page.Resources>
            <VM:VMBacktest x:Key="viewModel"/>
     </Page.Resources>
  • HeaderText="{Binding Metadata.QuoteAsset}"
  • HeaderText="{x:Bind viewModel.Metadata.QuoteAsset}"
Fun fact is, I'm using this on the same page and it works just fine:
<sfchart:NumericalAxis.Header>
<TextBlock Foreground="{StaticResource ForegroundColor}">
<Run Text="{Binding Metadata.BaseAsset}"/>
<Run Text="/"/>
<Run Text="{Binding Metadata.QuoteAsset}"/>
</TextBlock>
</sfchart:NumericalAxis.Header>

Greetings
Alexander

Deivaselvan Y [Syncfusion]
Replied On April 8, 2019 09:40 AM UTC

Hi Alexander, 
 
Thank you for contacting Syncfusion support. 
 
We have prepared sample based on your code snippet and analyzed the reported issue. By default, the header cell’s DataContext is set from the GridColumn. Hence header text will not be displayed when you bind without referring source like the below one 
 
HeaderText="{Binding Metadata.QuoteAsset}"  
HeaderText="{x:Bind viewModel.Metadata.QuoteAsset}" 
 
Instead, on defining source using StaticResource, the header text is properly binded. Instead of using StaticResource, you can use element name binding to overcome the reported binding issue as like below code snippet. 
 
HeaderText="{Binding ElementName=dataGrid, Path=DataContext.Order.OrderIDText,  
             Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
 
Please find the tested sample from our end in the below link 
Sample Link: 
 
If still issue exists at your end, could you please revert us with the modified sample? It will helps us to investigate further and provide appropriate solution at earliest. 
 
Regards,
Deivaselvan
 


Alexander
Replied On April 8, 2019 10:57 AM UTC

Thank you, that worked for me.

Greetings
Alexander

Deivaselvan Y [Syncfusion]
Replied On April 9, 2019 12:25 PM UTC

Hi Alexander,

We are glad to know that the given solution resolves your requirement. Please feel free to contact us if you need further assistance on this.

Regards, 
Deivaselvan 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon