我是WPF的新手并且只是试图绕过绑定,我可以让datagrid绑定,但我必须以某种方式做到这一点。这是我的类,如果我如图所示抓取构造函数中的数据,它就可以了。我有一个datacontext绑定到Attorney类,项目源绑定到AttyList属性。我也有INotifyChanged工作。
public class Data : CommonBase
{
public Data()
{
getAtty();
}
private List<Attorneys> _AttyList;
public List<Attorneys> AttyList
{
get { return _AttyList; }
set
{
if(value != this._AttyList)
{
_AttyList = value;
NotifyPropertyChanged("AttyList");
}
}
}
public void getAtty()
{
AttyList = new List<Attorneys>();
using (var context = new Context())
{
AttyList = context.Attorney.ToList();
}
}
}
但是,让我说我不想在构造函数中调用getAtty(),但是在Xaml文件的代码中,就像这样,
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var odata = new Data();
odata.getAtty();
}
无论出于何种原因,数据网格都没有填充?我知道我可以在第一个代码帖子的构造函数中做到这一点,但是如果我想单独调用它而不是当我有一个新实例时我就不能这样做。我做错了什么?
编辑:这是我的大部分XAML:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:testWPF"
xmlns:Models="clr-namespace:testWPF.Models" x:Class="testWPF.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized" Loaded="Window_Loaded">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF3E0213" Offset="0.116"/>
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="#FF970202" Offset="0.983"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35*"/>
<ColumnDefinition Width="12*"/>
</Grid.ColumnDefinitions>
<DataGrid x:Name="dgvAllAttorneys" HorizontalAlignment="Left" Margin="84,26,0,0" MouseDoubleClick="dgvAllAttorneys_MouseDoubleClick" VerticalAlignment="Top" Height="154" Width="208" ItemsSource="{Binding AttyList}" AutoGenerateColumns="False" SelectionMode="Single" IsReadOnly="True" Grid.ColumnSpan="1">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" ClipboardContentBinding="{x:Null}" Header="FirstName"/>
<DataGridTextColumn Binding="{Binding FullName}" ClipboardContentBinding="{x:Null}" Header="LastName" Width="*"/>
</DataGrid.Columns>
<DataGrid.DataContext>
<Models:Data/>
</DataGrid.DataContext>
</DataGrid>
</Grid>
答案 0 :(得分:2)
使用ObservableCollection<Attorneys>
代替List,以便向DataGrid通知更改。
public class Data : CommonBase
{
public Data()
{
getAtty();
}
private ObservableCollection<Attorneys> _AttyList;
public ObservableCollection<Attorneys> AttyList
{
get { return _AttyList; }
set
{
if(value != this._AttyList)
{
_AttyList = value;
NotifyPropertyChanged("AttyList");
}
}
}
public void getAtty()
{
AttyList = new ObservableCollection<Attorneys>();
using (var context = new Context())
{
AttyList = context.Attorney.ToList();
}
}
}
在您设置DataContext的XAML中,为该实例命名。
<DataGrid.DataContext>
<Models:Data x:Name="dataContext" />
<DataGrid.DataContext>
然后在Window的Loaded事件处理程序中,引用该实例来调用getAtty()
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dataContext.getAtty();
}