C# Wpf集合双向绑定

说明:

msdn中   ObservableCollection<T> 类    表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

在许多情况下,所使用的数据是对象的集合。 例如,数据绑定中的一个常见方案是使用 ItemsControl(如 ListBoxListView 或 TreeView)来显示记录的集合。

可以枚举实现 IEnumerable 接口的任何集合。 但是,若要设置动态绑定,以便集合中的插入或删除操作可以自动更新 UI,则该集合必须实现 INotifyCollectionChanged 接口。 此接口公开 CollectionChanged 事件,只要基础集合发生更改,都应该引发该事件。

WPF 提供 ObservableCollection<T> 类,它是实现 INotifyCollectionChanged 接口的数据集合的内置实现。

还有许多情况,我们所使用的数据只是单纯的字段或者属性,此时我们需要为这些字段或属性实现INotifyPropertyChanged接口,实现了该接口,只要字段或属性的发生了改变,就会提供通知机制。

实例:

Xaml指定TwoWay双向绑定

<Grid>
    <StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427">
        <TextBlock Height="23" Name="textBlock1" Text="学员编号:" />
        <TextBox Height="23" Name="txtStudentId" Width="301" HorizontalAlignment="Left"/>
        <TextBlock Height="23" Name="textBlock2" Text="学员列表:" />
        <ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Name="stackPanel2" Orientation="Horizontal">
                        <TextBlock  Text="{Binding Id,Mode=TwoWay}" Margin="5" Background="Beige"/>
                        <TextBlock Text="{Binding Name,Mode=TwoWay}" Margin="5"/>
                        <TextBlock  Text="{Binding Age,Mode=TwoWay}" Margin="5"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Button" Height="23" Name="button1" Width="75" HorizontalAlignment="Left" Click="button1_Click" />
    </StackPanel>
</Grid>

后台代码创建ObservableCollection<T>实例:

ObservableCollection<Students> infos = new ObservableCollection<Students>() {
    new Students(){ Id=1, Age=11, Name="Tom"},
    new Students(){ Id=2, Age=12, Name="Darren"},
    new Students(){ Id=3, Age=13, Name="Jacky"},
    new Students(){ Id=4, Age=14, Name="Andy"}
    };
public Bind7()
{
    InitializeComponent();

    this.lbStudent.ItemsSource = infos;
    this.txtStudentId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = lbStudent });
}
private void button1_Click(object sender, RoutedEventArgs e)
{
    infos[1] = new Students() { Id = 4, Age = 14, Name = "这是一个集合改变" };
    infos[2].Name = "这是一个属性改变";
}
public class Students
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

显示结果:

说明,ObservableCollection<T>只针对列表数据项修改进行通知,对于列表项属性修改不进行通知

如果想对于对象属性修改也通知到UI,需要类实现INotifyPropertyChanged接口

public class Students : INotifyPropertyChanged
{
    string _name;
    public int Id { get; set; }
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }
    public int Age { get; set; }
    protected internal virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

 

Xlua的Plugins是用来支持在Unity中使用Lua语言的插件。下面是使用Xlua Plugins的基本流程: 1. 下载Xlua插件 可以在Xlua官网下载最新版本的Xlua插件。 2. 将Xlua插件导入Unity项目 将下载好的Xlua插件导入到Unity项目中。 3. 创建Lua脚本 在Unity项目中创建一个.lua文件,编写Lua脚本代码。 4. 将Lua脚本打包成.bytes文件 使用luajit工具将Lua脚本打包成.bytes文件。将打包好的.bytes文件拖入Unity项目中。 5. 在C#脚本中调用Lua脚本 在C#脚本中使用Xlua插件提供的API调用Lua脚本。例如: ```csharp using XLua; using UnityEngine; public class LuaScript : MonoBehaviour { private LuaEnv luaEnv; private void Awake() { luaEnv = new LuaEnv(); luaEnv.DoString("require 'example'"); } private void OnDestroy() { luaEnv.Dispose(); } } ``` 以上代码创建了一个LuaEnv对象,在Awake方法中加载了Lua脚本文件example.lua,最后在OnDestroy方法中释放了LuaEnv对象。 6. 在Lua脚本中调用C#方法和属性 在Lua脚本中可以通过XLua提供的API调用C#方法和属性。例如: ```lua local gameObject = CS.UnityEngine.GameObject('TestObject') local transform = gameObject.transform transform.position = CS.UnityEngine.Vector3(0, 0, 0) local time = CS.UnityEngine.Time.deltaTime print(time) ``` 以上代码创建了一个GameObject对象,并且获取了它的Transform组件,设置了Transform组件的位置。最后打印了当前帧的时间。 这就是使用Xlua Plugins的基本流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值