这个问题不太可能帮助任何 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/