Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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# 对UserControl中的绑定感到困惑_C#_Wpf_Data Binding_User Controls_Inotifypropertychanged - Fatal编程技术网

C# 对UserControl中的绑定感到困惑

C# 对UserControl中的绑定感到困惑,c#,wpf,data-binding,user-controls,inotifypropertychanged,C#,Wpf,Data Binding,User Controls,Inotifypropertychanged,我正在创建一个UserControl对象,并尝试使用绑定(PropertyChanged)分配值。我制作了一个原型,当我在ViewModel中赋值时,该值不会出现或修改UserControl组件,但是如果我直接在视图中的UserControl对象中赋值,修改就会起作用。我想了解我做错了什么,因为如果我只在窗口上添加一个对象并直接绑定(同样,使用PropertyChanged)就可以了。 按照下面的代码操作 谢谢你的帮助 致以最良好的祝愿 古斯塔沃 UserControl: <UserCon

我正在创建一个UserControl对象,并尝试使用绑定(PropertyChanged)分配值。我制作了一个原型,当我在ViewModel中赋值时,该值不会出现或修改UserControl组件,但是如果我直接在视图中的UserControl对象中赋值,修改就会起作用。我想了解我做错了什么,因为如果我只在窗口上添加一个对象并直接绑定(同样,使用PropertyChanged)就可以了。 按照下面的代码操作

谢谢你的帮助

致以最良好的祝愿

古斯塔沃

UserControl:

<UserControl x:Class="WpfControlLibrary1.UserControl1"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="30" d:DesignWidth="300">
<Grid>
    <TextBlock Text="{Binding Title}" />
</Grid>
</UserControl>
    public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    #region Title Property

    public static String GetTitle(DependencyObject obj)
    {
        return (String)obj.GetValue(TitleProperty);
    }

    public static void SetTitle(DependencyObject obj, String value)
    {
        obj.SetValue(TitleProperty, value);
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.RegisterAttached(
            "Title",
            typeof(String),
            typeof(UserControl1),
            new FrameworkPropertyMetadata(TitleChangedCallback)
            );

    private static void TitleChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        UserControl1 _this = (d as UserControl1);
    }

    private String title;
    public String Title
    {
        get { return title; }
        set
        {
            title = value;
            OnPropertyChanged("Title");
        }
    }

    #endregion

    #region INotifyPropertyChanged event and method

    public event PropertyChangedEventHandler PropertyChanged;

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    #endregion
**我的窗口:**

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <uc:UserControl1 Title="{Binding TitleVM, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>

您的
窗口
没有
数据上下文
。无法解析绑定

试试这个:

public MainWindow()
{
    InitializeComponent();

    DataContext = this; //This is what you're missing!

    TitleVM = "açsldkfjasçldkfj";
}
编辑:

UserControl

public UserControl1()
{
    InitializeComponent();
    DataContext = this;
}

在HighCore的帮助下,我发现了一个问题,另一个问题是我的UserControl没有定义名称

只要说:

x:Name="UserControl"
进入UserControl的XAML,并将正常工作。此外,我已将我的代码修改为:

        public String Title
    {
        get { return (String)base.GetValue(TitleProperty); }
        set { base.SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TitleProperty =
     DependencyProperty.RegisterAttached(
         "Title",
         typeof(String),
         typeof(UserControl1),
         new FrameworkPropertyMetadata(null)
         );
一个干净的代码

附言:没有必要把:

DataContext = this;
关于UserControl的代码隐藏。至少在这里只有错误的结果,没有值提交


感谢HighCore的帮助。

感谢您的回答。我已经添加了这一点,我可以看到断点比值来了,但显示的只是一个“主窗口”文本,而不是“açsldkfjasçldkfj”。有什么线索吗?我也是这么做的。但当我这样做时,UserControl1.xaml.cs中的断点(TitleProperty中的断点除外)不会进入,它们不会从MainWindow中获取任何值。
DataContext = this;