1
我有一個問題:我有一個觀點:無法綁定的DependencyProperty到我的用戶
<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"));
}
和我嘗試在我看來,使用這一觀點與視圖模型:
<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>
視圖模型:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public string GoGoGo
{
get
{
return "Test message";
}
}
}
但每一次我得到了在輸出窗口下一條消息和時間沒有看到測試消息:
* System.Windows.Data錯誤:40:BindingExpression路徑錯誤: 'TestingTest' 屬性不在'object''UserControl1'(Name ='my')'上找到。 BindingExpression:路徑= TestingText; DataItem ='UserControl1'(Name ='my');目標元素是'TextBlock'(Name ='');目標屬性爲'Text'(類型'String') *
我該怎麼做?
Crabl,sanks ^) – 2012-04-02 16:57:06