日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Wpf控件ListBox使用实例2

發布時間:2023/12/4 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Wpf控件ListBox使用实例2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2.Xaml綁定選擇結果

<StackPanel Orientation="Vertical"><TextBlock Margin="10,10,10,10" FontWeight="Bold"> Pick a color from below list</TextBlock><ListBox Name="mcListBox" Height="100" Width="100" Margin="10,10,0,0" HorizontalAlignment="Left" ><ListBoxItem>Orange</ListBoxItem><ListBoxItem>Green</ListBoxItem><ListBoxItem>Blue</ListBoxItem><ListBoxItem>Gray</ListBoxItem><ListBoxItem>LightGray</ListBoxItem><ListBoxItem>Red</ListBoxItem></ListBox><TextBox Height="23" Name="textBox1" Width="120" Margin="10,10,0,0" HorizontalAlignment="Left" ><TextBox.Text><Binding ElementName="mcListBox" Path="SelectedItem.Content"/></TextBox.Text></TextBox><Canvas Margin="10,10,0,0" Height="200" Width="200" HorizontalAlignment="Left"><Canvas.Background><Binding ElementName="mcListBox" Path="SelectedItem.Content"/></Canvas.Background></Canvas> </StackPanel>

3.綁定ListBox.Templete 模板內容

/// <summary> /// List3.xaml 的交互邏輯 /// </summary> public partial class List3 : Window {public List3(){InitializeComponent();listBox.ItemsSource = new List<UserItem>() {new UserItem(1,"張三",true),new UserItem(2,"李四",false),new UserItem(3,"王五",false),new UserItem(4,"趙六",true)};}private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e){Button btn = sender as Button;if (btn != null){Image img = btn.FindName("img") as Image;if (img != null){MessageBox.Show(img.Source.ToString());}}} } public class UserItem {public UserItem(int ID, string Name, bool IsActive){this.ID = ID;this.Name = Name;this.IsActive = IsActive;}public int ID { get; set; }public string Name { get; set; }public bool IsActive { get; set; }public string BackGround{get {return this.IsActive? "/images/1.jpg": "/images/2.jpg";}} } View Code

Xaml定義

<Grid><Grid.RowDefinitions><RowDefinition Height="33*"/><RowDefinition Height="236*"/></Grid.RowDefinitions><ListBox x:Name="listBox" Grid.Row="1" Margin="30"><ListBox.ItemTemplate><DataTemplate><Button MouseDoubleClick="Button_MouseDoubleClick"><Grid><Image x:Name="img" Source="{Binding Path=BackGround}" Width="50" Height="30"></Image><TextBlock Text="{Binding Path=Name}" Margin="70 10" FontSize="18" Foreground="Red" FontWeight="Bold"></TextBlock></Grid></Button></DataTemplate></ListBox.ItemTemplate></ListBox><Label x:Name="label" Content="List模板綁定" HorizontalAlignment="Left" Margin="21,6,0,0" VerticalAlignment="Top"/> </Grid>

顯示結果:

?

總結

以上是生活随笔為你收集整理的Wpf控件ListBox使用实例2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。