服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - ItemsControl 数据绑定的两种方式

ItemsControl 数据绑定的两种方式

2022-11-04 11:50Hello——寻梦者! C#

这篇文章主要介绍了ItemsControl 数据绑定的两种方式,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下

     最近在学习ItemsControl这个控件的时候,查看了MSDN上面的一个例子,并且自己做了一些修改,这里主要使用了两种方式来进行相应的数据绑定,一种是使用DataContext,另外一种是直接将一个类绑定到前台,其实这两种方式原理差不多都是将数据模型的对象添加到一个ObservableCollection集合中,然后再绑定到前台,下面分别介绍两种绑定方式:

第一种

是将数据存储在一个ObservableCollection集合中,然后作为ItemsControl的DataContext对象,下面分别贴出相关的代码: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<Window x:Class="TestGrid.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:TestGrid"
  Title="MainWindow" Height="350" Width="525">
 <Grid>
  <ItemsControl Margin="10" x:Name="myItemsControl" ItemsSource="{Binding}">  
   <ItemsControl.Template>
    <ControlTemplate TargetType="ItemsControl">
     <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
      <ItemsPresenter/>
     </Border>
    </ControlTemplate>
   </ItemsControl.Template>  
   <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
     <WrapPanel/>
    </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>  
   <ItemsControl.ItemTemplate>
    <DataTemplate DataType="{ x:Type local:DataSource}">
     <DataTemplate.Resources>
      <Style TargetType="TextBlock">
       <Setter Property="FontSize" Value="18"/>
       <Setter Property="HorizontalAlignment" Value="Center"/>
      </Style>
     </DataTemplate.Resources>
     <Grid>     
      <Rectangle Fill="Green"/>     
      <Ellipse Fill="Red"/>
      <StackPanel>
       <TextBlock Margin="3,3,3,0" Text="{Binding Path=Priority,Mode=TwoWay}"/>
       <TextBlock Margin="3,0,3,7" Text="{Binding Path=TaskName,Mode=TwoWay}"/>
      </StackPanel>
     </Grid>
    </DataTemplate>
   </ItemsControl.ItemTemplate
   <ItemsControl.ItemContainerStyle>
    <Style>
     <Setter Property="Control.Width" Value="100"/>
     <Setter Property="Control.Margin" Value="5"/>
     <Style.Triggers>
      <Trigger Property="Control.IsMouseOver" Value="True">
       <Setter Property="Control.ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content.Description}"/>
      </Trigger>
     </Style.Triggers>
    </Style>
   </ItemsControl.ItemContainerStyle>
  </ItemsControl>
 </Grid>
</Window>

    这里需要注意的地方是 ItemsSource="{Binding}"这句必须添加,否则后台的数据是无法添加到前台的,这个需要注意,这里贴出后台的代码 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace TestGrid
{
 /// <summary>
 /// MainWindow.xaml 的交互逻辑
 /// </summary>
 public partial class MainWindow : Window
 {
  private ObservableCollection<DataSource> item = null;
  public MainWindow()
  {
   InitializeComponent();
   item = new ObservableCollection<DataSource>();
   item.Add(
    new DataSource()
    {
     Priority = "1",
     TaskName = "hello"
    }
    );
   item.Add(
     new DataSource()
    {
     Priority = "2",
     TaskName = "whats"
    }
    );
   item.Add(
    new DataSource()
    {
     Priority = "3",
     TaskName = "your"
    }
    );
   item.Add(
    new DataSource()
    {
     Priority = "4",
     TaskName = "name"
    }
    );
   item.Add(
    new DataSource()
    {
     Priority = "5",
     TaskName = "can"
    }
    );
   item.Add(
    new DataSource()
    {
     Priority = "6",
     TaskName = "you"
    }
    );
   this.myItemsControl.DataContext = item;
 
  }
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace TestGrid
{
 /// <summary>
 /// MainWindow.xaml 的交互逻辑
 /// </summary>
 public partial class MainWindow : Window
 {
  private ObservableCollection<DataSource> item = null;
  public MainWindow()
  {
   InitializeComponent();
   item = new ObservableCollection<DataSource>();
   item.Add(
    new DataSource()
    {
     Priority = "1",
     TaskName = "hello"
    }
    );
   item.Add(
     new DataSource()
    {
     Priority = "2",
     TaskName = "whats"
    }
    );
   item.Add(
    new DataSource()
    {
     Priority = "3",
     TaskName = "your"
    }
    );
   item.Add(
    new DataSource()
    {
     Priority = "4",
     TaskName = "name"
    }
    );
   item.Add(
    new DataSource()
    {
     Priority = "5",
     TaskName = "can"
    }
    );
   item.Add(
    new DataSource()
    {
     Priority = "6",
     TaskName = "you"
    }
    );
   this.myItemsControl.DataContext = item;
 
  }
 }
}

  这里最重要的一句就是 this.myItemsControl.DataContext = item;这个是将刚才的集合绑定到ItemsControl控件上,这里需要我们的注意。

第二种

  另外一种方式的数据绑定就是将一个类绑定到这个ItemsControl控件上,具体的方式如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<Window x:Class="TestGrid.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:TestGrid"
  Title="MainWindow" Height="350" Width="525">
 <Window.Resources>
  <local:MyData x:Key="myDataSource"/>
 </Window.Resources>
 <Grid>
  <ItemsControl Margin="10" x:Name="myItemsControl" ItemsSource="{Binding Source={StaticResource myDataSource}}">  
   <ItemsControl.Template>
    <ControlTemplate TargetType="ItemsControl">
     <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
      <ItemsPresenter/>
     </Border>
    </ControlTemplate>
   </ItemsControl.Template>  
   <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
     <WrapPanel/>
    </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>  
   <ItemsControl.ItemTemplate>
    <DataTemplate DataType="{ x:Type local:DataSource}">
     <DataTemplate.Resources>
      <Style TargetType="TextBlock">
       <Setter Property="FontSize" Value="18"/>
       <Setter Property="HorizontalAlignment" Value="Center"/>
      </Style>
     </DataTemplate.Resources>
     <Grid>     
      <Rectangle Fill="Green"/>     
      <Ellipse Fill="Red"/>
      <StackPanel>
       <TextBlock Margin="3,3,3,0" Text="{Binding Path=Priority,Mode=TwoWay}"/>
       <TextBlock Margin="3,0,3,7" Text="{Binding Path=TaskName,Mode=TwoWay}"/>
      </StackPanel>
     </Grid>
    </DataTemplate>
   </ItemsControl.ItemTemplate
   <ItemsControl.ItemContainerStyle>
    <Style>
     <Setter Property="Control.Width" Value="100"/>
     <Setter Property="Control.Margin" Value="5"/>
     <Style.Triggers>
      <Trigger Property="Control.IsMouseOver" Value="True">
       <Setter Property="Control.ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content.Description}"/>
      </Trigger>
     </Style.Triggers>
    </Style>
   </ItemsControl.ItemContainerStyle>
  </ItemsControl>
 </Grid>
</Window>

这里我们通过资源来引用一个类,让后使用  ItemsSource="{Binding Source={StaticResource myDataSource}}"将这个类绑定到ItemsSource控件上面,这里贴出相关的代码,我们定义了一个MyData的类,将该类作为数据源绑定到前台上,这个类的代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace TestGrid
{
 public class MyData:ObservableCollection<DataSource>
 {
  public MyData()
  {
   Add (new DataSource()
    {
     Priority = "1",
     TaskName = "My"
    }
    );
   Add(new DataSource()
    {
     Priority = "2",
     TaskName = "Name"
    }
    );
   Add(new DataSource()
    {
     Priority = "3",
     TaskName = "Is"
    }
    );
   Add(new DataSource()
    {
     Priority = "4",
     TaskName = "Ye"
    }
    );
   Add(new DataSource()
    {
     Priority = "5",
     TaskName = "Bo"
    }
    );
  
  }
  
 }
}

  这里面很重要的一部就是让这个类继承自 ObservableCollection<DataSource>,然后来添加相应的数据源,我们在使用继承的时候需要注意这些技巧。

  其实这两种情况都是将一个数据集合绑定到前台,只不过是一些绑定的方式有所不同,实际的原理都是一样的!

以上就是ItemsControl 数据绑定的两种方式的详细内容,更多关于ItemsControl 数据绑定的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/seekdream/p/4733398.html

延伸 · 阅读

精彩推荐
  • C#C# httpwebrequest访问HTTPS错误处理方法

    C# httpwebrequest访问HTTPS错误处理方法

    下面小编就为大家带来一篇C# httpwebrequest访问HTTPS错误处理方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网4512021-12-22
  • C#C#事件订阅发布实现原理详解

    C#事件订阅发布实现原理详解

    这篇文章主要介绍了C#事件订阅发布实现原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考...

    David Huang6722022-10-19
  • C#SuperSocket封装成C#类库的步骤

    SuperSocket封装成C#类库的步骤

    这篇文章主要介绍了SuperSocket封装成C#类库的步骤,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    xhubobo3732022-10-26
  • C#C#如何生成唯一订单号

    C#如何生成唯一订单号

    这篇文章主要为大家详细介绍了C#如何生成唯一订单号,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    一路狂奔的我10882022-02-28
  • C#Unity切割图集转换为多张图片

    Unity切割图集转换为多张图片

    这篇文章主要为大家详细介绍了Unity切割图集转换为多张图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    木小星10332022-09-24
  • C#C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法

    C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法

    这篇文章主要介绍了C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法,结合实例形式分析了Windows API函数FlashWindowEx的功能、定义及实现窗口闪烁的相关技...

    微wx笑11202021-12-03
  • C#C# 模拟浏览器并自动操作的实例代码

    C# 模拟浏览器并自动操作的实例代码

    这篇文章主要介绍了C# 模拟浏览器并自动操作的实例代码,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    Alan.hsiang5032022-09-23
  • C#C#中winform中panel重叠无法显示问题的解决

    C#中winform中panel重叠无法显示问题的解决

    这篇文章主要介绍了C#中winform中panel重叠无法显示问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    随风6666669442022-08-07