数据网格中自定义控件的绑定问题

时间:2011-09-02 01:42:53

标签: wpf xaml binding datagrid custom-controls

我已经制作了一个自定义控件来显示表示任务完成情况的饼图。我在此自定义中创建了一个属性,以发送完成任务的百分比。独立测试:它的工作原理。 我想在绑定到数据库的数据网格中使用该控件,所以我做了一个DataGridTemplateColumn来将自定义控件放入其中。

问题:我找不到将数据库字段绑定到完成值属性的方法。

控件代码

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

窗口代码

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

因此DataGridTextColumn中的绑定工作得很好,但DataGridTemplateColumn中的绑定却没有。 这是我在输出中得到的数据,通过诊断:

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'

出于某种原因看起来它认为他应该在自定义控件中寻找CompletedValue,不是吗?但我不知道我做错了什么......

1 个答案:

答案 0 :(得分:2)

您的PieChartControl正在设置它的DataContext属性,因此您无法在未指定SourceRelativeSource的情况下绑定到控件之外。

移除this.DataContext = this来电,并将以下内容添加到PieChartControl xaml。

在顶部的课程定义中:

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

并为每个绑定添加:

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