关于 UserControl 和 WPF 对话框的 DependencyProperty 问题

DependencyProperty issues with respect to a UserControl and WPF Dialog

我已经尽可能简化了代码以尝试获得一段有效的代码,但我仍然做不到。一些建议将不胜感激。

我正在尝试让 DependencyProperty 正常工作,就这么简单,但我在主 window 上设置的数据没有显示在用户控件中。

在 MainWindow 中,我将 xaml 中的 TextValue 设置为 "hi"。 TextValue 显示在 xaml 中并且编译得很好,所以我很确定我的 DependencyProperty 设置正确。对话框完全打开后,我查看调试器,我的 属性 TextValue 仍然为空。

我是否缺少设置数据上下文?也许我在我想做的事情上偏离了基地。

感谢您花时间找出我做错了什么。

我的用户控件是:UserControl1 Xaml:

<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" 
             Loaded="UserControl_Loaded"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

    </Grid>
</UserControl>

UserControl1.xaml.cs 是:

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

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("TextValue", typeof(string), typeof(UserControl1));

        private string _tv;
        public string TextValue
        {
            get
            {
                return _tv;
            }
            set
            {
                _tv = value;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}

我的电话 window 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:usercontrols="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525"
        Loaded='Window_Loaded'>
    <Grid>
        <usercontrols:UserControl1 x:Name="usercontroltest1" TextValue="hi"/>
    </Grid>
</Window>

我调用 window.cs 是:

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

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}

"property wrapper" 的 getter 和 setter 必须调用 DependencyObject 基础 class 的 GetValue 和 SetValue 方法,如下所示。除此之外,还有一个命名约定,要求依赖项 属性 的标识符字段的命名方式类似于 属性 加上 Property 后缀。有关所有详细信息,请参阅 Custom Dependency Properties

public static readonly DependencyProperty TextValueProperty =
    DependencyProperty.Register(
        nameof(TextValue), typeof(string), typeof(UserControl1));

public string TextValue
{
    get { return (string)GetValue(TextValueProperty); }
    set { SetValue(TextValueProperty, value); }
}

为了在其自身 XAML 中访问 UserControl 的依赖项 属性,您通常会像这样使用 RelativeSource 绑定:

<UserControl x:Class="WpfApplication1.UserControl1" ...>
    <Grid>
        <TextBlock Text="{Binding TextValue,
                          RelativeSource={RelativeSource AncstorType=UserControl}}" />
    </Grid>
</UserControl>