Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义控件DataContext不';行不通_C#_Wpf - Fatal编程技术网

C# 自定义控件DataContext不';行不通

C# 自定义控件DataContext不';行不通,c#,wpf,C#,Wpf,我有一个自定义控制器,它的工作原理如下: public static readonly DependencyProperty IDProperty = DependencyProperty.Register( "ID", typeof(int), typeof(DLBox), new FrameworkPropertyMetadata( 0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); pub

我有一个自定义控制器,它的工作原理如下:

public static readonly DependencyProperty IDProperty = DependencyProperty.Register(
    "ID", typeof(int), typeof(DLBox),
    new FrameworkPropertyMetadata(
        0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public int ID
{
    get { return (int)GetValue(IDProperty); }
    set { SetValue(IDProperty, value); }
}

我的意思是将
ID
绑定到
ViewModel
中的属性。但是当我想像这样把它绑起来的时候:

public static readonly DependencyProperty IDProperty = DependencyProperty.Register(
    "ID", typeof(int), typeof(DLBox),
    new FrameworkPropertyMetadata(
        0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public int ID
{
    get { return (int)GetValue(IDProperty); }
    set { SetValue(IDProperty, value); }
}

绑定到父级的DataContext,它根本不起作用。我有一些其他的自定义控制器,它们做得很好,但我不知道这一个到底有什么问题

这是控制器:

public partial class DLBox : UserControl
{
    public static DependencyProperty IDProperty =
        DependencyProperty.Register("ID", typeof(int), typeof(DLBox),
            new FrameworkPropertyMetadata(0,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                (o, e) => (o as DLBox).IDPropertyChanged((int)e.NewValue)));
    public int ID { get; set; }

    private void IDPropertyChanged(int e)
    {
        ID = e;
    }
}
谁能告诉我怎么了?因为我已经调试了6个小时了,什么都没找到!非常感谢。

更新: 这只需要添加:


我不知道为什么
现在真正的问题是,我想在2个项控件中使用它,即使使用DataContext也仍然不起作用。
(为了澄清,我在彼此内部有两个列表。想想第一个列表,比如10所学校第一个列表,每个学校内部都有一些学生第二个列表


更新2: ID只是
UserControl
中的
TextBlock
。这里没有我能展示的东西
我所做的只是在PropertyCallBack中设置
TextBlock
Text(在我的控制器中没有使用MVVM):

与这个问题无关,这就是为什么我无法解决它的原因
谢谢你的帮助

回答: 经过12个小时的努力,我发现这是一个愚蠢的错误!我不知道为什么以及何时在控制器的XAML中设置
DataContext


无论如何,谢谢。

您的依赖属性声明是错误的,因为CLR包装的getter和setter必须分别调用
GetValue
SetValue
方法。除此之外,您的PropertyChangedCallback是多余的。不需要在刚设置属性值时调用的回调中再次设置属性

声明应如下所示:

public static readonly DependencyProperty IDProperty = DependencyProperty.Register(
    "ID", typeof(int), typeof(DLBox),
    new FrameworkPropertyMetadata(
        0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public int ID
{
    get { return (int)GetValue(IDProperty); }
    set { SetValue(IDProperty, value); }
}

您的依赖项属性声明是错误的,因为CLR包装器的getter和setter必须分别调用
GetValue
SetValue
方法。除此之外,您的PropertyChangedCallback是多余的。不需要在刚设置属性值时调用的回调中再次设置属性

声明应如下所示:

public static readonly DependencyProperty IDProperty = DependencyProperty.Register(
    "ID", typeof(int), typeof(DLBox),
    new FrameworkPropertyMetadata(
        0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public int ID
{
    get { return (int)GetValue(IDProperty); }
    set { SetValue(IDProperty, value); }
}

请注意,您不应该执行类似于
(o As DLBox).IDPropertyChanged(…)
的操作,因为如果
o
不是
DLBox
,它将错误地抛出
NullReferenceException
,而您将导出
InvalidCastException
。表达式应改为
((DLBox)o).IDPropertyChanged(…)
。无论如何,您根本不需要它。您的
DLBox
中是否有明确设置其数据上下文的内容,例如
DataContext=this在构造函数中?@Clemens不!我也要设置吗?我不知道这件事**编辑:**尽管它不起作用!不,你不必。我只是问了一下,因为人们在创建用户控件时经常犯错误。从您在问题中显示的情况来看,内部ItemsControl和DLBox上的
DataContext=“{Binding}”
都应该是多余的。这样的表达式几乎总是多余的,因为它只是将当前DataContext设置为自身。当DataContext之前被(错误地)显式设置,因此它不会从控件的父控件继承时,会出现异常。请注意,您不应该执行类似于
(o As DLBox).IDPropertyChanged(…)
,因为如果
o
不是
DLBox
,它将错误地抛出
NullReferenceException
,则会出现异常,而您将退出一个
InvalidCastException
。表达式应改为
((DLBox)o).IDPropertyChanged(…)
。无论如何,您根本不需要它。您的
DLBox
中是否有明确设置其数据上下文的内容,例如
DataContext=this在构造函数中?@Clemens不!我也要设置吗?我不知道这件事**编辑:**尽管它不起作用!不,你不必。我只是问了一下,因为人们在创建用户控件时经常犯错误。从您在问题中显示的情况来看,内部ItemsControl和DLBox上的
DataContext=“{Binding}”
都应该是多余的。这样的表达式几乎总是多余的,因为它只是将当前DataContext设置为自身。例外情况是,DataContext之前被(错误地)显式设置,因此它不是从控件的父级继承的。还不起作用!而且,我以前用过这个解决方案,现在又用了一次,但它根本不起作用。我知道我的方法是多余的,但我不得不这样使用它。还不起作用!而且,我以前用过这个解决方案,现在又用了一次,但它根本不起作用。我知道我的方法是多余的,但我不得不这样使用它。