【问题标题】:WPF: Cannot bind to a custom controls dependency propertyWPF:无法绑定到自定义控件依赖属性
【发布时间】:2013-09-27 23:58:46
【问题描述】:

我创建了一个自定义控件,其中包含用于数据绑定的依赖属性。 然后,绑定的值应显示在文本框中。 此绑定工作正常。

当我实现自定义控件时出现问题。网格的数据上下文是一个简单的视图模型,其中包含一个用于绑定的 String 属性。

  1. 如果我将此属性绑定到标准 wpf 控件文本框,一切正常。
  2. 如果我将该属性绑定到我的自定义控件,则不会发生任何事情。

经过一些调试,我发现 SampleText 是在 CustomControl 中搜索的。当然那里不存在。 为什么在 CustomControl 中搜索我的属性,而不是像场景 1 中那样从 DataContext 中获取。

    <Window x:Class="SampleApplicatoin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:SampleApplication"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.DataContext>
            <controls:ViewModel/>
        </Grid.DataContext>
        <TextBox Text="{Binding SampleText}"/>
        <controls:CustomControl TextBoxText="{Binding SampleText}"/>
    </Grid>
</Window>

在自定义控件的 XAML 代码下方。 我使用 DataContext = Self 从后面的代码中获取依赖属性:

<UserControl x:Class="SampleApplication.CustomControl"
             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="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="87,133,0,0" TextWrapping="Wrap" Text="{Binding TextBoxText}" VerticalAlignment="Top" Width="120"/>
    </Grid>
</UserControl>

xaml.cs 文件只包含依赖属性:

public partial class CustomControl : UserControl
    {
        public static readonly DependencyProperty TextBoxTextProperty = DependencyProperty.Register("TextBoxText", typeof (String), typeof (CustomControl), new PropertyMetadata(default(String)));

        public CustomControl()
        {
            InitializeComponent();
        }

        public String TextBoxText
        {
            get { return (String) GetValue(TextBoxTextProperty); }
            set { SetValue(TextBoxTextProperty, value); }
        }
    }

感谢您对此的任何帮助。现在真的让我发疯了。

编辑:

我刚刚想到了两种可能的解决方案:

这是第一个(对我有用):

<!-- Give that child a name ... -->
<controls:ViewModel x:Name="viewModel"/>
<!-- ... and set it as ElementName -->
<controls:CustomControl TextBoxText="{Binding SampleText, ElementName=viewModel}"/>

第二个。这在我的情况下不起作用。我不知道为什么:

<controls:CustomControl TextBoxText="{Binding SampleText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:ViewModel}}}"/>
<!-- or -->
<controls:CustomControl TextBoxText="{Binding SampleText, RelativeSource={RelativeSource FindAncestor, AncestorType=controls:ViewModel}}"/>

【问题讨论】:

    标签: dependency-properties


    【解决方案1】:

    我也遇到过类似的情况。 就我而言,我通过在 ViewModel 的属性设置器中添加 OnPropertyChanged 来修复它。

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 2012-08-07
      • 2019-05-26
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多