如何:指定绑定源
在数据绑定中,绑定源对象是指从中获取数据的对象。 本主题介绍指定绑定源的不同方式。
例
如果要将多个属性绑定到公共源,则希望使用 DataContext
属性,该属性提供了一种方便的方法来建立一个范围,其中所有数据绑定属性都继承了一个公共源。
在以下示例中,数据上下文是在应用程序的根元素上建立的。 这允许所有子元素继承该数据上下文。 绑定的数据来自通过映射直接引用的自定义数据类 NetIncome
,该类被赋予了资源键 incomeDataSource
。
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SDKSample.DirectionalBinding"
xmlns:c="clr-namespace:SDKSample"
Name="Page1"
>
<Grid.Resources>
<c:NetIncome x:Key="incomeDataSource"/>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Padding" Value="8"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,6,0,0"/>
</Style>
</Grid.Resources>
<Grid.DataContext>
<Binding Source="{StaticResource incomeDataSource}"/>
</Grid.DataContext>
</Grid>
以下示例演示 NetIncome
类的定义。
public class NetIncome : INotifyPropertyChanged
{
private int totalIncome = 5000;
private int rent = 2000;
private int food = 0;
private int misc = 0;
private int savings = 0;
public NetIncome()
{
savings = totalIncome - (rent+food+misc);
}
public int TotalIncome
{
get
{
return totalIncome;
}
set
{
if( TotalIncome != value)
{
totalIncome = value;
OnPropertyChanged("TotalIncome");
}
}
}
public int Rent
{
get
{
return rent;
}
set
{
if( Rent != value)
{
rent = value;
OnPropertyChanged("Rent");
UpdateSavings();
}
}
}
public int Food
{
get
{
return food;
}
set
{
if( Food != value)
{
food = value;
OnPropertyChanged("Food");
UpdateSavings();
}
}
}
public int Misc
{
get
{
return misc;
}
set
{
if( Misc != value)
{
misc = value;
OnPropertyChanged("Misc");
UpdateSavings();
}
}
}
public int Savings
{
get
{
return savings;
}
set
{
if( Savings != value)
{
savings = value;
OnPropertyChanged("Savings");
UpdateSavings();
}
}
}
private void UpdateSavings()
{
Savings = TotalIncome - (Rent+Misc+Food);
if(Savings < 0)
{}
else if(Savings >= 0)
{}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler !=null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
Public Class NetIncome
Implements INotifyPropertyChanged
' Events
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
' Methods
Public Sub New()
Me._totalIncome = 5000
Me._rent = 2000
Me._food = 0
Me._misc = 0
Me._savings = 0
Me._savings = (Me.TotalIncome - ((Me.Rent + Me.Food) + Me.Misc))
End Sub
Private Sub OnPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Private Sub UpdateSavings()
Me.Savings = (Me.TotalIncome - ((Me.Rent + Me.Misc) + Me.Food))
If ((Me.Savings >= 0) AndAlso (Me.Savings >= 0)) Then
End If
End Sub
' Properties
Public Property Food As Integer
Get
Return Me._food
End Get
Set(ByVal value As Integer)
If (Me.Food <> value) Then
Me._food = value
Me.OnPropertyChanged("Food")
Me.UpdateSavings()
End If
End Set
End Property
Public Property Misc As Integer
Get
Return Me._misc
End Get
Set(ByVal value As Integer)
If (Me.Misc <> value) Then
Me._misc = value
Me.OnPropertyChanged("Misc")
Me.UpdateSavings()
End If
End Set
End Property
Public Property Rent As Integer
Get
Return Me._rent
End Get
Set(ByVal value As Integer)
If (Me.Rent <> value) Then
Me._rent = value
Me.OnPropertyChanged("Rent")
Me.UpdateSavings()
End If
End Set
End Property
Public Property Savings As Integer
Get
Return Me._savings
End Get
Set(ByVal value As Integer)
If (Me.Savings <> value) Then
Me._savings = value
Me.OnPropertyChanged("Savings")
Me.UpdateSavings()
End If
End Set
End Property
Public Property TotalIncome As Integer
Get
Return Me._totalIncome
End Get
Set(ByVal value As Integer)
If (Me.TotalIncome <> value) Then
Me._totalIncome = value
Me.OnPropertyChanged("TotalIncome")
End If
End Set
End Property
' Fields
Private _food As Integer
Private _misc As Integer
Private _rent As Integer
Private _savings As Integer
Private _totalIncome As Integer
End Class
说明
上面的示例实例化标记中的对象并将其用作资源。 如果要绑定到已在代码中实例化的对象,则需要以编程方式设置 DataContext
属性。 有关示例,请参阅使数据可用于 XAML 中的绑定。
或者,如果您想在单个绑定上明确设定数据源,您可以使用以下选项。 这些属性优先于继承的数据上下文。
财产 | 描述 |
---|---|
Source | 您可以使用此属性将源设置为一个对象的实例。 如果不需要建立多个属性继承相同数据上下文的范围的功能,则可以使用 Source 属性而不是 DataContext 属性。 有关详细信息,请参阅 Source。 |
RelativeSource | 当您希望指定与绑定目标位置相关的源时,这将非常有用。 一些常见的使用此属性的情形包括:将元素的一个属性绑定到同一元素的另一个属性,或者在样式或模板中定义绑定。 有关详细信息,请参阅 RelativeSource。 |
ElementName | 指定表示要绑定到的元素的字符串。 运用这种方法在需要绑定到应用程序中另一个元素的属性时非常有用。 例如,如果要使用 Slider 来控制应用程序中另一个控件的高度,或者要将控件的 Content 绑定到 ListBox 控件的 SelectedValue 属性。 有关详细信息,请参阅 ElementName。 |