Gregfr
Gregfr

Reputation: 666

Binding problem for a custom control in a datagrid

I've made a custom control to display a pie chart representing the completion of a task. I've created a property in this custom to send out the percentage of completion of the task. Tested standalone: it works. I want to use that control in a datagrid bound to a database, so i did a DataGridTemplateColumn to put my custom control in it.

PROBLEM: i can't find a way to bind the field of my database to the property of the completion value.

Code for the control

public class PieChartControl : Control
{
    public static readonly DependencyProperty CompletionValueProperty;
    public double CompletionValue
    {
        get { return (double)GetValue(PieChartControl.CompletionValueProperty); }
        set { SetValue(PieChartControl.CompletionValueProperty, value); }
    }


    static PieChartControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(PieChartControl), new FrameworkPropertyMetadata(typeof(PieChartControl)));
        FrameworkPropertyMetadata propMetadataCompletion = new FrameworkPropertyMetadata();
        CompletionValueProperty = DependencyProperty.Register("CompletionValue", typeof(double), typeof(PieChartControl), propMetadataCompletion);
    }

    public PieChartControl()
    {
        this.DataContext = this;
    }

Code of the Window

<Window x:Class="Practice1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Pie="clr-namespace:PieChart;assembly=PieChart"
        xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
        xmlns:local="clr-namespace:Practice1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:ConverterPagesnbToPercent x:Key="ConverterPagesnbToPercent"/>
    </Window.Resources>

 <Grid Name="grid1">
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" Grid.Row="1" >
        <DataGrid.Columns>
            <DataGridTextColumn
                Header="ProjectedNumChap"
                Width="SizeToCells"
                Binding="{Binding ProjectedNumChap}"/>

            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Pie:PieChartControl  CompletionValue="{Binding Path=CompletedNumChap, diagnostics:PresentationTraceSources.TraceLevel=High}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>

</Grid>

So the binding in the DataGridTextColumn works perfectly, but the same one in the DataGridTemplateColumn doesn't. Here's the data i got back in the output, thx to the diagnostic:

System.Windows.Data Warning: 54 : Created BindingExpression (hash=16804014) for Binding (hash=2616333)
System.Windows.Data Warning: 56 :   Path: 'CompletedNumChap'
System.Windows.Data Warning: 58 : BindingExpression (hash=16804014): Default mode resolved to OneWay
System.Windows.Data Warning: 59 : BindingExpression (hash=16804014): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 60 : BindingExpression (hash=16804014): Attach to PieChart.PieChartControl.CompletionValue (hash=42879999)
System.Windows.Data Warning: 65 : BindingExpression (hash=16804014): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=16804014): Found data context element: PieChartControl (hash=42879999) (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=16804014): Activate with root item PieChartControl (hash=42879999)
System.Windows.Data Warning: 106 : BindingExpression (hash=16804014):   At level 0 - for PieChartControl.CompletedNumChap found accessor <null>
System.Windows.Data Error: 40 : BindingExpression path error: 'CompletedNumChap' property not found on 'object' ''PieChartControl' (Name='')'.    BindingExpression:Path=CompletedNumChap; DataItem='PieChartControl' (Name=''); target element is 'PieChartControl' (Name=''); target property is 'CompletionValue' (type 'Double')
System.Windows.Data Warning: 78 : BindingExpression (hash=16804014): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 86 : BindingExpression (hash=16804014): TransferValue - using fallback/default value '0'
System.Windows.Data Warning: 87 : BindingExpression (hash=16804014): TransferValue - using final value '0'

For some reason it looks like it think that he should look for CompletedValue in the custom control, isn't it? But I don't get what i'm doing wrong though...

Upvotes: 1

Views: 1700

Answers (1)

fatty
fatty

Reputation: 2513

Your PieChartControl is setting it's DataContext property which is making it so you can't bind outside of the Control without specifying the Source or RelativeSource.

Remove the this.DataContext = this call, and add the following to your PieChartControl xaml.

In your class definition at the top:

xmlns:self="clr-namespace:Application.Controls.PieChartControl"

and for each of your bindings, add:

, RelativeSource={RelativeSource AncestorType={x:Type self:PieChartControl}}

Upvotes: 2

Related Questions