【问题标题】:Confused about Binding in UserControl对 UserControl 中的绑定感到困惑
【发布时间】:2013-03-08 21:19:27
【问题描述】:

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

感谢您的帮助。

最好的问候,

古斯塔沃。

用户控制:

<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 partial class MainWindow : INotifyPropertyChanged
{
    // Notify WPF that Counter changed
    public event PropertyChangedEventHandler PropertyChanged;

    public MainWindow()
    {
        InitializeComponent();

        TitleVM = "açsldkfjasçldkfj";
    }

    private String titleVM;
    public String TitleVM
    {
        get { return titleVM; }
        set
        {
            titleVM = value;
            OnPropertyChanged("TitleVM");
        }
    }

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

}

【问题讨论】:

    标签: c# wpf data-binding user-controls inotifypropertychanged


    【解决方案1】:

    您的Window 没有DataContext。无法解析绑定。

    试试这个:

    public MainWindow()
    {
        InitializeComponent();
    
        DataContext = this; //This is what you're missing!
    
        TitleVM = "açsldkfjasçldkfj";
    }
    

    编辑:

    UserControl 中也缺少相同的内容

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

    【讨论】:

    • 感谢您的回答。我已经添加了这一点,我可以看到断点比值即将到来,但显示的只是“MainWindow”文本,而不是“açsldkfjasçldkfj”。有什么线索吗?
    • 我也这样做了。但是当我这样做时,我在 UserControl1.xaml.cs 中的断点,除了 TitleProperty 中的断点没有进入,它们没有从 MainWindow 获得任何值。
    【解决方案2】:

    在 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)
             );
    

    我们眼中的干净代码。

    PS:不用写了:

    DataContext = this;
    

    关于 UserControl 背后的代码。至少这里只导致错误,没有值。

    感谢 HighCore 的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      • 2012-07-22
      相关资源
      最近更新 更多