当前位置: 首页>>代码示例>>C#>>正文


C# DataGrid.SetBinding方法代码示例

本文整理汇总了C#中DataGrid.SetBinding方法的典型用法代码示例。如果您正苦于以下问题:C# DataGrid.SetBinding方法的具体用法?C# DataGrid.SetBinding怎么用?C# DataGrid.SetBinding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataGrid的用法示例。


在下文中一共展示了DataGrid.SetBinding方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BindItemsSource

        public void BindItemsSource()
        {
            // Create the original DataContext object
            StringsContainer stringsContainer1 = new StringsContainer();
            stringsContainer1.Strings = new ObservableCollection<string> { "first", "second", "third" };

            // Create the next object to be used as DataContext
            StringsContainer stringsContainer2 = new StringsContainer();
            stringsContainer2.Strings = new ObservableCollection<string> { "one", "two", "three", "four", "five" };

            // Create the DataGrid and setup its binding
            DataGrid dataGrid = new DataGrid();
            dataGrid.DataContext = stringsContainer1;
            Binding binding = new Binding("Strings");
            binding.Mode = BindingMode.OneWay;
            dataGrid.SetBinding(DataGrid.ItemsSourceProperty, binding);
            TestPanel.Children.Add(dataGrid);

            this.EnqueueCallback(delegate
            {
                Assert.AreEqual(stringsContainer1.Strings, dataGrid.ItemsSource, "ItemsSource was not set from the original DataContext");
                Assert.AreEqual(3, dataGrid.DataConnection.Count, "ItemsSource was not set from the original DataContext");
                dataGrid.DataContext = stringsContainer2;
            });
            this.EnqueueYieldThread();

            this.EnqueueCallback(delegate
            {
                Assert.AreEqual(stringsContainer2.Strings, dataGrid.ItemsSource, "ItemsSource did not change along with the DataContext");
                Assert.AreEqual(5, dataGrid.DataConnection.Count, "ItemsSource did not change along with the DataContext");
            });
            this.EnqueueTestComplete();
        }
开发者ID:kvervo,项目名称:HorizontalLoopingSelector,代码行数:33,代码来源:ItemsSource.cs

示例2: Get_TabControl_Items

 private static System.Collections.ObjectModel.ObservableCollection<object> Get_TabControl_Items()
 {
     System.Collections.ObjectModel.ObservableCollection<object> items = new System.Collections.ObjectModel.ObservableCollection<object>();
     // e_3 element
     TabItem e_3 = new TabItem();
     e_3.Name = "e_3";
     e_3.HorizontalContentAlignment = HorizontalAlignment.Stretch;
     e_3.Header = "Controls";
     // e_4 element
     Grid e_4 = new Grid();
     e_3.Content = e_4;
     e_4.Name = "e_4";
     RowDefinition row_e_4_0 = new RowDefinition();
     row_e_4_0.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_0);
     RowDefinition row_e_4_1 = new RowDefinition();
     row_e_4_1.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_1);
     RowDefinition row_e_4_2 = new RowDefinition();
     row_e_4_2.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_2);
     RowDefinition row_e_4_3 = new RowDefinition();
     row_e_4_3.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_3);
     RowDefinition row_e_4_4 = new RowDefinition();
     row_e_4_4.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_4);
     RowDefinition row_e_4_5 = new RowDefinition();
     row_e_4_5.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_5);
     RowDefinition row_e_4_6 = new RowDefinition();
     row_e_4_6.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_6);
     RowDefinition row_e_4_7 = new RowDefinition();
     row_e_4_7.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_7);
     RowDefinition row_e_4_8 = new RowDefinition();
     row_e_4_8.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_8);
     RowDefinition row_e_4_9 = new RowDefinition();
     row_e_4_9.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_9);
     ColumnDefinition col_e_4_0 = new ColumnDefinition();
     col_e_4_0.Width = new GridLength(1F, GridUnitType.Auto);
     e_4.ColumnDefinitions.Add(col_e_4_0);
     ColumnDefinition col_e_4_1 = new ColumnDefinition();
     e_4.ColumnDefinitions.Add(col_e_4_1);
     // e_5 element
     TextBlock e_5 = new TextBlock();
     e_4.Children.Add(e_5);
     e_5.Name = "e_5";
     e_5.VerticalAlignment = VerticalAlignment.Center;
     e_5.Text = "Button";
     // button1 element
     Button button1 = new Button();
     e_4.Children.Add(button1);
     button1.Name = "button1";
     button1.Height = 30F;
     button1.Width = 200F;
     button1.Margin = new Thickness(5F, 5F, 5F, 5F);
     button1.HorizontalAlignment = HorizontalAlignment.Left;
     button1.TabIndex = 1;
     button1.Content = "Button 1";
     button1.CommandParameter = "Click Button 1";
     Grid.SetColumn(button1, 1);
     Grid.SetRow(button1, 0);
     Binding binding_button1_Command = new Binding("ButtonCommand");
     button1.SetBinding(Button.CommandProperty, binding_button1_Command);
     // button2 element
     Button button2 = new Button();
     e_4.Children.Add(button2);
     button2.Name = "button2";
     button2.Height = 30F;
     button2.Width = 200F;
     button2.Margin = new Thickness(5F, 5F, 5F, 5F);
     button2.HorizontalAlignment = HorizontalAlignment.Left;
     button2.TabIndex = 2;
     button2.Content = "Button 2";
     button2.CommandParameter = "Click Button 2";
     Grid.SetColumn(button2, 1);
     Grid.SetRow(button2, 1);
     Binding binding_button2_IsEnabled = new Binding("ButtonEnabled");
     button2.SetBinding(Button.IsEnabledProperty, binding_button2_IsEnabled);
     Binding binding_button2_Command = new Binding("ButtonCommand");
     button2.SetBinding(Button.CommandProperty, binding_button2_Command);
     // buttonResult element
     TextBlock buttonResult = new TextBlock();
     e_4.Children.Add(buttonResult);
     buttonResult.Name = "buttonResult";
     buttonResult.HorizontalAlignment = HorizontalAlignment.Left;
     Grid.SetColumn(buttonResult, 1);
     Grid.SetRow(buttonResult, 2);
     Binding binding_buttonResult_Text = new Binding("ButtonResult");
     buttonResult.SetBinding(TextBlock.TextProperty, binding_buttonResult_Text);
     // e_6 element
     TextBlock e_6 = new TextBlock();
     e_4.Children.Add(e_6);
     e_6.Name = "e_6";
     e_6.VerticalAlignment = VerticalAlignment.Center;
     e_6.Text = "CheckBox";
//.........这里部分代码省略.........
开发者ID:Mike-EEE,项目名称:UI_Examples,代码行数:101,代码来源:BasicUI.xaml.cs

示例3: Get_TabControl_Items

 private static System.Collections.ObjectModel.ObservableCollection<object> Get_TabControl_Items()
 {
     System.Collections.ObjectModel.ObservableCollection<object> items = new System.Collections.ObjectModel.ObservableCollection<object>();
     // e_3 element
     TabItem e_3 = new TabItem();
     e_3.Name = "e_3";
     e_3.HorizontalContentAlignment = HorizontalAlignment.Stretch;
     e_3.Header = "Controls";
     // e_4 element
     Grid e_4 = new Grid();
     e_3.Content = e_4;
     e_4.Name = "e_4";
     RowDefinition row_e_4_0 = new RowDefinition();
     row_e_4_0.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_0);
     RowDefinition row_e_4_1 = new RowDefinition();
     row_e_4_1.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_1);
     RowDefinition row_e_4_2 = new RowDefinition();
     row_e_4_2.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_2);
     RowDefinition row_e_4_3 = new RowDefinition();
     row_e_4_3.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_3);
     RowDefinition row_e_4_4 = new RowDefinition();
     row_e_4_4.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_4);
     RowDefinition row_e_4_5 = new RowDefinition();
     row_e_4_5.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_5);
     RowDefinition row_e_4_6 = new RowDefinition();
     row_e_4_6.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_6);
     RowDefinition row_e_4_7 = new RowDefinition();
     row_e_4_7.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_7);
     RowDefinition row_e_4_8 = new RowDefinition();
     row_e_4_8.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_8);
     RowDefinition row_e_4_9 = new RowDefinition();
     row_e_4_9.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_9);
     RowDefinition row_e_4_10 = new RowDefinition();
     row_e_4_10.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_10);
     RowDefinition row_e_4_11 = new RowDefinition();
     row_e_4_11.Height = new GridLength(1F, GridUnitType.Auto);
     e_4.RowDefinitions.Add(row_e_4_11);
     ColumnDefinition col_e_4_0 = new ColumnDefinition();
     col_e_4_0.Width = new GridLength(1F, GridUnitType.Auto);
     e_4.ColumnDefinitions.Add(col_e_4_0);
     ColumnDefinition col_e_4_1 = new ColumnDefinition();
     e_4.ColumnDefinitions.Add(col_e_4_1);
     // e_5 element
     TextBlock e_5 = new TextBlock();
     e_4.Children.Add(e_5);
     e_5.Name = "e_5";
     e_5.VerticalAlignment = VerticalAlignment.Center;
     e_5.Text = "Button";
     // button1 element
     Button button1 = new Button();
     e_4.Children.Add(button1);
     button1.Name = "button1";
     button1.Height = 30F;
     button1.Width = 200F;
     button1.Margin = new Thickness(5F, 5F, 5F, 5F);
     button1.HorizontalAlignment = HorizontalAlignment.Left;
     button1.TabIndex = 1;
     button1.Content = "Button 1";
     button1.CommandParameter = "Click Button 1";
     Grid.SetColumn(button1, 1);
     Grid.SetRow(button1, 0);
     Binding binding_button1_Command = new Binding("ButtonCommand");
     button1.SetBinding(Button.CommandProperty, binding_button1_Command);
     // button2 element
     Button button2 = new Button();
     e_4.Children.Add(button2);
     button2.Name = "button2";
     button2.Height = 30F;
     button2.Width = 200F;
     button2.Margin = new Thickness(5F, 5F, 5F, 5F);
     button2.HorizontalAlignment = HorizontalAlignment.Left;
     button2.TabIndex = 2;
     button2.Content = "Button 2";
     button2.CommandParameter = "Click Button 2";
     Grid.SetColumn(button2, 1);
     Grid.SetRow(button2, 1);
     Binding binding_button2_IsEnabled = new Binding("ButtonEnabled");
     button2.SetBinding(Button.IsEnabledProperty, binding_button2_IsEnabled);
     Binding binding_button2_Command = new Binding("ButtonCommand");
     button2.SetBinding(Button.CommandProperty, binding_button2_Command);
     // buttonResult element
     TextBlock buttonResult = new TextBlock();
     e_4.Children.Add(buttonResult);
     buttonResult.Name = "buttonResult";
     buttonResult.HorizontalAlignment = HorizontalAlignment.Left;
     Grid.SetColumn(buttonResult, 1);
     Grid.SetRow(buttonResult, 2);
     Binding binding_buttonResult_Text = new Binding("ButtonResult");
     buttonResult.SetBinding(TextBlock.TextProperty, binding_buttonResult_Text);
//.........这里部分代码省略.........
开发者ID:EmptyKeys,项目名称:UI_Examples,代码行数:101,代码来源:BasicUI.xaml.cs

示例4: Get_TabControl_Items

 private static System.Collections.ObjectModel.ObservableCollection<object> Get_TabControl_Items() {
     System.Collections.ObjectModel.ObservableCollection<object> items = new System.Collections.ObjectModel.ObservableCollection<object>();
     // e_1 element
     TabItem e_1 = new TabItem();
     e_1.Name = "e_1";
     e_1.Header = "Players";
     // e_2 element
     Grid e_2 = new Grid();
     e_1.Content = e_2;
     e_2.Name = "e_2";
     // PlayersDataGrid element
     DataGrid PlayersDataGrid = new DataGrid();
     e_2.Children.Add(PlayersDataGrid);
     PlayersDataGrid.Name = "PlayersDataGrid";
     PlayersDataGrid.AutoGenerateColumns = false;
     DataGridTextColumn PlayersDataGrid_Col0 = new DataGridTextColumn();
     PlayersDataGrid_Col0.Header = "#";
     Binding PlayersDataGrid_Col0_b = new Binding("Number");
     PlayersDataGrid_Col0.Binding = PlayersDataGrid_Col0_b;
     PlayersDataGrid.Columns.Add(PlayersDataGrid_Col0);
     DataGridTextColumn PlayersDataGrid_Col1 = new DataGridTextColumn();
     PlayersDataGrid_Col1.Header = "Name";
     Binding PlayersDataGrid_Col1_b = new Binding("Name");
     PlayersDataGrid_Col1.Binding = PlayersDataGrid_Col1_b;
     PlayersDataGrid.Columns.Add(PlayersDataGrid_Col1);
     DataGridTextColumn PlayersDataGrid_Col2 = new DataGridTextColumn();
     PlayersDataGrid_Col2.Header = "GameJolt ID";
     Binding PlayersDataGrid_Col2_b = new Binding("GameJoltID");
     PlayersDataGrid_Col2.Binding = PlayersDataGrid_Col2_b;
     PlayersDataGrid.Columns.Add(PlayersDataGrid_Col2);
     DataGridTextColumn PlayersDataGrid_Col3 = new DataGridTextColumn();
     PlayersDataGrid_Col3.Header = "Location";
     Binding PlayersDataGrid_Col3_b = new Binding("LevelFile");
     PlayersDataGrid_Col3.Binding = PlayersDataGrid_Col3_b;
     PlayersDataGrid.Columns.Add(PlayersDataGrid_Col3);
     DataGridTextColumn PlayersDataGrid_Col4 = new DataGridTextColumn();
     PlayersDataGrid_Col4.Header = "Play Time";
     Binding PlayersDataGrid_Col4_b = new Binding("PlayTime");
     PlayersDataGrid_Col4.Binding = PlayersDataGrid_Col4_b;
     PlayersDataGrid.Columns.Add(PlayersDataGrid_Col4);
     DataGridTextColumn PlayersDataGrid_Col5 = new DataGridTextColumn();
     PlayersDataGrid_Col5.Header = "IP";
     Binding PlayersDataGrid_Col5_b = new Binding("IP");
     PlayersDataGrid_Col5.Binding = PlayersDataGrid_Col5_b;
     PlayersDataGrid.Columns.Add(PlayersDataGrid_Col5);
     DataGridTextColumn PlayersDataGrid_Col6 = new DataGridTextColumn();
     PlayersDataGrid_Col6.Header = "Ping";
     Binding PlayersDataGrid_Col6_b = new Binding("Ping");
     PlayersDataGrid_Col6.Binding = PlayersDataGrid_Col6_b;
     PlayersDataGrid.Columns.Add(PlayersDataGrid_Col6);
     Grid.SetColumnSpan(PlayersDataGrid, 2);
     Binding binding_PlayersDataGrid_ItemsSource = new Binding("PlayersGridDataList");
     PlayersDataGrid.SetBinding(DataGrid.ItemsSourceProperty, binding_PlayersDataGrid_ItemsSource);
     items.Add(e_1);
     // e_3 element
     TabItem e_3 = new TabItem();
     e_3.Name = "e_3";
     e_3.Header = "Bans";
     // e_4 element
     Grid e_4 = new Grid();
     e_3.Content = e_4;
     e_4.Name = "e_4";
     // BansDataGrid element
     DataGrid BansDataGrid = new DataGrid();
     e_4.Children.Add(BansDataGrid);
     BansDataGrid.Name = "BansDataGrid";
     BansDataGrid.AutoGenerateColumns = false;
     DataGridTextColumn BansDataGrid_Col0 = new DataGridTextColumn();
     BansDataGrid_Col0.Header = "#";
     Binding BansDataGrid_Col0_b = new Binding("Number");
     BansDataGrid_Col0.Binding = BansDataGrid_Col0_b;
     BansDataGrid.Columns.Add(BansDataGrid_Col0);
     DataGridTextColumn BansDataGrid_Col1 = new DataGridTextColumn();
     BansDataGrid_Col1.Header = "Name";
     Binding BansDataGrid_Col1_b = new Binding("Name");
     BansDataGrid_Col1.Binding = BansDataGrid_Col1_b;
     BansDataGrid.Columns.Add(BansDataGrid_Col1);
     DataGridTextColumn BansDataGrid_Col2 = new DataGridTextColumn();
     BansDataGrid_Col2.Header = "GameJolt ID";
     Binding BansDataGrid_Col2_b = new Binding("GameJoltID");
     BansDataGrid_Col2.Binding = BansDataGrid_Col2_b;
     BansDataGrid.Columns.Add(BansDataGrid_Col2);
     DataGridTextColumn BansDataGrid_Col3 = new DataGridTextColumn();
     BansDataGrid_Col3.Header = "IP";
     Binding BansDataGrid_Col3_b = new Binding("IP");
     BansDataGrid_Col3.Binding = BansDataGrid_Col3_b;
     BansDataGrid.Columns.Add(BansDataGrid_Col3);
     DataGridTextColumn BansDataGrid_Col4 = new DataGridTextColumn();
     BansDataGrid_Col4.Header = "Minutes Left";
     Binding BansDataGrid_Col4_b = new Binding("MinutesLeft");
     BansDataGrid_Col4.Binding = BansDataGrid_Col4_b;
     BansDataGrid.Columns.Add(BansDataGrid_Col4);
     DataGridTextColumn BansDataGrid_Col5 = new DataGridTextColumn();
     BansDataGrid_Col5.Header = "Reason";
     Binding BansDataGrid_Col5_b = new Binding("Reason");
     BansDataGrid_Col5.Binding = BansDataGrid_Col5_b;
     BansDataGrid.Columns.Add(BansDataGrid_Col5);
     Binding binding_BansDataGrid_ItemsSource = new Binding("BansGridDataList");
     BansDataGrid.SetBinding(DataGrid.ItemsSourceProperty, binding_BansDataGrid_ItemsSource);
     items.Add(e_3);
//.........这里部分代码省略.........
开发者ID:PokeD,项目名称:PokeD.SCON,代码行数:101,代码来源:BasicUI.xaml.cs

示例5: CreateGridControl

        /// <summary>
        /// Creates the grid control.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <returns>
        /// The control.
        /// </returns>
        protected virtual FrameworkElement CreateGridControl(PropertyItem property)
        {
            var c = new DataGrid
            {
                CanDelete = property.ListCanRemove,
                CanInsert = property.ListCanAdd,
                InputDirection = property.InputDirection,
                EasyInsert = property.EasyInsert,
                AutoGenerateColumns = property.Columns.Count == 0
            };

            var glc = new GridLengthConverter();
            foreach (var ca in property.Columns.OrderBy(cd => cd.ColumnIndex))
            {
                var cd = new ColumnDefinition
                    {
                        PropertyName = ca.PropertyName,
                        Header = ca.Header,
                        FormatString = ca.FormatString,
                        Width = (GridLength)(glc.ConvertFromInvariantString(ca.Width) ?? GridLength.Auto),
                        IsReadOnly = ca.IsReadOnly
                    };

                if (ca.PropertyName == string.Empty && property.ListItemItemsSource != null)
                {
                    cd.ItemsSource = property.ListItemItemsSource;
                }

                switch (ca.Alignment.ToString(CultureInfo.InvariantCulture).ToUpper())
                {
                    case "L":
                        cd.HorizontalAlignment = HorizontalAlignment.Left;
                        break;
                    case "R":
                        cd.HorizontalAlignment = HorizontalAlignment.Right;
                        break;
                    default:
                        cd.HorizontalAlignment = HorizontalAlignment.Center;
                        break;
                }

                c.ColumnDefinitions.Add(cd);
            }

            c.SetBinding(DataGrid.ItemsSourceProperty, property.CreateBinding());
            return c;
        }
开发者ID:hasol81,项目名称:PropertyTools,代码行数:54,代码来源:DefaultPropertyControlFactory.cs

示例6: CreateGridControl

        /// <summary>
        /// Creates the grid control.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <returns>
        /// The control.
        /// </returns>
        protected virtual FrameworkElement CreateGridControl(PropertyItem property)
        {
            var c = new DataGrid
            {
                CanDelete = property.ListCanRemove,
                CanInsert = property.ListCanAdd,
                InputDirection = property.InputDirection,
                EasyInsert = property.EasyInsert,
                AutoGenerateColumns = property.Columns.Count == 0
            };

            foreach (var cd in property.Columns)
            {
                if (cd.PropertyName == string.Empty && property.ListItemItemsSource != null)
                {
                    cd.ItemsSource = property.ListItemItemsSource;
                }

                c.ColumnDefinitions.Add(cd);
            }

            c.SetBinding(DataGrid.ItemsSourceProperty, property.CreateBinding());
            return c;
        }
开发者ID:yovannyr,项目名称:PropertyTools,代码行数:31,代码来源:DefaultPropertyControlFactory.cs


注:本文中的DataGrid.SetBinding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。