首页 新闻 赞助 找找看

WPF中如何在代码中绑定到数组元素?

-1
悬赏园豆:100 [已解决问题] 解决于 2009-11-24 15:41

我有一个类

Code

界面是这样

Code
            bind.Mode = BindingMode.TwoWay;
bind.Source
= mc;
bind.Path
= //这里该怎么写?
Tx[i].SetBinding(TextBox.TextProperty, bind);

}
}
}

我以前的尝试中,无论如何都无法绑定到单一元素,最近一次尝试似乎解决了问题:

设置一个元素属性 int D(int i) 和一个数组属性 int[] DArray

再 bind.Path = "DArray[" & i & "]"

可以解决,如果改成 bind.Path = "D[" & i & "]" 则仍然无法建立绑定,似乎意味着绑定数组元素是做不到的?

虽然勉强达到目的,但还是有风险,因为绑定到数组,将无法触发属性 D 中的 set 过程,必须在属性 DArray 的 set 中对数组中每个元素重新赋值来触发,而 somecode 是相当多的语句,这就白白多出来一堆不必要的开销,而且以后还会涉及到交错数组,开销更大。

哪位高人知道如何做才能抛弃 DArray,直接绑定到 D[i]?(需要在 C# 代码中而不是在 XAML 中完成,如果能提供相应的 VB.NET 代码就更好了)

感谢万分!

问题补充: 郁闷了,终于有条件拿C#写了一下,原来C#里面就不支持普通带参属性,还是请同学们把我的代码看成VB.NET代码来分析吧 回 Galactica:是这个目的
孔雀的主页 孔雀 | 初学一级 | 园豆:49
提问于:2009-11-10 11:56
< >
分享
最佳答案
1

楼主的目的是不是想把一个数组中的每个元素都绑定到另一个数组中的每个TextBox控件的Text属性上?

 

<Window x:Class="WpfDataBindingDemo.MainWindow"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="MainWindow" Height="347" Width="499">
<Grid>
<ItemsControl x:Name="theTextBoxGroup"
ItemsSource
="{Binding Path=ValueCollection}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Height="23" Margin="60,20,300,0"
Text
="{Binding Path=Value, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>

绑定的类,使用ObservableCollection<T>定义集合属性,集合元素用带属性的类.

public class DataModel
    {
        public ObservableCollection<StringItem> ValueCollection
        {
            get;
            set;
        }

        public DataModel()
        {
        }
    }

    public class StringItem : INotifyPropertyChanged
    {
        private string _value;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        public Int32 Index { get; set; }

        public string Value
        {
            get { return this._value; }
            set
            {
                if (this._value == value)
                    return;

                this._value = value;
                NotifyPropertyChanged("Value");

                MessageBox.Show(string.Format("{0:D4}-{1}", Index, Value));
            }
        }
    }


在程序中绑定数据源:
DataModel bindingModel = new DataModel
            {
                // 初始化集合,为集合添加项
                ValueCollection = new ObservableCollection<StringItem>
                {
                    // 添加三项,每个项添加的时候通过Index指定顺序
                    // Index值用以区分Value更改时,StringItem在集合
                    // 中的位置
                    new StringItem {Index =0,  Value = "第一项初始值" },
                    new StringItem {Index =1, Value = "第二项初始值" },
                    new StringItem {Index =2, Value = "第三项初始值" },
                }
            };

this.theTextBoxGroup.DataContext = bindingModel;

收获园豆:100
Launcher | 高人七级 |园豆:45045 | 2009-11-10 15:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册