【发布时间】:2009-02-09 09:45:12
【问题描述】:
我不知道如何根据Parameter 在UserControl 中设置Path:
用户控制:
<UserControl x:Class="WpfApplication3.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase">
<Grid>
<TextBox Text="{Binding Path=MyPath}"/>
</Grid>
</UserControl>
后面的代码:
public partial class TestControl : UserControl
{
public string MyPath
{
get { return (string)GetValue(MyPathProperty); }
set { SetValue(MyPathProperty, value); }
}
public static readonly DependencyProperty MyPathProperty =
DependencyProperty.Register("MyPath", typeof(string), typeof(TestControl), new UIPropertyMetadata(""));
}
以及我打算如何使用它:
<local:TestControl MyPath="FirstName"></local:TestControl>
DataContext会从父对象中获取,并包含一个User的类,里面有一个FirstName属性。
目标是拥有一个可以绑定到任何路径的用户控件。 我知道这一定非常简单,但我对这项技术非常陌生,我找不到解决方案。
【问题讨论】:
标签: c# wpf data-binding dependency-properties