C# 如何使自定义XAML用户控件可绑定?
我正在做一个简单的演示来学习如何创建一个可绑定的用户控件。我创建了一个简单的类:C# 如何使自定义XAML用户控件可绑定?,c#,wpf,xaml,C#,Wpf,Xaml,我正在做一个简单的演示来学习如何创建一个可绑定的用户控件。我创建了一个简单的类: class Person { public string firstName; public string lastName; public Person(string first, string last) { firstName = first; lastName = last; } } 以及一个非常简单的用户控件: <Us
class Person
{
public string firstName;
public string lastName;
public Person(string first, string last)
{
firstName = first;
lastName = last;
}
}
以及一个非常简单的用户控件:
<UserControl x:Class="Example.ExampleHRControl"
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 x:Name="textFirstName"></TextBlock>
<TextBlock x:Name="textLastName"></TextBlock>
</Grid>
</UserControl>
然后我可以通过代码隐藏来解决它,并添加值:
Hr1.textFirstName.Text = "John";
Hr1.textLasttName.Text = "Doe";
我希望能够创建类
Person
的实例,并简单地将主窗口上的控件绑定到Person
类。您想要称之为依赖性属性的内容可以从xaml绑定到它1-创建字段
public static readonly DependencyProperty FirstNameProperty =
DependencyProperty.Register(
"FirstName", typeof(Strin),
2-创建属性
public String FirstName
{
get { return (String)GetValue(FirstNameProperty); }
set { SetValue(FirstNameProperty, value); }
}
3-您可以在XAML中使用它来绑定它,也可以直接使用它<local:YourControlName FirstName="john"/>
<local:YourControlName FirstName="{Binding MyFirstName}"/>
- 使用将帮助您生成干净的代码,并具有强大的IntelliSense功能
- 要想让这一切顺利进行,你需要做几件事
在代码隐藏中,为希望控件了解的Person对象添加依赖项属性:
public static readonly DependencyProperty PersonProperty =
DependencyProperty.Register("Person", typeof(Person),
typeof(ExampleHRControl));
public Person Person
{
get { return (Person)GetValue(PersonProperty); }
set { SetValue(PersonProperty, value); }
}
在XAML中,将代码设置为数据上下文,并将绑定添加到person对象:
<UserControl x:Class="Example.ExampleHRControl"
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"
x:Name="This">
<Grid>
<TextBlock x:Name="{Binding Path=Person.FirstName, ElementName=This}"/>
<TextBlock x:Name="{Binding Path=Person.LastName, ElementName=This}"/>
</Grid>
</UserControl>
现在,无论何时设置Person属性,您的控件都将使用与该Person关联的名字和姓氏进行自我更新。谢谢,这似乎是我所缺少的。我将努力把它们放在一起。如果可以的话,还有一个问题--我可以绑定整个控件而不是单个属性吗?不更改
DataContext
,尤其是如果这个UserControl
最终成为ContentControl
,这可能是有益的。解决这个问题的一个简单方法是命名用户控件,并通过ElementName
在绑定中引用它。看看这个原因。我已经做了我在上面的原始帖子中推荐的更改。
<UserControl x:Class="Example.ExampleHRControl"
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"
x:Name="This">
<Grid>
<TextBlock x:Name="{Binding Path=Person.FirstName, ElementName=This}"/>
<TextBlock x:Name="{Binding Path=Person.LastName, ElementName=This}"/>
</Grid>
</UserControl>