2011-01-07 80 views
2

我想创建一个包含数据网格中的用户控件,然后直接定义我的用户里面列:绑定WPF数据网格中的用户控件内

<my:ControlContainingDataGrid ItemsSource="{Binding}"> 
    <my:ControlContainingDataGrid.Columns> 
    <DataGridTextColumn Binding="{Binding Path=Property1}" Header="Property 1"/> 

在用户控件我揭露DataGrid的列:

static ControlContainingDataGrid() 
{ 
    ColumnsProperty = DependencyProperty.Register(
    "Columns", 
    typeof(ObservableCollection<DataGridColumn>), 
    typeof(ControlContainingDataGrid), 
    new UIPropertyMetadata(new ObservableCollection<DataGridColumn>()) 
); 
} 

[Description("Columns"), Category("Columns")] 
public ObservableCollection<DataGridColumn> Columns 
{ 
    get { return _datagGrid.Columns; } 
} 

public static readonly DependencyProperty ColumnsProperty; 

=>不起作用:绑定到Property1的列未创建。

我尝试以编程方式创建列:

_datagGrid.Columns.Add(new DataGridTextColumn { 
     Header = "Property 1", 
     Binding = new Binding { 
     Path = new PropertyPath("Property1"), 
     Mode = BindingMode.TwoWay, 
     }, 
    }); 

_datagGrid.ItemsSource = testList; 

=>这是行不通的:显示头,但我的DataGrid的每一行是空的(坏的绑定?)。


1 - 什么是通过在XAML部分的用户控件绑定一个DataGrid的列的simpliest方式?

2-什么是通过编程方式通过UserControl绑定DataGrid的列的最简单的方法?

+0

你能发布你的UserControl的XAML吗? – 2011-01-07 19:58:22

回答

0

我以编程方式做到这一点。几乎和你一样的代码。工作正常。我从来没有尝试绑定Columns集合本身,虽然我不明白为什么这不应该工作。

您的消息有点含糊不清。如果你的意思是说你的专栏没有出现,也许你需要在创建表之前添加专栏。如果这些行在列中显示为空值,那么您的绑定'Property1'是错误的。

我不在XAML中工作,所以对此一无所知。