我對WPF非常陌生,並且在創建綁定時嘗試使用Knockout JS作爲類比。我聽說WPF的綁定引擎更加強大,但我在Knockout中很容易陷入困境。WPF綁定到當前元素和父元素的屬性
假設我有一個具有地址集合的Person類。
public class Person
{
public string PersonId {get;set;}
public IList<Address> Addresses{get; set;}
public string FormattedName{get;set;}
}
public class Address
{
public string AddressId{get;set;}
public string Address1{get;set;}
public string City{get;set;}
public string State{get;set;}
public string Zip{get;set;}
}
假設一個頁面上,我的人集合和每個人,我想顯示所有的地址,並提供按鈕選擇一個地址。所以,我的網頁瀏覽模式是這樣的:
public class AddressSelection
{
public string PersonId{get;set;}
public string AddressId{get;set;}
}
public class PersonAddressSelectionViewModel
{
public IList<Person> People {get; set;}
public Person SelectedPerson {get;set;}
public Address SelectedAddress{get;set;}
public void SelectAddress(string personId, string addressId)
{
this.SelectedPerson = this.People.FirstOrDefault(x => x.PersonId == personId);
this.SelectedAddress = this.SelectedPerson?.Addresses.FirstOrDefault(x => x.AddressId == addressId);
}
public void SelectAddress(AddressSelection arg)
{ SelectAddress(arg.PersonId, arg.AddressId); }
}
現在,我想證明顯示了每個乘客的頭部,然後爲每個地址按鈕的UI。當選擇該按鈕時,應該觸發SelectAddress函數。但是,在XAML中,我不確定如何分配綁定以同時使用父元素和當前元素屬性;它是否可以從它們創建一個對象,或者甚至只是調用一個方法並從兩個方法傳遞參數。
在淘汰賽中,你只綁定功能和訪問父上下文,如:
<!-- ko foreach: $data.people -->
<h2 data-bind="text: formattedName"></h2>
<ul data-bind="foreach: $data.addresses">
<li>
<button data-bind="click: function() { $parents[1].selectAddress($parent.personId, $data.addressId); }">Select Address</button>
</li>
</ul>
<!-- /ko -->
我似乎無法弄清楚如何做同樣的事情在XAML。
<ItemsControl Grid.Row="2" ItemsSource="{x:Bind ViewModel.People, Mode=OneWay}"
HorizontalAlignment="Center" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Style="{StaticResource TextBlockSmallStyle}" VerticalAlignment="Center" Width="180" Text="{Binding FormattedName, Mode=OneWay}" />
<ItemsControl ItemsSource="{Binding Addresses}" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Width="180" Style="{StaticResource ButtonStyle}"
Content="{Binding Address1}"
Click="SelectAddressClicked" Tag="{Binding **what to put here**}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
在後面的代碼:
SelectAddressClicked(object sender, object args) {
((Button)sender).Tag as Address; // would prefer this to be something that has both the address id and person id
}
您不需要綁定到標籤。點擊按鈕的datacontext是地址。 ((Button)sender).DataContext as Address。 –
@MarkW感謝您的提示! – MPavlak