绑定不适用于数组

时间:2015-01-05 04:32:52

标签: c# wpf data-binding

我有TreeView Control的数据模板,

        DataTemplate cardLayout = new DataTemplate();
        cardLayout.DataType = typeof(NumberSystem);

        FrameworkElementFactory Stack = new FrameworkElementFactory(typeof(StackPanel));
        Stack.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

        FrameworkElementFactory Data = new FrameworkElementFactory(typeof(TextBlock));
        MultiBinding b = new MultiBinding();
        for (var i = 0; i < Count; i++)
        {
            Binding nn = new Binding("bits["+(Count - 1 - i).ToString()+"]");
            b.Bindings.Add(nn);
        }
        b.Converter = new BitConverter();
        b.Mode = BindingMode.OneWay;
        b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b.NotifyOnSourceUpdated = true;
        Data.SetBinding(TextBlock.TextProperty, b);
        Stack.AppendChild(Data);

        cardLayout.VisualTree = Stack;

        Map.ItemTemplate = cardLayout;

        NumberSystem.Size=32;
        NumberSystem n=new NumberSystem("0AF45000");

Map是UI中TreeView控件的名称。数据文本块有多个绑定到Registers类**的字符数组。但问题是只有一次在元素初始化期间进行绑定。其他时候如果更改了char数组,则更改不会反映在UI中的TextBlock中。**

      public class BitConverter : IMultiValueConverter
      {
        public object Convert(object[] values, Type targetType,
           object parameter, System.Globalization.CultureInfo culture)
        {
        string binary="";
        for (var i =0;i< Registers.RegisterSize;i++)
        { 
            binary+=values[i].ToString();
        }
        string hex = System.Convert.ToString(System.Convert.ToInt32(binary, 2), 16);
        hex = hex.PadLeft(8, '0');
        return "0x"+hex.ToUpper();
        }

        public object[] ConvertBack(object value, Type[] targetTypes,
           object parameter, System.Globalization.CultureInfo culture)
        {
         return new NotImplementedException();
        }
     }

public class NumberSystem
{
    public static int Size;
    public string Data { get; set; }
    public char[] bits { get; set; }

    public char this[int index]
    {
        get { return bits[index]; }
        set
        {
            bits[index] = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Binding.IndexerName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public NumberSystem(string Data)
    {
        int i = Convert.ToInt32(registerData, 16);
        string s = Convert.ToString(i, 2).PadLeft(RegisterSize, '0');
        bits = new char[Size];
        for (var itr = 0; itr < Size; itr++)
        {
            bits[itr] = s.ElementAt(Size-1-itr);
        }
    }
}

0 个答案:

没有答案