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>

Wpf控件ListBox使用实例2

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

相关文章:

  • 2021-11-24
  • 2021-11-29
  • 2021-04-15
  • 2021-11-29
  • 2022-12-23
猜你喜欢
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
  • 2021-06-21
  • 2022-12-23
相关资源
相似解决方案