如何在用户控件中使用 DependencyProperty

How to use DependencyProperty in user Control

我想创建一个可以通过 xaml 接收参数的用户控件。

我搜索并尝试了一些东西,但是 none 似乎对我有用。 我附上了重要的代码。 我的用户控件代码(仅依赖项属性):

 public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register(
        "DatabaseName", typeof(string), typeof(TablesForm), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsArrange));
    public string DatabaseName
    {
        get { return (string)GetValue(DatabaseNameProperty); }
        set
        {
            SetValue(DatabaseNameProperty, value);
            NotifyPropertyChanged("DatabaseName");
        }
    }

来自 Main 的调用 Window:

<StackPanel>
     <local:TablesForm DatabaseName="Testing"/>
</StackPanel>

当我将用户控件构造器中的字符串直接放入 dependencyProperty 时,它就可以工作了。 如果有人能解释背后的逻辑(并帮助我解决我的问题),那就太好了

感谢您的帮助。

UserControl 的有效实现是这样的

XAML UserControl1

<UserControl x:Class="WpfApplication1.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" 
             Name="_this"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Background="LightGray" Text="{Binding ElementName=_this, Path=DatabaseName}" />
    </Grid>
</UserControl>

CSharp UserControl1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public static readonly DependencyProperty DatabaseNameProperty =
            DependencyProperty.Register
            (
                @"DatabaseName",
                typeof(String),
                typeof(UserControl1),
                new PropertyMetadata(String.Empty)
            );
        public String DatabaseName
        {
            get { return GetValue(DatabaseNameProperty) as String; }
            set { SetValue(DatabaseNameProperty, value); }
        }

        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

XAML 测试窗口

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525"
        >
    <Grid>
        <local:UserControl1 DatabaseName="Database_Test" />
    </Grid>
</Window>

CSharp 测试窗口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

我终于让它工作了,我所错过的只是将实现添加到 Propertycallbackchanged。我不必设置元素名称。我的意思是,我所要做的就是改变:

public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register(
    "DatabaseName", typeof(string), typeof(TablesForm), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsArrange));

为此:

 public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register(
        "DatabaseName", typeof(string), typeof(TablesForm), new FrameworkPropertyMetadata(String.Empty, OnCurrentTimePropertyChanged));

并实施 OnCurrentTimePropertyChanged

private static void OnCurrentTimePropertyChanged(DependencyObject source,
    DependencyPropertyChangedEventArgs e)
    {
        TablesForm control = source as TablesForm;
        control.m_viewModel.Log = (string)e.NewValue;            
    }

这将帮助我操纵 DependencyProperty。

感谢@Benj 的帮助。