我对wpf很新,所以也许我错过了一些明显的东西。我在stackoverflow中发现了一个类似的问题并尝试了解决方案(在下面给出的代码中实现)但是无法使其工作。链接是here。让我解释一下这个问题。我正在使用框架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>();
}
}
我尝试了很多东西,但是我无法在网格上找到字符串。
请帮忙! 提前谢谢!
此致
萨马
答案 0 :(得分:2)
您只能绑定到公共属性!将str
作为财产。
来自MSDN: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.
答案 1 :(得分:0)
正如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; }
}
答案 2 :(得分:0)
试试这个......
我刚检查了您的样本数据它工作正常我不会进一步测试。
彻底检查....
我刚刚修改了您的代码,并在下面发布。
<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; }
}
}
}
答案 3 :(得分:0)
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; }
}
答案 4 :(得分:-1)
这似乎是一个愚蠢的错误,但是当我在InitializeComponent调用之前初始化observable集合时,它开始显示数据。