【问题标题】:assign itemsSource of DataGrid inside usercontrol in wpf在 wpf 中的用户控件内分配 DataGrid 的 itemsSource
【发布时间】:2012-08-14 00:53:05
【问题描述】:

我对 wpf 很陌生,所以也许我在这里遗漏了一些明显的东西。我在 stackoverflow 中发现了一个类似的问题,并尝试了解决方案(在下面给出的代码中实现),但无法让它工作。链接是here。让我解释一下这个问题。我正在使用框架 2010。

我有一个包含 1 个数据网格和 3 个按钮的用户控件。相同的xaml如下。

<UserControl x:Class="RadarControls.RadarDataGrid"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
         xmlns:local="clr-namespace:RadarControls"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="25" />
    </Grid.RowDefinitions>

    <DataGrid Name="myGrid" Grid.ColumnSpan="3" ItemsSource="{Binding}" AutoGenerateColumns="True"></DataGrid>

    <Button Content="Add" Grid.Row="1" Height="23" Margin="0,2,2,0" HorizontalAlignment="Left" Name="btnAdd" Width="100" />
    <Button Content="Delete" Grid.Column="1" Grid.Row="1" Margin="0,2,2,0" Height="23" HorizontalAlignment="Left" Name="btnDelete" Width="100" />
    <Button Content="Save" Grid.Column="2" Grid.Row="1" Height="23" Margin="0,2,0,0" HorizontalAlignment="Left" Name="btnSave" Width="100" />
</Grid>
</UserControl>

我正在使用此用户控件的窗口的 xaml 如下

<Window x:Class="RadarStudio.Users"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ctrls="clr-namespace:RadarControls;assembly=RadarControls"
    xmlns:vm='clr-namespace:RadarViewModel.Users;assembly=RadarViewModel'
    Title="Users" Height="300" Width="300">
<Grid >
    <ctrls:RadarDataGrid Name="grid1" DataContext="{Binding str}"></ctrls:RadarDataGrid>
</Grid>

我后面的窗口代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace RadarStudio
{
    /// <summary>
    /// Interaction logic for Users.xaml
    /// </summary>
    public partial class Users : Window
    {
        public Users()
        {
            InitializeComponent();
            //this.DataContext = this;

            str.Add("dhaval");
            str.Add("ravinder");
        }

        public ObservableCollection<string> str = new ObservableCollection<string>();
    }
}

我尝试了很多东西,但我无法在我的网格上找到字符串。

请帮忙! 提前致谢!

问候,

萨马尔

【问题讨论】:

    标签: c# wpf c#-4.0 wpfdatagrid


    【解决方案1】:

    您只能绑定到公共属性!将str 设为属性。

    来自MSDNThe properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.

    You cannot bind to public fields.

    【讨论】:

      【解决方案2】:

      正如 Zabavsky 提到的,WPF 绑定适用于对象的属性,但不适用于字段。

      尝试将 str 更改为属性:

      public partial class Users : Window
      {
          public Users()
          {
              InitializeComponent();
              this.DataContext = this;
      
              str = = new ObservableCollection<string>();
              str.Add("dhaval");
              str.Add("ravinder");
          }
      
          public ObservableCollection<string> str { get; set; }
      
      }
      

      【讨论】:

        【解决方案3】:

        这似乎是一个愚蠢的错误,但是当我在 InitializeComponent 调用之前初始化可观察集合时,它开始显示数据。

        【讨论】:

          【解决方案4】:

          试试这个.....

          我刚刚检查了您的示例数据效果很好我没有进一步测试。

          彻底检查......

          我刚刚修改了你的代码,并发布在下面。

          <UserControl x:Class="RadarControls.RadarDataGrid"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"  >
               <!--xmlns:local="clr-namespace:RadarControls"-->
              <Grid>
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition />
                      <ColumnDefinition />
                      <ColumnDefinition />
                  </Grid.ColumnDefinitions>
                  <Grid.RowDefinitions>
                      <RowDefinition />
                      <RowDefinition Height="25" />
                  </Grid.RowDefinitions>
          
                  <DataGrid Name="myGrid" Grid.ColumnSpan="3"  AutoGenerateColumns="True"></DataGrid> //Data Grid Is Modified
          
                  <Button Content="Add" Grid.Row="1" Height="23" Margin="0,2,2,0" HorizontalAlignment="Left" Name="btnAdd" Width="100" />
                  <Button Content="Delete" Grid.Column="1" Grid.Row="1" Margin="0,2,2,0" Height="23" HorizontalAlignment="Left" Name="btnDelete" Width="100" />
                  <Button Content="Save" Grid.Column="2" Grid.Row="1" Height="23" Margin="0,2,0,0" HorizontalAlignment="Left" Name="btnSave" Width="100" />
              </Grid>
          </UserControl>
          

          您应该使用此用户控件的窗口的 xaml

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Windows;
          using System.Windows.Controls;
          using System.Windows.Data;
          using System.Windows.Documents;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Windows.Media.Imaging;
          using System.Windows.Navigation;
          using System.Windows.Shapes;
          
          namespace RadarControls
          {
              /// <summary>
              /// Interaction logic for RadarDataGrid.xaml
              /// </summary>
              public partial class RadarDataGrid : UserControl
              {
                  public RadarDataGrid()
                  {
                      InitializeComponent();
                  }
          
                  public DataGrid RadarGrid//New Public Class is added
                  {
                      get { return myGrid; }
                  }
              }
          }
          

          您应该使用此用户控件的窗口的 xaml

          <Window x:Class="RadarStudio.Users"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Title="Users" Height="300" Width="300">
              <Grid >
                  <!--xmlns:ctrls="clr-namespace:RadarControls;assembly=RadarControls"
                  xmlns:vm='clr-namespace:RadarViewModel.Users;assembly=RadarViewModel'-->
                  <Grid Name="Dggrid"> </Grid>//New Grid is added as a place holder
              </Grid>
          </Window>
          

          后面的窗口代码如下

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Windows;
          using System.Windows.Controls;
          using System.Windows.Data;
          using System.Windows.Documents;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Windows.Media.Imaging;
          using System.Windows.Shapes;
          using System.Collections.ObjectModel;
          using RadarControls;
          
          namespace RadarStudio
          {
              /// <summary>
              /// Interaction logic for Users.xaml
              /// </summary>
              public partial class Users : Window
              {
                  RadarDataGrid rg = new RadarDataGrid();//Object of user control
                  List<UserList> List = new List<UserList>();// It will be your item source
                  UserList ListItem;
                  public Users()
                  {
                      InitializeComponent();
                      //this.DataContext = this;
                      ListItem = new UserList();
                      ListItem.UserName = "dhaval";//Adding First Item
                      List.Add(ListItem);
                      ListItem = new UserList();
                      ListItem.UserName = "ravinder";//Adding Second
                      List.Add(ListItem);
                      rg.RadarGrid.ItemsSource = List;//Assigned Itemsource
          
                      Dggrid.Children.Add(rg);
                  }
              }
          
          /*This Class is need to store user information, you can include users additional details 
          (if needed) by creating corresponding Properties*/
              public class UserList
              {
                  string userName;
          
                  public string UserName
                  {
                      get { return userName; }
                      set { userName = value; }
                  }
              }
          }
          

          【讨论】:

            【解决方案5】:

            窗口没有Datacontext,当你绑定str时找不到。修改xml:

            <Window x:Class="RadarStudio.Users"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:ctrls="clr-namespace:RadarControls;assembly=RadarControls"
            xmlns:vm='clr-namespace:RadarViewModel.Users;assembly=RadarViewModel'
            Title="Users" Height="300" Width="300"
            Name="root">
            
            <Grid >
                <ctrls:RadarDataGrid Name="grid1" DataContext="{Binding Path=str, ElementName=root}"/>
            </Grid> </Window>
            

            并将 str 设置为属性

            public partial class Users : Window
            {
                public Users()
                {
                    str = new ObservableCollection<string>();
                    InitializeComponent();
                    //this.DataContext = this;
            
                    str.Add("dhaval");
                    str.Add("ravinder");
                }
            
                public ObservableCollection<string> str { get; set; }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-04-22
              • 2014-07-21
              • 2022-01-17
              • 1970-01-01
              • 2021-01-04
              • 2014-10-05
              • 1970-01-01
              相关资源
              最近更新 更多