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

发布于 2024-12-02 17:16:01 字数 4541 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2024-12-09 17:16:01

您的 PieChartControl 正在设置它的 DataContext 属性,这样您就无法在不指定 Source的情况下绑定到控件外部相对来源

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

在顶部的类定义中:

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

并为每个绑定添加:

,relativeSource={RelativeSource AncestorType={ x:类型 self:PieChartControl}}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文