繁体   English   中英

在wpf中的usercontrol中分配DataGrid的itemsSource

[英]assign itemsSource of DataGrid inside usercontrol in wpf

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

我有一个usercontrol,其中包含1个datagrid和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>();
    }
}

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

请帮忙! 提前致谢!

问候,

萨马

您只能绑定到公共属性! 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. The 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.

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

尝试将str更改为property:

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; }

}

尝试这个.....

我只是检查了你的样本数据, 它工作正常,我没有进一步测试。

彻底检查....

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

<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你应该使用这个usercontrol

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; }
        }
    }
}

Window没有Datacontext,当你找不到绑定str时。 修改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"
Name="root">

<Grid >
    <ctrls:RadarDataGrid Name="grid1" DataContext="{Binding Path=str, ElementName=root}"/>
</Grid> </Window>

并将str设置为Property

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; }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM