【问题标题】:Dependency Property in a user control library is always null用户控件库中的依赖属性始终为空
【发布时间】:2019-10-08 10:46:15
【问题描述】:

所以我得到了这个控制:

CharacterMapControl.xaml:

<UserControl x:Class="CharacterMap.CharacterMapControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:CharacterMap">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="350"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <TextBlock Text=""></TextBlock>
        </StackPanel>
    </Grid>


</UserControl>

然后是 CharacterMapControl.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace CharacterMap
{
    /// <summary>
    /// Interaction logic for CharacterMapControl.xaml
    /// </summary>
    ///     
    public partial class CharacterMapControl : UserControl 
    {
        public static readonly DependencyProperty FilepathProperty = DependencyProperty.Register("Filepath", typeof(string), typeof(CharacterMapControl));
        public string Filepath
        {
            get { return (string)GetValue(FilepathProperty); }
            set { SetValue(FilepathProperty, value); }
        }



        public CharacterMapControl()
        {
            InitializeComponent();
        }
    }
}

这是在 .NET Core 的 WPF 用户控件库中。

然后我添加了一个新的 WPF App .NET Core 项目并编辑了 MainWindow.xaml,如下所示:

<Window x:Class="WPF_Control_Tester.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:charactermap="clr-namespace:CharacterMap;assembly=CharacterMap"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <charactermap:CharacterMapControl Filepath="D:\\repos\\WpfProjects\\latinchars.xml"></charactermap:CharacterMapControl>
    </Grid>
</Window>

嗯 - 现在 CharacterMapControl.xaml.cs 中的文件路径始终为空。我不明白为什么。它已正确绑定,应该映射到我在 MainWindow 中添加的文件路径,还是?

【问题讨论】:

    标签: c# wpf xaml dependency-properties


    【解决方案1】:

    您尚未将 TextBlock 的 Text 属性绑定到任何东西。

    当我尝试您的代码时,我添加了绑定:

            <TextBlock Text="{Binding Filepath, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    

    哪个有效

    【讨论】:

    • 嗯,我不明白为什么突然间它起作用了……但它确实起作用了。感谢那!尽管@nachiappan-kumarappan 的解决方案也有效并且更有意义:D 我如何在不实际绑定 dep 道具的情况下使用它?
    • 为什么会选择不绑定呢?依赖属性的一大优点是绑定。如果您不熟悉它,我建议您采用 mvvm 模式。
    • 我实际上只是希望能够将它用作 MainWindow.xaml 中的参数
    • 您可以将其设置为用户控件上的常规公共属性,并在 Loaded 事件中使用它,public string FilePath { get;放; }
    • 好吧,我想我想得太复杂了^^谢谢安迪的帮助!
    【解决方案2】:

    构造CharacterMapControl时,依赖属性值为null,因为定义依赖属性时没有指定默认值。

    在构造完CharacterMapControl控件后,会引发loaded事件,此时依赖属性会有初始化值。

    如下修改构造函数将有助于理解更多。

            public CharacterMapControl()
            {
                InitializeComponent();
    
                var y = GetValue(FilepathProperty);
                Console.WriteLine(y);
    
                this.Loaded += (sender, args) =>
                {
                    var x = GetValue(FilepathProperty);
                    Console.WriteLine(x);
                };
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多