【问题标题】:Binding problem for a custom control in a datagrid数据网格中自定义控件的绑定问题
【发布时间】:2011-11-08 20:36:30
【问题描述】:

我制作了一个自定义控件来显示一个表示任务完成的饼图。我在这个自定义中创建了一个属性来发送任务完成的百分比。独立测试:它有效。 我想在绑定到数据库的数据网格中使用该控件,所以我做了一个 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,不是吗?但我不明白我做错了什么......

【问题讨论】:

    标签: wpf xaml binding datagrid custom-controls


    【解决方案1】:

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

    删除this.DataContext = this 调用,并将以下内容添加到您的PieChartControl xaml。

    在顶部的类定义中:

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

    对于每个绑定,添加:

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

    【讨论】:

    • 这完全有道理...并解决了问题,谢谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2011-08-05
    相关资源
    最近更新 更多