wpf - 无法将 DependencyProperty 绑定(bind)到我的 UserControl

标签 wpf xaml mvvm

这个问题不太可能帮助任何 future 的访客;它只与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the help center .




9年前关闭。




我有下一个问题:我有一个观点:

<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" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock>
            test
            <TextBlock Text="{Binding TestingTest}"></TextBlock>
            test
        </TextBlock>
    </Grid>
</UserControl>

带有代码隐藏:
    public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public string TestingTest
    {
        get { return (string)GetValue(TestingTestProperty); }
        set { SetValue(TestingTestProperty, value); }
    }

    public static readonly DependencyProperty TestingTestProperty =
        DependencyProperty.Register("TestingTest", typeof(string), typeof(UserControl1), new UIPropertyMetadata("testing test"));

}

我尝试在 ViewModel 的 View 中使用此 View :
<Window x:Class="WpfApplication1.MainWindow" Name="wnd" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525">
<Grid>
    <WpfApplication1:UserControl1 TestingTest="{Binding ElementName=wnd, Path=Title}"></WpfApplication1:UserControl1>
</Grid>

View 模型:
    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public string GoGoGo
    {
        get
        {
            return "Test message";
        }
    }
}

但是每次我在输出窗口中收到下一条消息并且看不到测试消息时:

*System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“UserControl1”(名称=“我的”)上找不到“TestingTest”属性。绑定(bind)表达式:路径=测试文本; DataItem='UserControl1'(名称='我的');目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”)
*

我错了什么?

最佳答案

这是一个错字。

在您的 UserControl 上,您有属性 TestingTest在 WPF 上你有 TestingText ('s' 和 'x')

关于wpf - 无法将 DependencyProperty 绑定(bind)到我的 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9980223/

相关文章:

javascript - Kendo Grid MVVM 与 rowtemplate 绑定(bind)到事件错误

wpf - 将IEnumerable <T>与ObservableCollection <T>包装在一起

c# - 在 WPF 中使用 Unity Prism 在 TextBox 中键入时实时更新 TextBlock

c# - 在不维护数据库上下文的情况下检索和修改实体?

wpf - 列表框的选择错误,其中列表项是值类型/结构并包含重复项?

c# - 任何时候只能打开一个 ContentDialog

wpf - WindowsFormsHost 始终是 WPF 元素的最顶层

c# - 列表框 WP7 中的反向堆栈项目

c# - 是否可以为 Silverlight 中的静态资源提供类型转换器?

c# - 使用多层 MVVM 处理 TreeView 中的数据库事务和异常