简体   繁体   English

自定义控件上的WPF数据绑定错误

[英]WPF Databinding error on custom control

I am currently getting the following error 我目前收到以下错误

System.Windows.Data Error: 40 : BindingExpression path error: 'EthernetView' property not found on 'object' ''LabelTextBox' (Name='Root')'. BindingExpression:Path=EthernetView.SessionName; DataItem='LabelTextBox' (Name='Root'); target element is 'LabelTextBox' (Name='Root'); target property is 'Text' (type 'String')

when I bind data to a custom control like 当我将数据绑定到像这样的自定义控件时

<controls:LabelTextBox Grid.Column="0" Padding="10,5,10,0" LabelText="Name" Text="{Binding EthernetView.SessionName}" 
                                       TextWidth="175"  IsReadOnly="True" IsError="False" HorizontalAlignment="Right"/>

I am pretty sure I have my Dependency property on the object set up correctly 我很确定我对对象的依赖项属性设置正确

public partial class LabelTextBox : UserControl, INotifyPropertyChanged
{
    public string Text
    {
        get { return LblTextBox.Text; }
        set 
        {
            SetValue(TextProperty, value);
            LblTextBox.Text = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Text"));
        }
    }

    public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register(
        "Text",
        typeof(string),
        typeof(LabelTextBox),
        new PropertyMetadata(default(string), OnTextPropertyChanged));

    private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        LabelTextBox source = d as LabelTextBox;
        source.LblTextBox.Text = e.NewValue as string;
    }
}

The xaml for the custom control looks like 自定义控件的xaml看起来像

<UserControl x:Class="Project.LabelTextBox"
             x:Name="Root"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             mc:Ignorable="d" 
             d:DesignHeight="20" d:DesignWidth="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition x:Name="TextWidthColumn" Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <TextBlock x:Name="LeftLabel" Grid.Column="0" Foreground="Black" Padding="0,0,10,0" VerticalAlignment="Center" FontSize="12"
                   Text="{Binding LabelText, ElementName=Root, Mode=TwoWay}"/>
        <Border x:Name="TextBoxBorder" Grid.Column="1" BorderThickness="2" Background="#FAFAFAFA">
            <TextBox x:Name="LblTextBox" VerticalAlignment="Center" Margin="1"  FontSize="12" Text="{Binding Text}"
                   TextChanged="LblTextBox_TextChanged" KeyDown="OnKeyDownHandler"/>
        </Border>
        <Image x:Name="ErrorImage" Grid.Column="2" Source="pack://application:,,,/Project;component/Images/RedX.ico" Height="12" Width="12"/>
        <TextBlock x:Name="RightLabel" Grid.Column="3" Foreground="Black" Padding="10,0,0,0" VerticalAlignment="Center" FontSize="12"
                   Text="{Binding LabelText, ElementName=Root, Mode=TwoWay}"/>
    </Grid>
</UserControl>

The code works properly at runtime, but I would like to clean up the output errors. 该代码在运行时可以正常运行,但是我想清除输出错误。 Can anyone give me some insight on how to fix this error. 谁能给我一些有关如何解决此错误的见解。

Dependency Properties looks like this (GetValue, SetValue!): 依赖项属性如下所示(GetValue,SetValue!):

    public readonly static DependencyProperty IsBusyProperty = DependencyProperty.Register(
    "IsBusy", typeof(bool), typeof(BusyControl), new PropertyMetadata(false));

    public bool IsBusy
    {
        get { return (bool)GetValue(IsBusyProperty); }
        set { SetValue(IsBusyProperty, value); }
    }

so yours is not right. 所以你不对

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM