欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Adding buttons to databound listbox items in ...

Adding buttons to databound listbox items in WPF

Introduction

In this tutorial I will demonstrate how to create a listbox in WPF which is databound to a collection, we then would like to add a button to each item in the listbox. Clicking this button will button will delete that item from the collection.

Making an ObservableCollection

We create a small class which represents a User with a Name and Age:

public class User
{

public string Name { get; set; }
public
int Age { get; set; }

}

In the code-behind of our MainWindow we the create a new ObservableCollection of users and populate when the app starts:


public ObservableCollection<User> Users;

public MainWindow()
{

InitializeComponent();
Users = new ObservableCollection<User>() {

new User() { Name = “Tim”, Age = 30 },
new User() { Name = “Marc”, Age = 48 },
new User() { Name = “Ann”, Age = 39 },
new User() { Name = “Jan”, Age = 25 },
new User() { Name = “Marie”, Age = 69 }};

}

Creating a databound listbox

We then add a listbox to our application which we name lbUsers and we state that the ItemsSource should use the datacontext of the control itself.

<ListBox IsSynchronizedWithCurrentItem=”True”

Name=”lbUsers” ItemsSource=”{Binding}”
ItemTemplate=”{StaticResource UserTemplate}” />

We then create the specified ItemTemplate that will visualize each user in the listbox. Each item will simply sow the name and age of the user, followed by a ‘Delete’ button.

<Window.Resources>

<DataTemplate x:Key=”UserTemplate” >

<StackPanel Orientation=”Horizontal” >

<TextBlock Text=”{Binding Path=Name}” Width=”50″/>
<TextBlock Text=”{Binding Path=Age}” Width=”20″/>

<Button Content=”Delete” Click=”cmdDeleteUser_Clicked”/>

</StackPanel>

</DataTemplate>

</Window.Resources>

Back in our code-behind we now only have to change the datacontext of the listbox (do this right after you populated the listbox for example)

lbUsers.DataContext = Users;

If you now run the application you should see a populated listbox. The delete button won’t work of course.

Making the buttons works

We now have to write the eventhandler code when the user clicks on a delete-button. Believe it or not, this is very straightforward:

private void cmdDeleteUser_Clicked(object sender, RoutedEventArgs e)
{

Button cmd = (Button)sender;
if
(cmd.DataContext is User)
{

User deleteme = (User)cmd.DataContext;
Users.Remove(deleteme);

}

}

We know the sender that triggers this will be a Button so we bluntly cast it to a Button. Next we checkthat the DataContext of the clicked button is indeed from a User (and not some other object as a result of bad coding J ).

If it is, we can simply remove that given user from the listbox. Since our collection, from which we remove the user, is an ObservableCollection the change will automatically be shown in the listbox on screen.

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
解決 WPF 綁定集合后數據變動(dòng)界面卻不更新的問(wèn)題
無(wú)廢話(huà)WPF系列9: Binding的源
WPF 數據綁定實(shí)例一
silverlight教程(6)——使用用戶(hù)管制,以落實(shí)主/詳細情況
INotifyPropertyChanged 接口用法
python 圖形界面 - Tkinter
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久