5.02 WPF的 Combox、ListBox,slider、ProgressBar使用

  1. 关于Combox\ListBox使用:

1.1 内容绑定有两种方法,

优先使用方法1,因为列表变化的时候,Combox会自动显示新的内容。而方法2并不会实时更新。

方法1:使用DataContext

this.comboBox1.DisplayMemberPath = "name"; //显示的内容

this.comboBox1.SelectedValuePath = "address"; //SelectedValue对应的列

this.comboBox1.DataContext = m_Person2;

在xml中需要增加如下一句话:ItemsSource="{Binding}"

方法2:使用ItemsSource

this.comboBox.DisplayMemberPath = "name";

this.comboBox.SelectedValuePath = "address";

this.comboBox.ItemsSource = m_Person;

【备注】

1.例子中的name,address必须是 属性字段。至于Person是类或者结构体没有关系。

2.可以通过SelectedItem 得到选择的对象,或者SelectedValue直接得到选择对象中绑定的值。

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

if (this.comboBox1.SelectedIndex > -1)

{

var item= (this.comboBox1.SelectedItem) as Person;

MessageBox.Show(item.address.ToString());

// MessageBox.Show(this.comboBox.SelectedValue.ToString());

}

}

1.2 具体代码:

cs 复制代码
 public class Person
 {
     public string name { get; set; }
     public string address { get; set; }
 }
 public List<Person> m_Person = new List<Person>();
 public List<Person> m_Person2 = new List<Person>();


m_Person.Add(new Person { name = "mike1", address = "天街1号" });
m_Person.Add(new Person { name = "mike2", address = "天街2号" });
m_Person.Add(new Person { name = "mike3", address = "天街3号" });
m_Person.Add(new Person { name = "mike4", address = "天街4号" });
this.comboBox.DisplayMemberPath = "name";
this.comboBox.SelectedValuePath = "address";
this.comboBox.ItemsSource = m_Person;   //方法1

 m_Person2.Add(new Person { name = "marry1", address = "银河系1号" });
 m_Person2.Add(new Person { name = "marry2", address = "银河系2号" });
 m_Person2.Add(new Person { name = "marry3", address = "银河系3号" });
 m_Person2.Add(new Person { name = "marry4", address = "银河系4号" });
 this.comboBox1.DisplayMemberPath = "name";
 this.comboBox1.SelectedValuePath = "address";
 this.comboBox1.DataContext = m_Person2;  //方法2


 private void button_Click(object sender, RoutedEventArgs e)
 {
     m_Person.Add(new Person { name = "mike5", address = "天街5号" });
     m_Person2.Add(new Person { name = "marry5", address = "银河系5号" });

 }

界面:

XML 复制代码
 <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="143,149,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox_SelectionChanged"/>
 <ComboBox x:Name="comboBox1" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="153,217,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged"/>

二、Slider

slider比较关键的参数是:

Minimum="0" Maximum="100" SmallChange="1" Orientation="Horizontal" TickPlacement="Both"

注意:对于label要实施显示这个slider的数值,可以用下列办法,即把label绑定到这个slider上。

Content="{Binding ElementName=slider, Path=Value, Mode=OneWay}"

XML 复制代码
  <Slider x:Name="slider" HorizontalAlignment="Left" Margin="359,103,0,0" VerticalAlignment="Top" Width="217" RenderTransformOrigin="0.5,0.5" Height="39"
          Minimum="0" Maximum="100" SmallChange="1"  Orientation="Horizontal"  TickPlacement="Both" >

  </Slider>
  <Label x:Name="label" Content="{Binding ElementName=slider, Path=Value, Mode=OneWay}" HorizontalAlignment="Left" Margin="370,142,0,0" VerticalAlignment="Top"/>

三、ProgressBar

比较关键的参数是:

Minimum="0" Maximum="100" Orientation="Horizontal"

另外:IsIndeterminate=true时,进度条将一直在动,含义是加载中。

<ProgressBar IsIndeterminate="true" Height="10" Minimum="0" Maximum="100" Width="100" Value="40"/>

备注:如果希望按了按钮后,进度条陆续移动,可以用如下方法实现:

cs 复制代码
private void button1_Click(object sender, RoutedEventArgs e)
{
    int start = (int)this.pbar1.Minimum;
    int end= (int)this.pbar1.Maximum;
    new Task(()=>
    {               
        for (int i = start; i < end; i++)
        {
            this.pbar1.Dispatcher.Invoke(()=> {
                this.pbar1.Value = i;
            });
            Thread.Sleep(100);
        }
    }).Start();      
}
相关推荐
wangnaisheng1 天前
【WPF】Opacity 属性的使用
wpf
姬激薄2 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)2 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心2 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo3 天前
9.1.领域驱动设计
wpf
大道随心3 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠3 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go3 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet3 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf
冰茶_3 天前
WPF之绑定模式深入
学习·microsoft·微软·c#·wpf·绑定模式