[英]WPF BInding selected Item Property of list box
我有以下情況我有計算機列表列出計算機;
和計算機具有以下屬性
Computer{
User current-user;
String name;
}
User{
Date-time Start Time;
Date Time Elapsed;
....}
我想有以下觀點
列表視圖中的計算機名稱列表
並在該計算機上選擇當前用戶的詳細信息時 ![在此處輸入圖像說明] [1]
我需要一些提示,如何在XAML 計算機上執行此操作, 並且用戶對象實現了Inotifypropertychanged接口,並且我的集合是Observable集合
這是一個客戶端服務器應用程序,因此我假設在上述條件下,UI將使用來自客戶端計算機的當前用戶信息進行更新。 謝謝!
您可以通過以下方式做到這一點:
1-將您的計算機列表綁定到列表框
<ListBox Name="ComputersListbox" ItemsSource={Binding YourPropertyListHere}>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text={Binding name}>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
將列表框的選定項綁定到網格布局的數據上下文:
<Grid Name="UserDetailGrid" DataContext={Binding SelectedItem.current-user,ElementName=ComputersListbox}><!--All the controls in the grid will consider their datasource is the SelectedItem.current-user in the listbox which is a computer-->
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text={Binding Elapsed} />
<TextBlock Grid.Column="1" Text={Binding StartTime} />
</Grid>
無論何時更改所選項目,網格中的數據都會被修改。 由於您實現了INotifyPropertyChanged,因此在更改視圖模型中的屬性時將更新視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.