none
WPF עזרה אם Binding RRS feed

  • שאלה

  • יש לי מחלקה של לוח שיש בה מטריצה של מחלקה משבצת
    אני רוצה לעשות Binding
    אם כל איבר במטריצה במחלקה של הלוח איך אני עושה את זה?האם זה אפשרי?
    תודה
    • הועבר על-ידי Arik Poznanski יום שישי 12 אוגוסט 2011 08:23 רלוונטי לפורום #C (מאת:פיתוח ב- .Net)
    • הועבר על-ידי Hengzhe Li יום רביעי 18 ינואר 2012 07:34 (מאת:Microsoft Visual C#)
    יום שני 15 נובמבר 2010 09:54

תשובות

  • Following is an example.

    The matrix class contains the matrix data.
    Note that I provide a FlatData property which returns the data as a flat list.

    On the loaded event of the form I create the textbox UI and set the binding.

     

    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace BindingLoop
    {
      public class Matrix
      {
        public const int MatrixSize = 3;
        private int[,] _data;
    
        public Matrix()
        {
          // set matrix data
          _data = new int[MatrixSize, MatrixSize];
          for (int i = 0; i < MatrixSize; ++i)
          {
            for (int j = 0; j < MatrixSize; ++j)
            {
              _data[i, j] = i * MatrixSize + j;
            }
          }
        }
    
        // get flat list
        public List<int> FlatData
        {
          get
          {
            return (from int i in _data
                select i)
                .ToList();
          }
        }
      }
    
      /// <summary>
      /// Interaction logic for MainWindow.xaml
      /// </summary>
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
          // set data context for uniformgrid
          LayoutRoot.DataContext = new Matrix();
          for (int i = 0; i < Matrix.MatrixSize; ++i)
          {
            for (int j = 0; j < Matrix.MatrixSize; ++j)
            {
              // create textbox
              TextBox currentTextBox = new TextBox();
    
              // add textbox to uniformgrid
              LayoutRoot.Children.Add(currentTextBox);
              currentTextBox.SetValue(Grid.ColumnProperty, i);
              currentTextBox.SetValue(Grid.RowProperty, j);
    
              // set binding
              int c = i * Matrix.MatrixSize + j;
              Binding currentBinding = new Binding("FlatData[" + c.ToString() + "]");
              currentTextBox.SetBinding(TextBox.TextProperty, currentBinding);
            }
          }
        }
      }
    }
    
    

    Arik Poznanski
    blogs.microsoft.co.il/blogs/arik

    • הוצע כתשובה על-ידי Arik Poznanski יום שני 15 נובמבר 2010 12:19
    • סומן כתשובה על-ידי Meir Pinto יום שלישי 11 ינואר 2011 06:05
    יום שני 15 נובמבר 2010 12:19

כל התגובות

  • את יכולה לפרט יותר מה רכיב הUI שאליו את רוצה לעשות Binding?

    את יכולה לחבר את המטריצה לListBox בעל panel מתאים שיציג את הפריטים בצורה שאת רוצה.
    לצורך זה תצטרכי כנראה לחשוף רשימה שטוחה של הפריטים מתוך המחלקה של הלוח.

    אם את מנסה לחבר את המטריצה שלך למספר רכיבי UI שונים אז אין מנוס מלקבוע binding נפרד לכל רכיב.
    אפשר לעשות את זה יחסית בקלות בקוד שרץ בלולאה על רכיבי הUI שלך.


    Arik Poznanski
    blogs.microsoft.co.il/blogs/arik

    יום שני 15 נובמבר 2010 11:10
  • אתה יכול לתת לי דוגמה איך לחבר מספר לרכיבים שונים בלולאה


    תודה!!

    • הוצע כתשובה על-ידי Arik Poznanski יום שני 15 נובמבר 2010 12:16
    יום שני 15 נובמבר 2010 11:41
  • Following is an example.

    The matrix class contains the matrix data.
    Note that I provide a FlatData property which returns the data as a flat list.

    On the loaded event of the form I create the textbox UI and set the binding.

     

    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace BindingLoop
    {
      public class Matrix
      {
        public const int MatrixSize = 3;
        private int[,] _data;
    
        public Matrix()
        {
          // set matrix data
          _data = new int[MatrixSize, MatrixSize];
          for (int i = 0; i < MatrixSize; ++i)
          {
            for (int j = 0; j < MatrixSize; ++j)
            {
              _data[i, j] = i * MatrixSize + j;
            }
          }
        }
    
        // get flat list
        public List<int> FlatData
        {
          get
          {
            return (from int i in _data
                select i)
                .ToList();
          }
        }
      }
    
      /// <summary>
      /// Interaction logic for MainWindow.xaml
      /// </summary>
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
          // set data context for uniformgrid
          LayoutRoot.DataContext = new Matrix();
          for (int i = 0; i < Matrix.MatrixSize; ++i)
          {
            for (int j = 0; j < Matrix.MatrixSize; ++j)
            {
              // create textbox
              TextBox currentTextBox = new TextBox();
    
              // add textbox to uniformgrid
              LayoutRoot.Children.Add(currentTextBox);
              currentTextBox.SetValue(Grid.ColumnProperty, i);
              currentTextBox.SetValue(Grid.RowProperty, j);
    
              // set binding
              int c = i * Matrix.MatrixSize + j;
              Binding currentBinding = new Binding("FlatData[" + c.ToString() + "]");
              currentTextBox.SetBinding(TextBox.TextProperty, currentBinding);
            }
          }
        }
      }
    }
    
    

    Arik Poznanski
    blogs.microsoft.co.il/blogs/arik

    • הוצע כתשובה על-ידי Arik Poznanski יום שני 15 נובמבר 2010 12:19
    • סומן כתשובה על-ידי Meir Pinto יום שלישי 11 ינואר 2011 06:05
    יום שני 15 נובמבר 2010 12:19
  •  

    היי,

    אשמח אם תוכל/י לעדכן אותנו בסטטוס השאלה שלך.

    במידה וקיבלת תשובה מתאימה לשאלתך, יש לסמן את התשובה המתאימה ע"י לחיצה על "סמן כתשובה" ליד סימון ה V הירוק.


    אם תגובתי פתרה את בעייתך - לחץ/י, על "סמן כתשובה" ליד סימן ה V הירוק.


    מיקרוסופט מציעה שירות זה ללא תשלום, עם מטרת סיוע למשתמשים, הגדלת מסד הידע הקשור בטכנולוגיות ובמוצרים של Microsoft. תוכן זה מתפרסם "כמות שהוא", הוא אינו מעיד על כל אחריות מצד מיקרוסופט.
    “Microsoft offers this service for free, with the purpose of helping users and enlarging the knowledge database related to Microsoft products and technologies. This content is provided "as is" and it does not imply any responsibility from Microsoft's part."
    יום ראשון 09 ינואר 2011 14:59